(Start-Process) vs. (New-Object System.Diagnostics.Process)

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

(Start-Process) vs. (New-Object System.Diagnostics.Process)

Post by noescape »

Hi,

from within a jobscript, when I create a process using
  1. New-Object System.Diagnostics.Process
and then start it, it works fine and the process I create (psexec) start within context of PS Studio:
  1. -JobScript { `
  2.             param ($Station,
  3.                 $Package,
  4.                 $Version)
  5.                        
  6.             $installinfo = New-object System.Diagnostics.ProcessStartInfo
  7.             $installinfo.CreateNoWindow = $true
  8.             $installinfo.UseShellExecute = $false
  9.             $installinfo.RedirectStandardOutput = $true
  10.             $installinfo.RedirectStandardError = $true
  11.             $installinfo.FileName = "$env:TLS_STORE\sysinternals\psexec.exe"
  12.             $installinfo.Arguments = @("-accepteula -s \\$($station) cscript C:\GLOW\TLS\install.vbs /p:$Package /v:$Version")
  13.             $install = New-Object System.Diagnostics.Process
  14.             $install.StartInfo = $installinfo
  15.             [void]$install.Start()
  16.             $install.WaitForExit()
  17.                         return $install,$Package,$Station          
  18.         }
BUT when I start a process using start-process, PSEXEC starts out of PSStudio context and thus the job ends without waiting for the actual psexec process to end:
  1. -JobScript { `
  2.             param ($Station,
  3.                 $Package,
  4.                 $Version)
  5.  
  6. $install = Start-Process "$env:TLS_STORE\sysinternals\psexec.exe" -ArgumentList "-accepteula -s \\$($Station) cscript C:\GLOW\TLS\install.vbs /p:$Package /v:$Version" -Wait -NoNewWindow -PassThru
  7. return $install,$Package,$Station
  8. }
Is this behavior intended? Why does start-process create the psexec process thread out of PSStudio context?
Thanks for insight.

Thomas
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: (Start-Process) vs. (New-Object System.Diagnostics.Process)

Post by davidc »

We have had users encounter issues with PowerShell where the Start-Process cmdlet will fail to wait for the process before returning despite using the Wait parameter.

Try adding the following line to your script after calling Start-Process:
  1. $install.WaitForExit()
I'm not sure if this is an issue with older version of PowerShell or not. What version of PowerShell do you have installed?

David
David
SAPIEN Technologies, Inc.
User avatar
noescape
Posts: 16
Last visit: Tue Jan 04, 2022 2:17 am

Re: (Start-Process) vs. (New-Object System.Diagnostics.Process)

Post by noescape »

Hi David,

I tried adding $install.WaitForExit() with no success. We are using PowerShell v4.
Maybe the problem isn't with the Wait parameter, but because the PsExec process starts it's thread out of PowerShell Studio process tree, unlike (New-Object System.Diagnostics.Process) which creates the thread inside of it.

In the end I have chosen a different approach with executing PsExec on the remote station with invoke-command:
  1. param ($Station,
  2.                 $Package,
  3.                 $Version)
  4. $install = Invoke-Command -ComputerName $Station -ScriptBlock {
  5.                     param ($Package,
  6.                         $Version)
  7.                     $install_exec = Start-Process -FilePath "$env:TLS_STORE\sysinternals\psexec.exe" -ArgumentList "-accepteula -s cscript C:\GLOW\TLS\install.vbs /p:$Package /v:$Version" -Wait -NoNewWindow -PassThru
  8.                     return $install_exec.ExitCode
  9.                 } -ArgumentList $Package, $Version
  10.           return $install,$Package,$Station
This is actually even better since you don't have to wait for PsExeSvc service to establish connection. We have Sysinternals on all our computers so there's no risk of running non-existant PsExec.

Regards,

Tom
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked