Start and Stop Azure VM’s in Parallel

During a project I had to start and stop about 100 VMs, if I put the runbook in serial mode, it took about 2 hours to be execute, so… the only way to do it in a fast way, was to program the runbook to be executed in parallel mode. How can it be done? Easy!
#list and start vm with tag "OpeningHours" and value "7 to 19"
$vms = get-azvm | where {$_.Tags["OpeningHours"] -contains "7 to 16"}
$jobs = @()
foreach ($vm in $vms)
{
$job = Start-Job -ScriptBlock {
start-Azvm -Name $vm.name
}
$jobs = $jobs + $job
}
# Wait for it all to complete
Wait-Job -Job $jobs

That’s all, you can change the start-azvm with a stop-azvm -force and you will be done

Advertisement

12 thoughts on “Start and Stop Azure VM’s in Parallel

  1. Hi

    Thanks for this script.

    I’m quite new to Powershell and had a problem with it or 2 actually.

    Firstly it wouldn’t recognise the $vm.name in the ScriptBlock, I fixed this by replacing with $using:vm.name
    Then it prompted me for a resourcegroup.

    So ultimately I replaced
    start-Azvm -Name $vm.name
    with
    $using:vm | start-Azvm

    Hope this helps you / others.

    Thanks again

    Chris.

    I’m quite new to Powershell and had a problem with it or 2 actually.

    Firstly it wouldn’t recognise the $vm.name in the ScriptBlock, I fixed this by replacing with $using:vm.name
    Then it prompted me for a resourcegroup.

    So ultimately I replaced
    start-Azvm -Name $vm.name
    with
    $using:vm | start-Azvm

    Hope this helps you / others.

    Thanks again

    Chris.

    Liked by 1 person

      1. Thank you @Albandrod and Chris for the comments,
        Start-Job -ScriptBlock { $using:itemName | Start-AzVM -ResourceGroupName $LoadAgentResourceGroup -Name $itemName }

        Start-Job -ScriptBlock { $using:itemName.Name | Start-AzVM -ResourceGroupName $LoadAgentResourceGroup -Name $itemName.Name }

        It is not working for me, could you please help me what I missed here

        Like

  2. Thank you @Albandrod and Chris for the comments,
    Start-Job -ScriptBlock { $using:itemName | Start-AzVM -ResourceGroupName $LoadAgentResourceGroup -Name $itemName }

    Start-Job -ScriptBlock { $using:itemName.Name | Start-AzVM -ResourceGroupName $LoadAgentResourceGroup -Name $itemName.Name }

    It is not working for me, could you please help me what I missed here (for me it is Itemname is the vmname), I have provided it like this $itemName = ‘npd-jmetr-app01′,’npd-jmetr-app02′,’npd-jmetr-app03′,’npd-jmetr-app04′,’npd-jmetr-app05’;

    Like

  3. declaring $itemName should be like $itemName=(“npd-jmetr-app01″,”npd-jmetr-app02″,”npd-jmetr-app03″,”npd-jmetr-app04″,”npd-jmetr-app05”), check the variable $LoadAgentResourceGroup, are the Vm in different RG?

    Like

  4. Thank you albandrod, mistake was with providing the parameters, solve it by
    $StartVMjobs = @()
    $parameters = @($VMName, $ResourceGroupName)
    $Startjob = Start-Job -ScriptBlock {
    param($VM_NAME, $RG_Name)
    Start-AzVM -Name $VM_NAME -ResourceGroupName $RG_Name
    } -ArgumentList $parameters

    $StartVMjobs = $StartVMjobs + $Startjob

    Like

  5. I have similar another problem, like I have 5 Linux machines, I have user name and password to connect them over SSH (currently using putty client to connect them).

    I want to automate it through PowerShell for the following steps,
    1. Want to connect to each machine.
    2. Navigate through to a particular folder (/home/server/)
    3. Run the command “nohup ./server &” in all machine in parallel.

    Could you please put some insight on this ?

    Like

  6. I have similar another problem, like I have 5 Linux machines, I have user name and password to connect them over SSH (currently using putty client to connect them).

    I want to automate it through PowerShell for the following steps,
    1. Want to connect to each machine.
    2. Navigate through to a particular folder (/home/server/)
    3. Run the command “nohup ./server &” in all machine in parallel.

    Could you please put some insight on this ?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

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