简体   繁体   中英

Powershell Invoke-Command remotely with -AsJob failed to launch remote program

I want to launch a long-running remote program but don't want to wait. So I put below one line command in PS script file and run "Powershell -file xxx.ps1"

Invoke-command server1 {xxx.exe} -AsJob

But the remote program is not running after the script run, but if I run above command in PS console interactively, remote program running ok. Seems the program is killed when script terminates.

Why is this?

UPDATE

If I sleep 1 second at end of script, the remote app running fine.

The best way around this is probably to use Wait-Job . It will wait for the job you launched until it's done. Otherwise the script just closes, probably before the connection has already been made completely.

If you want to wait for all jobs running in the background to be completed, you can use:

Invoke-command server1 {xxx.exe} -AsJob
Get-Job | Wait-Job

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