Support Forums

(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.
User avatar
noescape
Posts: 16
Joined: Wed Apr 22, 2015 4:57 am

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

Postby noescape » Mon Jan 18, 2016 7:01 am

Hi,

from within a jobscript, when I create a process using
New-Object System.Diagnostics.Process
and then start it, it works fine and the process I create (psexec) start within context of PS Studio:

-JobScript { `
param ($Station,
$Package,
$Version)
 
$installinfo = New-object System.Diagnostics.ProcessStartInfo
$installinfo.CreateNoWindow = $true
$installinfo.UseShellExecute = $false
$installinfo.RedirectStandardOutput = $true
$installinfo.RedirectStandardError = $true
$installinfo.FileName = "$env:TLS_STORE\sysinternals\psexec.exe"
$installinfo.Arguments = @("-accepteula -s \\$($station) cscript C:\GLOW\TLS\install.vbs /p:$Package /v:$Version")
$install = New-Object System.Diagnostics.Process
$install.StartInfo = $installinfo
[void]$install.Start()
$install.WaitForExit()
return $install,$Package,$Station
}


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:

-JobScript { `
param ($Station,
$Package,
$Version)
 
$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
return $install,$Package,$Station
}


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: 4354
Joined: Thu Aug 18, 2011 4:56 am

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

Postby 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:

$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)

Postby 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:

param ($Station,
$Package,
$Version)
$install = Invoke-Command -ComputerName $Station -ScriptBlock {
param ($Package,
$Version)
$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
return $install_exec.ExitCode
} -ArgumentList $Package, $Version
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

Return to “Windows PowerShell”

Who is online

Users browsing this forum: No registered users and 2 guests