简体   繁体   中英

Start-Process inside Invoke-Command closes immediately unless -wait switch, but how can I continue script?

I'm trying to remotely run this Windows Update Assistance Installer.exe and I notice that the.exe closes immediately unless I use the -wait command. However, if I use the -wait command I can't continue my foreach loop for the other computers since it takes hours for an install to finish. If I take the -wait command out, I think it launches then closes immediately.

$computers | % {
 {more code...}

    Invoke-Command -Session $Session -ScriptBlock {
    
    $msbuild = "C:\windows\temp\Windows10Upgrade9252(21H2).exe"
    $Args = '/quietinstall /skipeula /auto upgrade /copylogs'
    Start-Process -FilePath $msbuild -ArgumentList $args -Wait
    
     }
}

Run in parallel (within -throttlelimit) with $computers or $sessions as the computername/sessionname. Can also use -asjob. $args might be reserved already. Start-process won't return text output or the exit code without further action, which I've added. You might use scheduled tasks instead.

$sessions = new-pssession $computers 
Invoke-Command $sessions {
  $msbuild = "C:\windows\temp\Windows10Upgrade9252(21H2).exe"
  $myargs = '/quietinstall /skipeula /auto upgrade /copylogs'
  $p = start-process -wait $msbuild $myargs -passthru
  [pscustomobject]@{exitcode=$p.exitcode]
} # -asjob


exitcode PSComputerName RunspaceId
-------- -------------- ----------
       0 localhost      197d26f5-754b-49d3-baf4-2ca8fccacd4c

Other ways to wait for a program to finish: How to tell PowerShell to wait for each command to end before starting the next?

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