Invoke-AzVMRunCommand log output, error handling



一旦我们运行命令"Invoke-AzVMRunCommand"在远程虚拟机上执行PS脚本,即使它实际上失败了,它也总是成功的。我知道远程 VM 那里有日志文件:

"C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandWindows\1.1.3\Status">

问题:

但是如何在本地电源外壳上检索错误,try-catch等并没有显示出来。 使用"Invoke-AzVMRunCommand"的正确错误处理是什么,理想情况下得到.txt的结果,如下所示:

|输出文件 - 文件路径 xxx.txt

谢谢。

最终经过长时间的测试,我最终得到了这个解决方案,它从远程脚本执行中抛出错误并将其记录.txt文件中:

$result = Invoke-AzVMRunCommand -ErrorAction Stop -ResourceGroupName "MyRg" -Name "MyVM" -CommandId 'RunPowerShellScript' -ScriptPath MyScript.ps1
Remove-Item -path script.ps1 
if ($result.value.Message -like '*error*') 
{  
Write-Output "Failed. An error occurred: `n $($result.value.Message)" | Out-File -Filepath C:OutputLog.txt -Append
throw $($result.value.Message)        
}
else
{
Write-Output "Success" | Out-File -Filepath C:OutputLog.txt -Append
} 

最新更新