【问题标题】:How can i get the output of a CustomScriptExtenstion when using Azure Resource Manager?使用 Azure 资源管理器时如何获取 CustomScriptExtenstion 的输出?
【发布时间】:2016-07-01 19:38:26
【问题描述】:

我正在使用 Azure PowerShell Runbook 在 Azure 虚拟机上执行 PowerShell 脚本。当我使用 Azure 资源管理器功能时,我找不到获取远程脚本输出的方法,我必须将其用于我的部署。有很多使用“非资源管理器”方式的示例,如下所示:

# Execute remote script
$Vm = Get-AzureVM -ServiceName "DSCDemo" -Name "DSCPull"
Set-AzureVMCustomScriptExtension -ContainerName scripts -StorageAccountName psmag -FileName user.ps1 -Run user.ps1 -VM $vm | Update-AzureVM -Verbose
# Get output
$vm = Get-AzureVM -ServiceName DSCDemo -Name DSCPull
$output = $Vm.ResourceExtensionStatusList.ExtensionSettingStatus

然后 $output 变量包含已执行脚本的标准和错误输出。对于我的资源管理器版本,相同的代码看起来非常相似:

# Execute remote script
$vm = Get-AzureRmVM -Name "DSCPull" -ResourceGroupName $ResourceGroupName
$result = Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroupName `
                                                -VMName "DSCPull"  `
                                                -Name 'user' `
                                                -Location $vm.Location  `
                                                -StorageAccountName psmag  `
                                                -StorageAccountKey '<key>' `
                                                -FileName "user.ps1"  `
                                                -ContainerName "scripts"  `
                                                -RunFile "user.ps1"
$output = Get-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName -Status

但输出完全不同,我确实找到了包含标准输出或错误输出的任何内容。

如何借助 Azure 资源管理器功能检索输出?

【问题讨论】:

    标签: powershell azure azure-resource-manager resourcemanager


    【解决方案1】:

    好的,我找到了答案!您始终可以借助 Get-AzureRmVmDiagnosticExtension 命令查询结果:

    $output = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $ResourceGroupName -VMName 'DSCPull' -Name 'user' -Status
    $output.SubStatuses[0]
    $output.SubStatuses[1]
    

    会返回类似的东西

    Code          : ComponentStatus/StdOut/succeeded
    Level         : Info
    DisplayStatus : Provisioning succeeded
    Message       : my output on remote
    Time          :
    
    
    Code          : ComponentStatus/StdErr/succeeded
    Level         : Info
    DisplayStatus : Provisioning succeeded
    Message       :
    Time          :
    

    【讨论】:

    • 只是为了添加您的答案并为可能面临此问题的其他人节省一些时间,请注意,此 cmdlet 在从 powershell 终端运行时可以正常工作,但如果您从自动化运行手册运行它“工作流”类型的,这不会返回预期的输出。要获取输出,您需要将其包装在 InlineScript{} 块中并从该块返回输出。详情请查看网址amoghnatu.net/2018/04/15/…
    【解决方案2】:

    在我的测试中,也可以使用Get-AzureRmVMExtension 检索它,这可以说是更合乎逻辑的使用方法。您必须包含 -Status 参数,否则您无法取回状态和子状态值。

    此外,如果在资源管理器模板的 输出部分 中检索它,类似这样的方法可以工作(尽管我不喜欢硬编码的零索引):

    "outputs": {
        "foo": {
            "type": "string",
            "value": "[reference('Microsoft.Compute/virtualMachines/my-vm/extensions/my-script').instanceView.substatuses[0].message)]"
        }
    }

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多