Azure - Copy Virtual Network - Step One
2015-01-25 by
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