Page 1 of 1

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

Posted: Mon Jan 18, 2016 6:01 am
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

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

Posted: Tue Jan 19, 2016 8:53 am
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

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

Posted: Wed Jan 20, 2016 12:02 am
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