Skip to content

Instantly share code, notes, and snippets.

@pkirch
Created April 14, 2015 12:22
Show Gist options
  • Save pkirch/798c60d63e549701524c to your computer and use it in GitHub Desktop.
Save pkirch/798c60d63e549701524c to your computer and use it in GitHub Desktop.
PowerShell Workflow script for Azure Automation to remove a bulk of VMs. (For production use you should add some error handling.)
workflow Stop-ITCampManagementVMs
{
Param (
# Name of the target Azure subscription.
[Parameter(Mandatory=$True)]
[String]$SubscriptionName = "MSFT IT Camp Stage",
[Parameter(Mandatory=$False)]
[String]$NamePrefix = "itc0203"
)
# Get the Azure connection asset that is stored in the Automation service based on the name that was passed into the runbook
$AzureConn = Get-AutomationConnection -Name $SubscriptionName
# Get the Azure management certificate that is used to connect to this subscription
$Certificate = Get-AutomationCertificate -Name $AzureConn.AutomationCertificateName
# Set the Azure subscription configuration
Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $AzureConn.SubscriptionID -Certificate $Certificate
# Select the Azure subscription.
Select-AzureSubscription -SubscriptionName $SubscriptionName
$matchingCloudServices = Get-AzureService | Where-Object -Property ServiceName -ilike "$NamePrefix*"
foreach -Parallel ($service in $matchingCloudServices) {
Write-Warning "Removing $($service.ServiceName)"
Remove-AzureService -ServiceName $service.ServiceName -DeleteAll -Force
}
$matchingStorageAccounts = Get-AzureStorageAccount | Where-Object -Property StorageAccountName -ilike "$NamePrefix*"
foreach -Parallel ($storage in $matchingStorageAccounts) {
Write-Warning "Removing $($storage.StorageAccountName)"
Remove-AzureStorageAccount -StorageAccountName $storage.StorageAccountName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment