VirtualMachines.beginRunCommandAndWait返回预配成功异常问题求助
Azure VM RunCommand 未识别PowerShell脚本执行错误的问题
调用VirtualMachines.beginRunCommandAndWait执行PowerShell脚本时,即便脚本执行异常且退出码大于1,组件始终返回“Provisioning succeeded”。从执行日志可见WinAppDriver安装失败(退出码1622),但预配状态仍显示成功。需明确如何让组件识别脚本执行错误,是否需要抛出异常,或是组件存在其他错误反馈机制。
执行日志
{ "code": "ComponentStatus/StdOut/succeeded", "level": "Info", "displayStatus": "Provisioning succeeded", "message": "Restart WinAppDriver\n" + "Transcript started, output file is C:\\Windows\\TEMP\\WinAppDriver.txt\n" + "Transcript stopped, output file is C:\\Windows\\TEMP\\WinAppDriver.txt\n" + "Transcript started, output file is C:\\Windows\\TEMP\\WinAppDriver.txt\n" + "Downloading WinAppDriver Installer from: https://test/WindowsApplicationDriver_1.2.1.msi\n" + "Download completed\n" + "Installing WinAppDriver\n" + "Installation Failed with Exit Code: 1622\n" + "Transcript stopped, output file is C:\\Windows\\TEMP\\WinAppDriver.txt" }
原PowerShell脚本
$Name = 'WinAppDriver' $Log = Join-Path $ENV:TEMP "$Name.txt" Start-Transcript -Append $Log try { $Dir = get-location $File = "WindowsApplicationDriver.msi" $TempFolder="temp" $Filepath="$Dir\$TempFolder\$File" # WinAppDriver Installation if (!(Test-Path $TempFolder)) { Write-Host "Creating temp folder at $TempFolder" New-Item -Name $TempFolder -Type Directory | Out-Null } Write-Host "Installing $Name" $Process = Start-Process -FilePath msiexec -ArgumentList "/i $Filepath /l*vx log.txt /qn /norestart" -PassThru -wait -NoNewWindow if ($Process.exitCode -ne 0) { throw "Installation Failed with Exit Code: $($Process.exitCode)" } Write-Host "Installation Completed with Exit Code: $($Process.exitCode)" exit $Process.exitCode } Finally{ Stop-Transcript }
尝试过的错误处理片段
片段1:直接输出错误并退出
Write-Host "error" exit $Process.exitCode
片段2:抛出异常后退出
throw "error" exit $Process.exitCode
问题原因及解决方法
原因
Azure VM RunCommand的默认逻辑仅检查脚本是否成功启动并完成执行,不会主动解析脚本内部的退出码或异常。当脚本抛出异常时,PowerShell的默认错误处理可能会将最终退出码重置为0,导致RunCommand认为执行成功。
解决方法
要让RunCommand识别脚本执行错误,需确保脚本的最终退出码不为0,且退出码能正确传递给RunCommand组件:
- 移除
throw语句:抛出异常会中断脚本流程,且可能被PowerShell环境捕获,导致退出码无法正确传递。直接使用exit命令返回错误码即可。 - 明确设置退出码:在安装失败时,直接调用
exit $Process.exitCode终止脚本,确保错误码被传递。 - 修改后的脚本示例:
$Name = 'WinAppDriver' $Log = Join-Path $ENV:TEMP "$Name.txt" Start-Transcript -Append $Log try { $Dir = Get-Location $File = "WindowsApplicationDriver.msi" $TempFolder = "temp" $Filepath = "$Dir\$TempFolder\$File" if (!(Test-Path $TempFolder)) { Write-Host "Creating temp folder at $TempFolder" New-Item -Name $TempFolder -Type Directory | Out-Null } Write-Host "Installing $Name" $Process = Start-Process -FilePath msiexec -ArgumentList "/i $Filepath /l*vx log.txt /qn /norestart" -PassThru -Wait -NoNewWindow if ($Process.ExitCode -ne 0) { Write-Host "Installation Failed with Exit Code: $($Process.ExitCode)" exit $Process.ExitCode } Write-Host "Installation Completed with Exit Code: $($Process.ExitCode)" exit $Process.ExitCode } finally { Stop-Transcript }
额外说明
如果必须使用异常处理,需确保异常被捕获后仍能返回非0退出码。可以在catch块中捕获异常,设置退出码后终止脚本:
try { # 安装逻辑 } catch { Write-Host "Error occurred: $_" exit 1 # 或使用具体错误码 } finally { Stop-Transcript }
内容的提问来源于stack exchange,提问作者Leonid Petrrov
相关产品推荐
相关产品推荐

