Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Last active August 29, 2015 14:05
Embed
What would you like to do?
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