Home > OS >  Start-Process inside Invoke-Command closes immediately unless -wait switch, but how can I continue s
Start-Process inside Invoke-Command closes immediately unless -wait switch, but how can I continue s

Time:11-29

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
    
     }
}

CodePudding user response:

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?

  • Related