Powershell 2.0 如何从远程计算机上运行的脚本将退出代码返回到本地计算机上的变量?

Powershell 2.0 如何从远程计算机上运行的脚本将退出代码返回到本地计算机上的变量?,powershell-2.0,Powershell 2.0,我正在尝试使用Powershell 2.0从本地计算机上的Powershell窗口在远程计算机上执行Powershell脚本。我的目标是将退出代码从远程计算机上的脚本存储到本地计算机上的变量,无论它是在本地脚本中还是仅在本地Powershell窗口中。目前我得到的只是一条错误消息,声明如下 Cannot invoke pipeline because run space is not in the Opened state. Current state of run space is Close

我正在尝试使用Powershell 2.0从本地计算机上的Powershell窗口在远程计算机上执行Powershell脚本。我的目标是将退出代码从远程计算机上的脚本存储到本地计算机上的变量,无论它是在本地脚本中还是仅在本地Powershell窗口中。目前我得到的只是一条错误消息,声明如下

Cannot invoke pipeline because run space is not in the Opened state. Current state of run space is Closed. + CategoryInfo: OperationStopped: Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], InvalidRunspaceStateException + FullyQualifiedErrorID : RemotePipeLineExecutionFailed
$Remote=New-PSSession -ComputerName 7.7.7.7 -Credential $cred

Invoke-Command -Session $Remote -ScriptBlock {$Code1=.\VerifyBatchExistence.ps1 7777 2 .\Batch.ps1 NAME1 NAME2}

$Code2=Invoke-Command -Session $Remote -ScriptBlock {$Code1}
$Code2
我的情况是。我试图从远程计算机上的脚本向本地计算机上的变量返回退出代码。这里实际上涉及到两个脚本,它们都在远程计算机上和同一文件夹中。第一个脚本称为“VerifyBatchExistence.ps1”。此文件将名为“batch.ps1”的批处理文件作为输入,该整数表示“batch.ps1”不存在时要输出的退出代码,另一个整数表示“batch.ps1”所需的参数数量,以及“batch.ps1”输入所需的变量字符串。在“Batch.ps1”中,我有一段代码,我希望在传入的参数之一无效时触发它

exit 931
在“VerifyBatchExistence.ps1”中,我有以下一段代码

Powershell - File .\Batch.ps1 BATCHPARAM1 BATCHPARAM2

$Code1 = $LASTEXITCODE

$Host.SetShouldExit($Code1)

exit
在我的本地机器上,我做了以下操作

Cannot invoke pipeline because run space is not in the Opened state. Current state of run space is Closed. + CategoryInfo: OperationStopped: Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], InvalidRunspaceStateException + FullyQualifiedErrorID : RemotePipeLineExecutionFailed
$Remote=New-PSSession -ComputerName 7.7.7.7 -Credential $cred

Invoke-Command -Session $Remote -ScriptBlock {$Code1=.\VerifyBatchExistence.ps1 7777 2 .\Batch.ps1 NAME1 NAME2}

$Code2=Invoke-Command -Session $Remote -ScriptBlock {$Code1}
$Code2
在此之后,我收到上述错误。你知道我做错了什么,或者我如何从远程机器返回一个退出代码给本地机器上的一个变量,这个远程机器上有一个脚本正在使用并运行另一个脚本吗?对此的任何答复都将不胜感激。谢谢。

我是这样做的:

invoke-command -session $Remote -scriptblock { mycommand }
$remotelastexitcode = invoke-command -session $Remote -ScriptBlock { $lastexitcode }

if ( $remotelastexitcode -ne 0 )
{
    "$remotelastexitcode"
    exit 1
}