Skip to content

Instantly share code, notes, and snippets.

@pkirch
Created April 14, 2015 12:22

Revisions

  1. pkirch created this gist Apr 14, 2015.
    33 changes: 33 additions & 0 deletions Stop-ITCampManagementVMs.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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
    }
    }