Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjwaight/69db4ed51a2be401e8fd to your computer and use it in GitHub Desktop.
Save sjwaight/69db4ed51a2be401e8fd to your computer and use it in GitHub Desktop.
Example script that shows you how you can retrieve all virtual machines in an Azure subscription and then shut them down.
# Based on the August 2014 PowerShell Cmdlets (v2.4 of Azure SDK)
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1"
Function ForceShutdownVM($virtualMachine)
{
Write-Host "Looking at host" $_.Name -ForegroundColor Yellow
if($_.Status -ne "StoppedDeallocated")
{
if($_.InstanceName -Like "*_IN_*")
{
Write-Host "`tThis is a Cloud Service and requires deletion to save run costs." -ForegroundColor Red
}
else
{
Write-Host "`tForcing Shutdown of VM" -ForegroundColor Red
Stop-AzureVM -Name $_.Name -Force
}
}
else
{
Write-Host "`tVM is already off" -ForegroundColor Green
}
}
# Assumes you have previously downloaded the subscription details
# using the Get-AzurePublishSettingsFile Cmdlet
Select-AzureSubscription -SubscriptionName $Args[0]
Get-AzureVM | ForEach-Object {ForceShutdownVM($_)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment