1

I'm using the Azure VM Javascript SDK in my Node.js webApp. I'm trying to use the RunCommand Function to run a custom script on my Azure virtual machines.

The problem I'm having is with obtaining the response from running the command which should contain the StdOut and StdErr strings.

If I run the command from the Azure CLI, Like the following:

az vm run-command invoke -g 'myResource' -n 'myVm' --command-id RunPowerShellScript --scripts 'Get-ChildItem -Name'

Then I am able to receive the response, e.g. (notice in the 'message' section there is a listing of the files returned from 'Get-ChildItem')

{
  "value": [
    {
      "code": "ComponentStatus/StdOut/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "myTxtFile.txt\nscript12.ps1\nscript13.ps1\nscript14.ps1\nscript15.ps1\nscript16.ps1\nscript17.ps1\nscript18.ps1\nscript19.ps1\nscript20.ps1\nscript21.ps1\nscript22.ps1\nscript23.ps1\nscript24.ps1\nscript25.ps1\nscript26.ps1",
      "time": null
    },
    {
      "code": "ComponentStatus/StdErr/succeeded",
      "displayStatus": "Provisioning succeeded",
      "level": "Info",
      "message": "",
      "time": null
    }
  ]
}

However, when I run this code from the javascript SDK I don't get any thing returned. Here is the code:

let usr = ...;
let pas = ...;
let subscriptionId = ...;
let client = null;
msRestAzure.loginWithUsernamePassword(usr, pas, function(err, credentials) {
    if (err) return console.log(err);
    client = new azureArmCompute.ComputeManagementClient(credentials, subscriptionId);

    let param = {
        commandId: 'RunPowerShellScript',
        script: [
            'echo $null >> myTxtFile.txt\n' + 
            'Get-ChildItem -Name\n'
        ]
    };
    client.virtualMachines.runCommand('myResource', 'myVm', param, function (err, result) {
        console.log(err);
        console.log(result);
    })


});

and here is what is printed to the console:

null
{}

I know that the script is actually running because I tried, and was successfully able, to create a text file (myTxtFile.txt).

Anyone have any clues as to why I'm not getting anything in the result object?

Edit 1 (in response to @Itay):

Looking at the source, the callback is supposed to be a "ServiceCallback" of type "RunCommandResult". Here are the three function declarations for RunCommand. RunCommand Function Declarations

Here's the declaration for the ServiceCallback interface. ServiceCallback interface

So in my callback I was expecting there to be four returns "err" and "result" and "request" and "response" (I'm obviously leaving off the latter two). In my example I'm printing the error and result objects to the console, but the result object doesn't have anything in it...

Stanton
  • 904
  • 10
  • 25

1 Answers1

0

I think the signature for the callback you defined is not as described by the documentation.

Looking at runCommand(string, string, RunCommandInput, ServiceCallback< RunCommandResult >), it seems that the callback should be accepting a RunCommandResult interface, which in turn, contains a property called Value which is an array of InstanceViewStatus interface instances that might hold the information you are looking for.

Hope it helps!

Itay Podhajcer
  • 2,616
  • 2
  • 9
  • 14
  • See edit 1 with some more details... When I print the `result` object to the console I only get an empty object `{}` back, when I was expecting the RunCommandResult – Stanton Jan 14 '19 at 14:21