Get StdOut from RunCommand using Azure VM Javascript SDK

别说谁变了你拦得住时间么 提交于 2019-12-13 01:10:59

问题


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.

Here's the declaration for the 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...


回答1:


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!



来源:https://stackoverflow.com/questions/54152364/get-stdout-from-runcommand-using-azure-vm-javascript-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!