Friday, September 7, 2018

Start Azure VMs using PowerShell workflow

Today I provide you two scripts to start your Azure VMs in a specified Subscription. The first script will start some VMs and the second will start all VMs of your Subscription.

Start some VMs in a Subscription

workflow StartADVMs
{
$Cred = Get-AutomationPSCredential -Name 'admad'
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -SubscriptionName "MYSubscriptionName"

Get-AzureVM -Name "ad1" -ServiceName "dcs-01" | Start-AzureVM
Get-AzureVM -Name "ad2" -ServiceName "dcs-01" | Start-AzureVM
Get-AzureVM -Name "ad3" -ServiceName "dcs-01" | Start-AzureVM
Get-AzureVM -Name "ad4" -ServiceName "dcs-01" | Start-AzureVM
}


Start all VMs in a Subscription

workflow StartAllVMs
{   
$Cred = Get-AutomationPSCredential -Name 'admad'
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -SubscriptionName "MYSubscriptionName"
       
         $VMS = Get-AzureVM
           
             foreach($VM in $VMS)
                 {                   
                     $VMName = $VM.Name
                     Start-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name -Force
                     Write-Output "Starting VMs :  $VMName "           
                  }


No comments:

Post a Comment