0
votes

I tried to run few Azure cmdlets in background (i tried it by switch parameter "-asJob" and by with Start-Job cmdlet), i push cmdlet operations to the hashtable with certian keys and then iterate running jobs. The problem is that all cmdlet change state to "Completed" in the same time, even if some job in hashtable actually ended by "Completed".
Feels like that all jobs end with last ending job. Below i write some example code with problem

$vmssInstances = Get-AzVmssVM -ResourceGroupName $(agentPoolName) -VMScaleSetName $(agentPoolName)
$groupedVmssInstancesByComputerName = @{}
foreach($vmssInstance in $vmssInstances) 
{
    $groupedVmssInstancesByComputerName[$vmssInstance.OsProfile.ComputerName] = @{vmmsInstance = $vmssInstance; agents = @(); isFullyUpdated = $false}
}
foreach($key in $groupedVmssInstancesByComputerName.Keys)
{
    $vmssInstance = $groupedVmssInstancesByComputerName[$key]["vmmsInstance"]
    Write-Host "Trying to reimage instance $($vmssInstance.InstanceId)..."
    $groupedVmssInstancesByComputerName[$key]["reimageOperation"] =   Set-AzVmssVM -Reimage -InstanceId $vmssInstance.InstanceId -ResourceGroupName $(agentPoolName) -VMScaleSetName $(agentPoolName) -AsJob
}
while($true) 
{
    Get-Job
    Start-Sleep -Seconds 10
}

I cant understand what is going on. Maybe i dont know some features of powershell. Please help me, guyes!

1

1 Answers

0
votes

Try using Wait-Job - Suppresses the command prompt until one or all of the PowerShell background jobs running in the session are completed.

Get-Job | Wait-Job

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/wait-job?view=powershell-6