Created
January 27, 2017 14:04
-
-
Save raghur/1fef6c8c128193f7893abe5aeb5f45ce to your computer and use it in GitHub Desktop.
Start/stop VM using azure automation
This file contains hidden or 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
| param ( | |
| [string] $action, | |
| [string] $resourcegroup, | |
| [string] $vmname | |
| ) | |
| function Send-Push{ | |
| param( | |
| [string] $action, | |
| [string] $vmname, | |
| [string] $message | |
| ) | |
| $uri = 'https://api.pushover.net/1/messages.json' | |
| $parameters = @{ | |
| token = "yourapptoken" | |
| user = "yourusertoken" | |
| message = $message | |
| title = "$vmname - $action" | |
| } | |
| $parameters | Invoke-RestMethod -Uri $uri -Method Post | |
| } | |
| $creds = Get-AutomationPSCredential -Name 'automation-creds' | |
| Add-AzureRmAccount -Credential $creds | |
| if ($action -eq "stop") { | |
| "Stopping VM $vmname in resource group $resourcegroup..." | |
| Stop-AzureRmVM -ResourceGroupName $resourcegroup -Name $vmname -force | |
| Send-Push "Stopped" $vmname "completed successfully" | |
| } else { | |
| $weekday = (Get-Date).DayOfWeek | |
| if ( $weekday -eq 'Saturday' -or $weekday -eq 'Sunday' ) { | |
| Send-Push "" $vmname "It's a weekend - skipping starting VM" | |
| return | |
| } | |
| "Starting VM $vmname in resource group $resourcegroup..." | |
| Start-AzureRmVM -ResourceGroupName $resourcegroup -Name $vmname | |
| Send-Push $action $vmname "VM started" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment