Please note, this can be scheduled with azure automation
To set up creds to connect see Authenticating to Azure using Azure Active Directory
At TrueNorth we try to get the most out azure. We use it for Development, PreSales, Testing infrastructure architectures, Performance testing and more. It so easy to forget to turn a VM off and then you find yourself trying to navigate the site on your mobile to deallocate your VM.
So, we have created a PowerShell script that can be triggered at a set time, the script enumerates all your VMs and powers them down.
As we use our BizSpark subscription for non customer VMs it allows us filter with
Select-AzureSubscription BizSpark
<br /> workflow ShutDown-AllVMs {<br /> param (<br /> [parameter(Mandatory=$true)]<br /> [String] $VMCredentialName = "YOUR ASSET NAME"<br /> )</p> <p>$Credential = Get-AutomationPSCredential -Name $VMCredentialName</p> <p>if ($Credential -eq $null) {<br /> throw "Could not retrieve ‘$VMCredentialName’ credential asset. Check that you created this asset in the Automation service."<br /> }</p> <p>Add-AzureAccount -Credential $Credential</p> <p>Select-AzureSubscription BizSpark</p> <p>InlineScript {<br /> Get-azurevm | ? { $_.Status -eq "StoppedVM"} | Stop-AzureVM -Force<br /> Get-azurevm | ? { $_.Status -ne "StoppedDeallocated"} | Stop-AzureVM -Force</p> <p># if we miss anything, this will not include VMs that are starting up<br /> Get-azurevm | ? { $_.Status -eq "ReadyRole"} | Stop-AzureVM -Force<br /> }<br /> }<br />
This is scheduled with the Windows Task Scheduler, it run at 8PM and will wake up the laptop if needed (it will go back to sleep). We could use the Scheduler feature of Azure but for the purpose of this post we are just going to trigger the PS script.
We are also looking into creating an Azure AddOn that will just deallocates your VM when its not in use after or at certain times.