我可以 return 从 WSL Bash 到 Powershell 或命令提示符的退出代码 %errorlevel% 吗?
Can I return an exit code %errorlevel% from WSL Bash to Powershell or Command Prompt?
我有一个 bash 脚本,可以根据条件 return 退出 0 或退出 1。 bash 脚本是从执行远程处理的 Windows Powershell 调用的。我在命令提示符下测试了 %errorlevel% 但它没有 return 正确的值!
远程执行。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
我有一个 bash 脚本,可以根据条件 return 退出 0 或退出 1。 bash 脚本是从执行远程处理的 Windows Powershell 调用的。我在命令提示符下测试了 %errorlevel% 但它没有 return 正确的值!
远程执行。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