Sequentially Start & Stop Azure VMs

You can see the Azure automation version of sequentially starting & stopping Azure VMs on my other post.

Have you ever wanted to sequentially start and stop your Azure Virtual Machines all in one hit? Not just Azure VMs, you can use this same method for any sequential job you want to run in Azure.

The following script will start and stop your Azure VMs in parallel by using background jobs in PowerShell. Please note, this script is based on Azure Resource Manager, however with easy change to the code, this same structure will still work with Azure Service Management.

Also note that this script won’t work if you’re logging onto Azure with a Microsoft account. In this instance, you would need to go into the Azure AD directory and create a user in the Azure AD organisation directory in which is linked to your Azure subscription. Make this user a (co-administrator or contributor) of Azure and use this account with the below PowerShell script.


$subscription = 'Visual Studio Ultimate with MSDN'
$username = "powershell@tenant.onmicrosoft.com"
$password = "Password"
### Log into Azure with an organisational account
$secpasswd = ConvertTo-SecureString $password AsPlainText Force
$Cred = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
LoginAzureRmAccount Credential $Cred
#Choose subscription
Select-AzureRmSubscription SubscriptionName $subscription
###############################################################################################################
# Sequentially start Azure VMs
Get-AzureRmVM | where {$_.Name -match 'playlist'}| %{
echo $_.Name
$scriptBlock = {
param($Arg0, $Arg1, $Arg2, $Arg3, $Arg4)
### Log into Azure with an organisational account
$secpasswd = ConvertTo-SecureString $Arg3 AsPlainText Force
$Cred = New-Object System.Management.Automation.PSCredential ($Arg2, $secpasswd)
LoginAzureRmAccount Credential $Cred
#Choose subscription
Select-AzureRmSubscription SubscriptionName $Arg4
Start-AzureRmVM Name $Arg0 ResourceGroupName $Arg1
}
Start-Job $scriptBlock ArgumentList @($_.Name, $_.ResourceGroupName, $username, $password, $subscription)
}
# Wait for it all to complete
While (Get-Job State "Running")
{
Start-Sleep 10
echo '—————————'
Get-Job
Get-Job | Receive-Job
}
Remove-Job *
###############################################################################################################
# Sequentially stop Azure VMs
Get-AzureRmVM | where {$_.Name -match 'playlist'}| %{
echo $_.Name
$scriptBlock = {
param($Arg0, $Arg1, $Arg2, $Arg3, $Arg4)
### Log into Azure with an organisational account
$secpasswd = ConvertTo-SecureString $Arg3 AsPlainText Force
$Cred = New-Object System.Management.Automation.PSCredential ($Arg2, $secpasswd)
LoginAzureRmAccount Credential $Cred
#Choose subscription
Select-AzureRmSubscription SubscriptionName $Arg4
Stop-AzureRmVM Name $Arg0 ResourceGroupName $Arg1 Force
}
Start-Job $scriptBlock ArgumentList @($_.Name, $_.ResourceGroupName, $username, $password, $subscription)
}
# Wait for it all to complete
While (Get-Job State "Running")
{
Start-Sleep 10
echo '—————————'
Get-Job
Get-Job | Receive-Job
}
Remove-Job *

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s