简体   繁体   English

使用PowerShell Invoke-Command无法获得任何结果

[英]Use PowerShell Invoke-Command cannot get any result

PowerShell 5.1 PowerShell 5.1

Invoke-Command -ScriptBlock {start-job {gsv}} -ComputerName Will
Invoke-Command -ScriptBlock {get-job} -ComputerName Will

Nothing was displayed. 什么也没显示。 I couldn't retrieve any job. 我找不到任何工作。

Jobs are tied to the current session. 作业与当前会话相关。 The way you run Invoke-Command creates a new session with every invocation, so the second session naturally doesn't know anything about jobs of the first one. 运行Invoke-Command的方式会在每次调用时创建一个新会话,因此第二个会话自然对第一个会话一无所知。

Change your code to something like this, so both invocations run in the same session: 将您的代码更改为类似的内容,以便两个调用都在同一会话中运行:

$session = New-PSSession -ComputerName Will
Invoke-Command -Session $session -ScriptBlock { Start-Job {gsv} }
Invoke-Command -Session $session -ScriptBlock { Get-Job }
Remove-PSSession $session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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