Invoke-AzVMRunCommand has begun reporting an error

Jan Vávra 341 Reputation points
2024-11-05T13:20:50.8166667+00:00

I am creating a set of vms as copy of an original vm and after vm is created I run a remote command to rename vm. It has worked without any error notice but now ...


for (...)
{
        # 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 += $Job
        }

I am getting an error

 181 |              $Jobs += $Job
     |              ~~~~~~~~~~~~~
     | Method invocation failed because [Microsoft.Azure.Commands.Common.AzureLongRunningJob`1[[Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet,      
     | Microsoft.Azure.PowerShell.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]] does not contain a method named 'op_Addition'.    

At the end I have a join part

    foreach ($Job in $Jobs) {
        $Job | Wait-Job
        $Result = $Job | Receive-Job
        $Job.Host + ", " + $Job.Id + ", " + $Result.Value[0].Message
    }

with output

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Long Running O… AzureLongRunni… Completed     True            localhost            Invoke-AzVMRunCommand

So the created job was OK

I've just updated Az module to 12.4.0
and $PSVersionTable is

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
705 questions
{count} votes

Accepted answer
  1. anashetty 1,145 Reputation points Microsoft Vendor
    2024-11-08T14:37:50.67+00:00

    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.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. anashetty 1,145 Reputation points Microsoft Vendor
    2024-11-05T16:30:49.9733333+00:00

    Hi Jan Vávra,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    Based on the error details you shared, I have shared troubleshooting steps that I felt will help resolve the issue you reported.

    I think that $Jobs has not been initialized correctly as an array or collection. Before the loop starts, Initialize $Jobs as an array.

    # Initialize the Jobs array
    $Jobs = @()  #This initializes $Jobs as an empty array
    
    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 += $Job 
        }
    }
    
    
    # Wait for all jobs to complete and gather results
    foreach ($Job in $Jobs) {
        $Job | Wait-Job
        $Result = $Job | Receive-Job
        Write-Host $Job.Host + ", " + $Job.Id + ", " + $Result.Value[0].Message
    }
    

    Please use above PowerShell script, this might be helpful to you. If you still find any difficulties, please let me know I would like to work closer on this issue.

    Thank you.

    0 comments No comments

  2. Jan Vávra 341 Reputation points
    2024-11-08T13:54:12.3533333+00:00

    @anashetty You were right, the problem was in $Job.Host.ToString().
    I debugged the pwsh in VS Code and It's a pitty there is no info about on what machine the job ran.

    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
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.