Azure - Copy Virtual Network - Step One

A Tutorial on how to copy an entire azure virtual network with a subscription to a new environment for testing, training or QA.  The goal here is no down time.

Azure - Copy Virtual Network - Step One

 

Here is a PowerShell script to copy a virtual network.  This is the first version, subsequent version may contain revisions to shut down servers and do other administrative tasks.  The goal here is to come up with a way to successfully make an exact replica of a production environment for use by developers, QA team, and Business Analysts.  Please bear with me as this is step one.

NOTE:  Set these Variables in advance

 

 

$SubscriptionName = ""

$SourceEnvironmentName = ""

$DestinationEnvironmentName = ""

$netConfigPath = ""

 

 

function UpdateNetworkXML

{

 

    $xml = [xml](Get-Content $args[0])

   

    $sourceNode = $xml.SelectSingleNode("/*[local-name()='NetworkConfiguration']/*[local-name()='VirtualNetworkConfiguration']/*[local-name()='VirtualNetworkSites']/*[local-name()='VirtualNetworkSite' and @name='" + $SourceEnvironmentName + "']")

    $destinationNode = $xml.SelectSingleNode("/*[local-name()='NetworkConfiguration']/*[local-name()='VirtualNetworkConfiguration']/*[local-name()='VirtualNetworkSites']/*[local-name()='VirtualNetworkSite' and @name='" + $DestinationEnvironmentName + "']")

 

    if ($destinationNode -eq $null)

            {

                $destinationNode = $sourceNode.CloneNode($true)

                $sourceNode.ParentNode.AppendChild($destinationNode)

            }

   $destinationNode.Attributes["name"].Value = $DestinationEnvironmentName;

   $xml.Save($args[0])

}

 

Add-AzureAccount

Select-AzureSubscription $SubscriptionName

Get-AzureVNetConfig -ExportToFile $netConfigPath

UpdateNetworkXML $netConfigPath

Write-Output "Updating Virtual Networks"

Set-AzureVNetConfig -ConfigurationPath $netConfigPath

 

Last Updated: 1/25/2015 1:47:00 PM