Friday, May 16, 2014

Back to parity, cleanup script

Below is the finalized cleanup script that corresponds to our current state of buildout:

# ========================================================
#
# 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 "2. 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 "3. 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

################################################################################
Write-Host
Write-Host "4. Remove the lab Storage Account " -NoNewline
Write-Host $labName -ForegroundColor Yellow

Get-AzureStorageAccount | Where-Object { $_.label -eq $labName } | Remove-AzureStorageAccount

################################################################################
Write-Host
Write-Host "5. Creating Affinity Group " -NoNewline
Write-Host $labName -ForegroundColor Yellow

Get-AzureAffinityGroup | Where-Object { $_.Name -eq $labName } | Remove-AzureAffinityGroup

No comments:

Post a Comment