【问题标题】:Get StdOut from RunCommand using Azure VM Javascript SDK使用 Azure VM Javascript SDK 从 RunCommand 获取 StdOut
【发布时间】:2019-01-14 22:18:35
【问题描述】:

我在我的 Node.js webApp 中使用Azure VM Javascript SDK。我正在尝试使用 RunCommand Function 在我的 Azure 虚拟机上运行自定义脚本。

我遇到的问题是通过运行应包含 StdOut 和 StdErr 字符串的命令获得响应。

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

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

然后我能够收到响应,例如(注意“消息”部分有一个从“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 运行此代码时,我没有得到任何返回。代码如下:

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);
    })


});

这是打印到控制台的内容:

null
{}

我知道脚本实际上正在运行,因为我尝试并成功地创建了一个文本文件 (myTxtFile.txt)。

有人知道为什么我在结果对象中没有得到任何东西吗?

编辑 1(回应 @Itay):

查看源代码,回调应该是“RunCommandResult”类型的“ServiceCallback”。下面是 RunCommand 的三个函数声明。

这是 ServiceCallback 接口的声明。

所以在我的回调中,我期望有四个返回“err”和“result”以及“request”和“response”(我显然忽略了后两个)。在我的示例中,我将错误和结果对象打印到控制台,但结果对象中没有任何内容...

【问题讨论】:

    标签: javascript node.js azure azure-virtual-machine runcommand


    【解决方案1】:

    我认为您定义的回调的签名与文档中描述的不同。

    查看runCommand(string, string, RunCommandInput, ServiceCallback< RunCommandResult >),回调似乎应该接受RunCommandResult interface,而RunCommandResult interface又包含一个名为Value的属性,它是一个InstanceViewStatus interface实例数组,可能包含您正在查看的信息为。

    希望对你有帮助!

    【讨论】:

    • 查看编辑 1 的更多详细信息...当我将 result 对象打印到控制台时,我只得到一个空对象 {} 回来,而我期待 RunCommandResult
    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多