Skip to content

Instantly share code, notes, and snippets.

@raghur
Created January 27, 2017 14:04
Show Gist options
  • Select an option

  • Save raghur/1fef6c8c128193f7893abe5aeb5f45ce to your computer and use it in GitHub Desktop.

Select an option

Save raghur/1fef6c8c128193f7893abe5aeb5f45ce to your computer and use it in GitHub Desktop.
Start/stop VM using azure automation
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