Hi Jan Vávra,
I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.
Issue:
Invoke-AzVMRunCommand has begun reporting an error
Solution:
the problem was in $Job.Host.ToString(). So, I modified my code as follows.
$Jobs = [ordered]@{} # Initialize a hashtable
for (...) { #Your existing loop condition
# Run a script string to rename a VM and restart it
if ($null -ne $vm) {
Write-Host "Renaming COMPUTER NAME in vm $vmName"
$Job = Invoke-AzVMRunCommand -ResourceGroupName $targetRgName -Name $vmName -CommandId "RunPowerShellScript" -ScriptString ("Rename-Computer -NewName '" + $vmName + "' -Force; Restart-Computer") -AsJob
$Jobs[$vmName] = $Job
}
}
foreach ($vmName in $Jobs.Keys) {
$Job = $Jobs[$vmName] | Wait-Job
$Result = $Job | Receive-Job
$joinedMessages = $Result.Value | Where-Object { $null -ne $_.Message -and $_.Message -ne "" } | ForEach-Object { $_.Message } | Join-String -Separator "`n"
"*** $vmName ***"
"JobId: $($Job.Id), result: $($Result.Status)"
$joinedMessages
}
Thank you again for your time and patience throughout this issue. Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.
Thank you.