Created
March 7, 2015 07:38
-
-
Save scott-kloud/599e99af0b1c22894af2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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