Creating Azure Virtual Networks using Powershell and XML Part 3: Powershell functions for deletion

This is part three of a series of posts about using powershell to script the creation, deletion and (hopefully) modification of Azure Virtual Networks. In part 1 I went through the key steps with some rough code. Part 2 showed the much tidier functions I’ve now written to create virtual network elements. This is part 3, and I will present functions to remove elements. Hopefully I will manage to get the modification functions to work which be a fourth installment!

I’m not going to go through how to use the new functions in this part – I covered that before. I’m simply going to present the new functions that perform the following actions:

  1. Remove an entire virtual network definition.
  2. Remove a DNS definition.
  3. Remove a single subnet from a virtual network.
  4. Remove a DNS registration from a virtual network.

The big thing I learned when writing this code is that if I used the RemoveAll method on an xml node in my configuration xml object, it didn’t actually remove the node itself but only the attributes and child nodes. This left empty elements (such as <VirtualNetworkSite />) that confused Azure. The solution was to call the RemoveChild method on the parent node of the one I wanted rid of, specifying my target node.

The Functions

Delete-azureVnetNetwork

Delete-azureVnetNetwork takes one parameter: networkName. It makes sure the network exists, then removes the appropriate VirtualNetworkSite node and all it’s children.

function delete-azureVnetNetwork { param ( [string]$networkName )  #check that the network already exists $network = $workingVnetConfig.GetElementsByTagName("VirtualNetworkSite") | where {$_.name -eq $networkName} if ($network.Count -eq 0) { 	write-Output "Network $networkName does not exist" 	$removeNetwork = $null 	return $removeNetwork }  #remove the node and children $network.ParentNode.RemoveChild($network)   #return true as we deleted the node $removeNetwork = $true  return $removeNetwork } 

Delete-azureVnetSubnet

Delete-azureVnetSubnet takes two parameters: networkName and subnetName. It checks to make sure both exist, then removes the appropriate Subnet element from the specified network.

function delete-azureVnetSubnet { param ( [string]$networkName, [string]$subnetName )  #check that the network  exists $network = $workingVnetConfig.GetElementsByTagName("VirtualNetworkSite") | where {$_.name -eq $networkName} if ($network.Count -eq 0) { 	write-Output "Network $networkName does not exist" 	$removeSubnet = $null 	return $removeSubnet }  #check to make sure our subnet name exists $subNet = $network.GetElementsByTagName("Subnet") | where {$_.name -eq $subnetName} if ($subNet.count -eq 0) { 	write-Output "Subnet $subnetName does not exist in network" 	$removeSubnet = $null 	return $removeSubnet  }  #remove the node and children $subNet.ParentNode.RemoveChild($subNet)  #return true as we deleted the node $removeSubnet = $true return $removeSubnet } 

Delete-azureVnetDnsRef

Delete-azureVnetDnsRef takes two parameters: networkName and dnsName. It checks to make sure both the network and the DNS reference within it exist, then removes the appropriate DnsServerRef element from the specified network.

function delete-azureVnetDnsRef { param ( [string]$networkName, [string]$dnsName )  #check that the network  exists $network = $workingVnetConfig.GetElementsByTagName("VirtualNetworkSite") | where {$_.name -eq $networkName} if ($network.Count -eq 0) { 	write-Output "Network $networkName does not exist" 	$removeDnsRef = $null 	return $removeDnsRef }  #check that the dns reference is there $dnsRef = $network.GetElementsByTagName("DnsServerRef") | where {$_.name -eq $dnsName} if ($dnsRef.count -eq 0) { 	write-Output "DNS reference $dnsName does not exist" 	$removeDnsRef = $null 	return $removeDnsRef }  #remove the node and children $dnsRef.ParentNode.RemoveChild($dnsRef)   #return true as we deleted the node $removeDnsRef = $true  return $removeDnsRef } 

Delete-azureVnetDns

Delete-azureVnetDnsRef takes one parameter: dnsName. It checks to make sure that the DNS is not referenced by any virtual networks and that the DNS exists, then removes the appropriate DnsServer element.

function delete-azureVnetDns { param ( [string]$dnsName )  #check that the dns isn't referenced in any networks $dnsRef = $workingVnetConfig.GetElementsByTagName("DnsServerRef") | where {$_.name -eq $dnsName} if ($dnsRef.count -ne 0) { 	write-Output "DNS $dnsName is referenced in networks" 	$removeDns = $null 	return $removeDnsRef } #check that the DNS exists $dns = $workingVnetConfig.GetElementsByTagName("DnsServer") | where {$_.name -eq $dnsName} if ($dns.Count -eq 0) { 	write-Output "DNS Server $dnsName does not exists" 	$removeDns = $null 	return $removeDns }  #remove the node and children $dns.ParentNode.RemoveChild($dns)   #return true as we deleted the node $removeDns = $true  return $removeDns }

Using the functions

These functions modify an XML configuration that needs to be held in an object call $workingVnetConfig. My previous post showed how they can be loaded from a powershell file and called. Get-azureNetworkXml is required to get the XML configuration object. The functions here can then be used to remove items from that configuration, then save-azureNetworkXml will push the modified configuration back into Azure.

Leave a Reply

Your email address will not be published. Required fields are marked *