Any way to poll for the status of an ongoing Azure VM Run Command running on a VM in Powershell?

Ryan Wong 21 Reputation points

I am currently using the Invoke-AzVMRunCommand with the 'RunPowerShellScript' CommandId to execute a run command extension for executing a powershell script on an Azure VM. I'm aware that only one run command extension execution can run at a time on a given Azure VM per the documentation. For the case that we have multiple jobs running in parallel and are invoking a run command against the same target VM in each of these jobs, I was wondering if there is there any way to get or query the status of a run command that may be currently executing on the VM? This is so that we can wait for the completion of any ongoing executions before attempting to call Invoke-AzVMRunCommand again against the same VM?

I found documentation on getting the "execution status and results" for a run command given you provide a RunCommandName, however I'm not sure how to name the run command in the Invoke-AzVMRunCommand cmdlet and haven't had any luck finding further documentation online. I would greatly appreciate further guidance on how to persist the run command to the virtual machine under the resource group in Azure or how I can get the current executing run command extension on a VM by means other than the "Get-AzVMRunCommand" cmdlet.

Thanks in advance for your assistance and let me know if you require more details!

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,514 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 103.2K Reputation points MVP

    Hi @Ryan Wong ,

    if you use Invoke-AzVMRunCommand it will be recorded in the Activity Log of the VM.
    There should be 3 events in the 'Activity Log' per RunCommand with Status Accepted, Started and Succeeded (or Failed if Invoke-AzVMRunCommand is not able to execute the RunCommand).

    You can get the ActivityLog details for the VM with this PowerShell script:

    $vm = "TestVM01"  
    $activityLog = Get-AzLog -ResourceId "$($(Get-AzVM -Name "$vm").Id)" -MaxRecord 50  
    $activityLog | Where-Object { $_.OperationName -eq "Run Command on Virtual Machine" } |  
        Sort-Object -Property SubmissionTimestamp, CorrelationId -Descending |  
            Select-Object OperationName, EventName, CorrelationId, SubmissionTimestamp, Status |  
                Format-Table -AutoSize  

    The result should look like this:

    Run Command on Virtual Machine End request    3bdf8654-38e8-449a-9a2a-2d5e4f6dcbba 30.08.2022 08:03:20 Succeeded  
    Run Command on Virtual Machine Begin request 3bdf8654-38e8-449a-9a2a-2d5e4f6dcbba 30.08.2022 08:02:28 Started  
    Run Command on Virtual Machine End request    3bdf8654-38e8-449a-9a2a-2d5e4f6dcbba 30.08.2022 08:02:28 Accepted  

    If the Succeeded line is missing but you get a Accepted and Started line the RunCommand should be still in progress. The CorrelationId is the reference ID of each RunCommand.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful