Automatically shutting down VMs in Azure

Well it’s been a short time since I made the transition from DW/BI to Cloud.  I’ve learned a couple of interesting things.

I was trying to find/write a script that would shut down all my VMs.  They are all over the place, so I had ample opportunity to borrow/steal code.

I implemented the script as an Automation Account Runbook.  Kinda like SQL Server Agent for Azure.  Except on steriods.  So I created a Credential, got a valid login created and ran a script that looked suspiciously like this one.

What was weird is my VMs, deployed as RM, not Classic, were still running.

Here’s what I found. It was getting a list of VMs using Get-AzureVM.

Like I said, my VMs were deployed as RM.  Instead, the call would be Get-AzureRmVM.  This gets the properties of RM VMs.  So I re-wrote the script.

By the way, PowerShell Workflows don’t like Write-Host.  Instead, use Write-Output.  The output will be visible in the Test Pane.  Hey, you can even edit the PowerShell script directly in the portal.

Oh, and don’t use -StayProvisioned flag.  While the VM will be considered shut down, you’ll still accrue charges.

I don’t know if this is the “right” way to do things, but it works for me.  There’s some hardcoded values in here that will have to be removed.  Call this a work-in-progress

workflow runShutDownVMs
{
 $cred = Get-AutomationPSCredential -Name '{nameOfACredential}'
 Write-Output ("Credential User Name: " + $cred.UserName)
 
 $acct = Add-AzureAccount -Credential $cred
 Write-Output "Adding Credential to Azure Account"

$subscription = Select-AzureSubscription -SubscriptionName "{nameOfYourSubscription}"
 $subscriptionName = (Get-AzureSubscription | Where-Object {$_.IsCurrent -eq $true }).SubscriptionName
 Write-Output "Getting access to subscription $subscriptionName (should be [Marc Jellinek - Visual Studio Enterprise])"

$VMs = Get-AzureVM
 $msg = "Discovered [" + [string]($VMs.Count) + "] Classic Virtual Machines"
 Write-Output $msg

foreach ( $VM in $VMs )
 {
 $msg = "Shutting down Classic VM: [" + $VM.Name + "]"
 Write-Output $msg
 $stopped = Stop-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name -Force
 }

Login-AzureRmAccount -Credential $cred
 Write-Output "Logging in to Azure RM Account"
 
 $acct = Add-AzureRmAccount -Credential $cred
 Write-Output "Adding Credential to Azure RM Account"
 
 Get-AzureRMSubscription -SubscriptionName $subscriptionName | Select-AzureRMSubscription
 Write-Output "Getting access to subscription $subscriptionName (should be [Marc Jellinek - Visual Studio Enterprise])"

$resGroups = Get-AzureRmResourceGroup
 Write-Output "Getting Access to Resource Groups"
 
 foreach ($resGroup in $resGroups)
 {
 Write-Output "Processing Resource Group " + $resGroup.ResourceGroupName
 
 $VMs = Get-AzureRmVM -ResourceGroupName $resGroup.ResourceGroupName
 $msg = "Discovered [" + [string]($VMs.Count) + "] RM Virtual Machines"
 Write-Output $msg
 
 foreach ( $VM in $VMs )
 {
 $msg = "Shutting down RM VM: [" + $VM.Name + "]"
 Write-Output $msg
 $stopped = Stop-AzureRmVM -Name $VM.Name -ResourceGroupName $resGroup.ResourceGroupName -Force
 }
 }
}
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: