Skip to content

Instantly share code, notes, and snippets.

@sitereactor
Created December 27, 2014 11:20
Show Gist options
  • Save sitereactor/2a4886a39395c9656f1c to your computer and use it in GitHub Desktop.
Save sitereactor/2a4886a39395c9656f1c to your computer and use it in GitHub Desktop.
Powershell script for stopping Azure VMs on a schedule in an Azure Automation runbook
workflow Stop-AzureVMsOnSchedule {
param(
# The name of the VM(s) to start on schedule. Can be wildcard pattern.
[Parameter(Mandatory = $true)]
[string]$VMName,
# The service name that $VMName belongs to.
[Parameter(Mandatory = $true)]
[string]$ServiceName,
# The name of the subscription.
[Parameter(Mandatory = $true)]
[string]$SubName,
# The name of the OrgCredUser.
[Parameter(Mandatory = $true)]
[string]$PsUserName
)
# Specify Azure Subscription Name
$Cred = Get-AutomationPSCredential -Name $PsUserName
# Connect to Azure
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -SubscriptionName $SubName
$vm = Get-AzureVM -ServiceName $ServiceName -Name $VMName
Stop-AzureVM -ServiceName $vm.ServiceName -Name $vm.Name -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment