REST API run command on virtual machine

Benjamin Balanger 20 Reputation points
2023-06-21T02:33:11.72+00:00

Hello,

I am trying to run a script on my vm using the rest api https://learn.microsoft.com/en-us/rest/api/compute/virtual-machines/run-command?tabs=HTTP

the form looks like this :

{:location=>"West Europe", :properties=>{:source=>{:script=>"uname -a'"} }

I get a response saying it succeeded, but I can't find how to get the output. I am missing something?

Thank you.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,446 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
{count} votes

Accepted answer
  1. navba-MSFT 27,540 Reputation points Microsoft Employee Moderator
    2023-06-21T03:20:06.05+00:00

    @Benjamin Balanger Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I see that you are trying to invoke the VM RunCommand REST API and want to see the output response returned.

    Please note that the output response for the above REST API would look like below:
    User's image

    Note that the status code is 202, which means Accepted. This being an ASYNC operation it will send a Location response header. This Location header url needs to be invoked again in subsequent request inorder to get the desired output. And this is by-design.

    So to simplify this process, the easiest way is to run the Invoke-AzVMRunCommand PowerShell cmdlet, this makes the same above REST API call underneath. Also this will query the Location header in subsequent request the background and will eventually display the desired output. See the below sample script and its corresponding output:

    PS script:

    Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter @{param1 = "var1"; param2 = "var2"}

    Response received:
    See the Message value in the response for the desired output from the script.

    Value[0]        : 
      Code          : ComponentStatus/StdOut/succeeded
      Level         : Info
      DisplayStatus : Provisioning succeeded
      Message       : 
    hello World response from my sample script
    From Sample Script..
    Value[1]        : 
      Code          : ComponentStatus/StdErr/succeeded
      Level         : Info
      DisplayStatus : Provisioning succeeded
      Message       : 
    Status          : Succeeded
    Capacity        : 0
    Count           : 0
    

    Hope this helps.

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.