Created
March 12, 2018 12:12
-
-
Save nidr0x/b85b7b78204b51ed209fcac3b639cf81 to your computer and use it in GitHub Desktop.
Schedule startup and shutdown of Azure Machines via PowerShell
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
# 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