简体   繁体   中英

Invoke-AzVMRunCommand log output, error handling

Once we run command "Invoke-AzVMRunCommand" to execute a PS script on a remote VM, it always succeeds even it actually fails. I know remote VM has the log file there:

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

The problem:

But how to retrieve the error on a local powershell, try-catch etc. does not show it. What is a proper error handling using "Invoke-AzVMRunCommand", ideally getting results in .txt, something like:

| Out-File -Filepath xxx.txt

thank you.

Eventually after long testing, I end up with this solution, which throws an error from remote script execution and logs it in .txt file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM