Thursday, May 15, 2014

Testing is messy! Time to clean up.

In creating a script to build a lab, I forgot to include time to tear the lab down.  I have been doing some manual tear down, but that is getting old fast.  So, I am backing up a bit and will create a teardown script in parallel.  For now, here is tearing down the last thing built, the network:

# ========================================================
#
# Script Information
#
# Title: DeleteLab
# Author: Larry Smithmier
# Originally created: 5/15/2014 - 09:11:25
# Original path: DeleteLab.ps1
# Description: Delete a SharePoint lab in Azure
#
# ========================================================
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, Position = 1)]
[string]$subscriptionName,

[Parameter(Mandatory = $True)]
[string]$labName
)
Add-AzureAccount
Write-Host "Deleting " -NoNewline
Write-Host $labName -ForegroundColor Yellow -NoNewline
Write-Host " using the " -NoNewline
Write-Host $subscriptionName -ForegroundColor Yellow -NoNewline
Write-Host " Azure subscription"
Write-Host "=============================================================="

################################################################################
Write-Host
Write-Host "1. Selecting Subscription " -NoNewline
Write-Host $subscriptionName -ForegroundColor Yellow
select-AzureSubscription $subscriptionName

################################################################################
Write-Host
Write-Host "4. Update the Network Configuration file to remove " -NoNewline
Write-Host $labName -ForegroundColor Yellow

#using simple text replacement
$configurationFileName = Get-ChildItem ".\NetworkConfig.netcfg"

Get-AzureVNetConfig -ExportToFile $configurationFileName.FullName

$XMLFile = New-Object xml
$XMLFile.Load($configurationFileName.FullName)

$XMLFile.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.ChildNodes | Where-Object { $_.name -eq $labName } | ForEach-Object { $XMLFile.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.RemoveChild($_) }

$configurationFileName = $configurationFileName.FullName.Replace("NetworkConfig","$labName")

$XMLFile.Save($configurationFileName)

################################################################################
Write-Host
Write-Host "5. Removing Azure Network using the configuration file " -NoNewline
Write-Host $configurationFileName.FullName -ForegroundColor Yellow

Select-AzureSubscription $subscriptionName
Set-AzureSubscription $subscriptionName -CurrentStorageAccount $storageName
Remove-AzureVNetConfig -ErrorAction SilentlyContinue
Set-AzureVNetConfig -ConfigurationPath $configurationFileName

No comments:

Post a Comment