如何解决我可以将退出代码 %errorlevel% 从 WSL Bash 返回到 Powershell 或命令提示符吗?
我有一个 bash 脚本,它根据条件返回 exit 0 或 exit 1。该 bash 脚本是从执行远程处理的 Windows Powershell 调用的。我在命令提示符下测试了 %errorlevel%,但它没有返回正确的值!
远程执行.ps1
param (
[Parameter(Mandatory=$true,HelpMessage="Remote computer")]
[String]$computer,[Parameter(Mandatory=$true,HelpMessage="Command to be executed on the remote computer")]
[String]$command,)
$message = "Executing: '" + $command + "' on remote computer " + $computer
Write-Host $message
$session = New-PSSession -ComputerName $computer
Invoke-Command -Session $session -ScriptBlock ([scriptblock]::Create($command))
$returncode=Invoke-Command -Session $session -ScriptBlock { $? }
Remove-PSSession $session
if ($returncode -eq $true) {
return 0
exit 0
}
else {
return 1
exit 1
}
}
powershell -file RemoteExecute.ps1 -computer "myServer" -command "bash -c './somescript.sh'"
Executing: bash -c './somescript.sh' on remote computer myServer
ERROR. Aborted!
exit 1
C:\Windows\system32>echo %errorlevel%
0
解决方法
解决了。 刚刚换了$?到 $lastexitcode 并返回该变量
$returncode=Invoke-Command -Session $session -ScriptBlock { $lastexitcode }
Remove-PSSession $session
exit $returncode
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。