使用Azure VM Javascript SDK从RunCommand获取StdOut [英] Get StdOut from RunCommand using Azure VM Javascript SDK

查看:116
本文介绍了使用Azure VM Javascript SDK从RunCommand获取StdOut的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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.

我遇到的问题是从运行应包含StdOut和StdErr字符串的命令获取响应.

如果我从Azure CLI运行命令,则如下所示:

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'

然后我可以收到回复,例如(请注意,在消息"部分中列出了从"Get-ChildItem"返回的文件)

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
    }
  ]
}

但是,当我从javascript SDK运行此代码时,我没有得到任何回报.这是代码:

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
{}

我知道该脚本实际上正在运行,因为我尝试并且能够创建文本文件(myTxtFile.txt).

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?

编辑1(以回复@Itay):

从源头来看,回调应该是类型为"RunCommandResult"的"ServiceCallback".这是RunCommand的三个函数声明.

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

这是ServiceCallback接口的声明.

Here's the declaration for the ServiceCallback interface.

因此,在我的回调中,我期望有四个返回值"err","result","request"和"response"(我显然省去了后两个).在我的示例中,我将错误和结果对象打印到控制台,但是结果对象中没有任何内容...

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...

推荐答案

我认为您定义的回调的签名与文档中所描述的不一致.

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

查看 RunCommandResult接口,其中依次包含一个名为Value的属性,该属性是一个

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.

希望有帮助!

这篇关于使用Azure VM Javascript SDK从RunCommand获取StdOut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆