Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Restart multiple Azure VMs

$
0
0

Hello,

I am trying to restart multiple VMs in parallel. But for some reason the code doesn't work. I can see proper messages from the Write-Host (proper name and serviceName) but the restart is not done.

$vms = Get-AzureVM | Where { $_.Name -like "TEST*" } | %{
    $vm = $_   
    $ScriptBlock = {
        $vmName = ($using:vm).Name
        $serviceName = ($using:vm).ServiceName
        Write-Host "Name='$vmName', ServiceName='$serviceName'"
        Restart-AzureVM -Name $vmName -ServiceName $serviceName
    }
    Start-Job $ScriptBlock
}
while (Get-Job -State Running)
{
    Start-Sleep 1
}
Get-Job | Receive-Job

When I update the code to:

$vms = Get-AzureVM | Where { $_.Name -like "TEST*" } | Foreach { Restart-AzureVM -ServiceName $_.ServiceName -Name $_.Name }

VMs are restarted, however in sequence, one by one. Anyone who can give me some advice what is wrong? Thank you.


Viewing all articles
Browse latest Browse all 6937

Trending Articles