Powershell 使用Azure资源管理器时,如何获取CustomScriptExtenstion的输出?

Powershell 使用Azure资源管理器时,如何获取CustomScriptExtenstion的输出?,powershell,azure,azure-resource-manager,resourcemanager,Powershell,Azure,Azure Resource Manager,Resourcemanager,我正在使用Azure PowerShell Runbook在Azure虚拟机上执行PowerShell脚本。在使用Azure资源管理器功能时,我找不到获取远程脚本输出的方法,我必须使用这些功能进行部署。有很多使用“非资源管理器”方式的示例,如下所示: # Execute remote script $Vm = Get-AzureVM -ServiceName "DSCDemo" -Name "DSCPull" Set-AzureVMCustomScriptExtension -Container

我正在使用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
#执行远程脚本
$vm=获取AzureRmVM-Name“DSCPull”-ResourceGroupName$ResourceGroupName
$result=设置AzureRmVMCustomScriptExtension-ResourceGroupName$ResourceGroupName`
-VMName“DSCPull”`
-名称“用户”`
-Location$vm.Location`
-StorageAccountName psmag`
-StorageAccountKey“”`
-文件名“user.ps1”`
-容器名称“脚本”`
-运行文件“user.ps1”
$output=Get AzureRmVM-Name$VMName-ResourceGroupName$ResourceGroupName-Status
但是输出是完全不同的,我确实找到了任何包含标准输出或错误输出的东西


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

好的,我找到了答案!您始终可以使用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          :

在我的测试中,还可以使用检索,这可以说是更符合逻辑的方法。您必须包含
-Status
参数,否则无法返回Status和substatus值

另外,如果在资源管理器模板的输出部分中检索它,类似的方法也会起作用(尽管我不喜欢硬编码的零索引):

“输出”:{
“foo”:{
“类型”:“字符串”,
“值”:“[引用('Microsoft.Compute/virtualMachines/my vm/extensions/my script')。实例视图。子状态[0]。消息)]”
}

}
为了补充您的答案并为可能面临此问题的其他人节省一些时间,请注意,此cmdlet在从powershell终端运行时可以正常工作,但如果您是从类型为“Workflow”的自动化runbook运行,则不会返回预期的输出。要获得输出,需要将其包装在InlineScript{}块中,并从该块返回输出。查看URL以了解详细信息