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
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.
LikeLiked by 1 person
Thanks for your comment Chris!
LikeLike
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
LikeLike
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’;
LikeLike
Which problem are you having? can you explain the error?
LikeLike
In the console it is triggering without an error but the VM is still in deallocated state
LikeLike
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?
LikeLike
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
LikeLike
Glad you’ve been able to solve it 🙂
LikeLike
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 ?
LikeLike
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 ?
LikeLike
Have you thought about running the script in AutomationAccount with SSH module enabled?
LikeLike