L'applet de commande Powershell Start-Process
est agit bizarrement:
Quand je lance un autre processus de la console et je précise -NoNewWindow
, la propriété ExitCode (un int
!) est nul.
Voici un test: Même chose avec autre chose que cmd
. Ce test a été sur un Win10 avec PS5, il est aussi la même chose avec Win7 et PS5:
PS C:\Users\Martin> cmd.exe /Cver
Microsoft Windows [Version 10.0.15063]
PS C:\Users\Martin> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.15063.296
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.296
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\Users\Martin> $pNewWindow = Start-Process -FilePath "cmd.exe" -ArgumentList '/C"exit 42"' -PassThru
PS C:\Users\Martin> $pNewWindow.WaitForExit()
PS C:\Users\Martin> $pNoNewWindow.HasExited
True
PS C:\Users\Martin> $pNewWindow.ExitCode
42
PS C:\Users\Martin> $pNoNewWindow = Start-Process -FilePath "cmd.exe" -ArgumentList '/C"exit 42"' -PassThru -NoNewWindow
PS C:\Users\Martin> $pNoNewWindow.WaitForExit()
PS C:\Users\Martin> $pNoNewWindow.HasExited
True
PS C:\Users\Martin> $pNoNewWindow.ExitCode
PS C:\Users\Martin> $pNoNewWindow.ExitCode -eq $null
True
PS C:\Users\Martin> $pNoNewWindow | Get-Member | ? {$_.Name -imatch "exit"}
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Exited Event System.EventHandler Exited(System.Object, System.EventArgs)
WaitForExit Method bool WaitForExit(int milliseconds), void WaitForExit()
ExitCode Property int ExitCode {get;}
ExitTime Property datetime ExitTime {get;}
HasExited Property bool HasExited {get;}
PS C:\Users\Martin>
... Ainsi, la propriété est là, mais il est null
, même si elle est un int
?
http://stackoverflow.com/questions/10262231/obtaining-exitcode-using-start-process-and-waitforexit-instead-of-wait –
N'est-ce pas un doublon alors? – Matt
@Matt - Je ne voudrais pas nécessairement dupe mais il est certainement proche. Ne hésitez pas à voter si dupeing il ajoute de la valeur pour vous –