简体   繁体   中英

Use PowerShell Invoke-Command cannot get any result

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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