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

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Locked
User avatar
noescape
Posts: 16
Joined: Wed Apr 22, 2015 4:57 am

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

Post by noescape » Mon Jan 18, 2016 7:01 am

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.  
  3.             param ($Station,
  4.  
  5.                 $Package,
  6.  
  7.                 $Version)
  8.  
  9.                        
  10.  
  11.             $installinfo = New-object System.Diagnostics.ProcessStartInfo
  12.  
  13.             $installinfo.CreateNoWindow = $true
  14.  
  15.             $installinfo.UseShellExecute = $false
  16.  
  17.             $installinfo.RedirectStandardOutput = $true
  18.  
  19.             $installinfo.RedirectStandardError = $true
  20.  
  21.             $installinfo.FileName = "$env:TLS_STORE\sysinternals\psexec.exe"
  22.  
  23.             $installinfo.Arguments = @("-accepteula -s \\$($station) cscript C:\GLOW\TLS\install.vbs /p:$Package /v:$Version")
  24.  
  25.             $install = New-Object System.Diagnostics.Process
  26.  
  27.             $install.StartInfo = $installinfo
  28.  
  29.             [void]$install.Start()
  30.  
  31.             $install.WaitForExit()
  32.  
  33.                         return $install,$Package,$Station          
  34.  
  35.         }
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.  
  3.             param ($Station,
  4.  
  5.                 $Package,
  6.  
  7.                 $Version)
  8.  
  9.  
  10.  
  11. $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
  12.  
  13. return $install,$Package,$Station
  14.  
  15. }
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: 4981
Joined: Thu Aug 18, 2011 4:56 am

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

Post by davidc » Tue Jan 19, 2016 9:53 am

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
Joined: Wed Apr 22, 2015 4:57 am

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

Post by noescape » Wed Jan 20, 2016 1:02 am

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.  
  3.                 $Package,
  4.  
  5.                 $Version)
  6.  
  7. $install = Invoke-Command -ComputerName $Station -ScriptBlock {
  8.  
  9.                     param ($Package,
  10.  
  11.                         $Version)
  12.  
  13.                     $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
  14.  
  15.                     return $install_exec.ExitCode
  16.  
  17.                 } -ArgumentList $Package, $Version
  18.  
  19.           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

Locked