问题
$app = Start-Process notepad -Passthru
Wait-Process $app.Id
This method returns the wrong PID. I try to stop the process right after the process is started using the PID and get the following error:
10424 Stop-Process : Cannot find a process with the process identifier 10424. At F:{{Redacted path}}\Dev_WAS_V85_All_Open.ps1:8 char:13 + Stop-Process <<<< $app.Id + CategoryInfo : ObjectNotFound: (10424:Int32) [Stop-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.StopProcessCommand
Added code below.
$pid_id_array = @()
$pid_id_array_index = 0
Set-Location F:\{{Redacted path}}\scripts\windows\BlueZone
Start-Process _clip.bat
$app = Start-Process {{Redacted part of the file name}}_server1.bat -passthru
Wait-Process $app.Id
write-host $app.Id
Stop-Process $app.Id
$app = Start-Process {{Redacted part of the file name}}_server1_FTP.bat -passthru
Wait-Process $app.Id
write-host $app.Id
Stop-Process $app.Id
$app = Start-Process {{Redacted part of the file name}}_server1_DMgr_FTP.bat -passthru
Wait-Process $app.Id
write-host $app.Id
Stop-Process $app.Id
$app = Start-Process {{Redacted part of the file name}}_server1_ISC.url -passthru
Wait-Process $app.Id
write-host $app.Id
Stop-Process $app.Id
$app = Start-Process {{Redacted part of the file name}}_CLI_strings.docx -passthru
Wait-Process $app.Id
write-host $app.Id
Stop-Process $app.Id
$app = Start-Process {{Redacted part of the file name}}_DMgr_CLI_strings.docx -passthru
Wait-Process $app.Id
write-host $app.Id
Stop-Process $app.Id
exit
来源:https://stackoverflow.com/questions/45149718/start-process-passthru-does-not-return-the-correct-pid