Skip to content

Instantly share code, notes, and snippets.

@nidr0x
Created March 12, 2018 12:12
Show Gist options
  • Save nidr0x/b85b7b78204b51ed209fcac3b639cf81 to your computer and use it in GitHub Desktop.
Save nidr0x/b85b7b78204b51ed209fcac3b639cf81 to your computer and use it in GitHub Desktop.
Schedule startup and shutdown of Azure Machines via PowerShell
# Input Parameters for
# - VmName: name of the vm to perform action to
# - ResourceGroupName: resource group where the vm belongs to
# - VmAction:action to perform (startup or shutdown)
Param(
[string]$VmName,
[string]$ResourceGroupName,
[ValidateSet("Startup", "Shutdown")]
[string]$VmAction
)
# Authenticate with your Automation Account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
# Startup VM
IF ($VmAction -eq "Startup") {
Start-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName
}
# Shutdown VM
IF ($VmAction -eq "Shutdown") {
Stop-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment