Skip to content

Instantly share code, notes, and snippets.

@scott-kloud
Created March 7, 2015 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scott-kloud/599e99af0b1c22894af2 to your computer and use it in GitHub Desktop.
Save scott-kloud/599e99af0b1c22894af2 to your computer and use it in GitHub Desktop.
workflow AutoShutdownWorkflow
{
#$VerbosePreference = "continue"
# Automation Settings
$pscreds = Get-AutomationPSCredential -Name "runbook-account"
$subscriptionName = "[subscription name here]"
$tagName = "autoShutdown"
# Authenticate using WAAD credentials
Add-AzureAccount -Credential $pscreds | Write-Verbose
# Set subscription context
Select-AzureSubscription -SubscriptionName $subscriptionName | Write-Verbose
Write-Output "Checking for resources with $tagName flag set..."
# Get virtual machines within tagged resource groups
$vms = Get-AzureResourceGroup -Tag @{ Name=$tagName; Value=$true } | Get-AzureResource -ResourceType "Microsoft.ClassicCompute/virtualMachines"
# Shutdown all VMs tagged
$vms | ForEach-Object {
Write-Output "Shutting down $($_.Name)..."
# Gather resource details
$resource = $_
# Stop VM
Get-AzureVM | ? { $_.Name -eq $resource.Name } | Stop-AzureVM -Force
}
Write-Output "Completed $tagName check"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment