How to get the PowerShell script shutdown code?
2 answers
If this is a command \ script that does not have an exxitCode parameter, then you can get the value of the $LastExitCode variable
PS C:\> ping 8.8.8.8 -c 1 Bad option -c. Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS] [-r count] [-s count] [[-j host-list] | [-k host-list]] [-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name Options: -t Ping the specified host until stopped. To see statistics and continue - type Control-Break; To stop - type Control-C. -a Resolve addresses to hostnames. -n count Number of echo requests to send. -l size Send buffer size. -f Set Don't Fragment flag in packet (IPv4-only). -i TTL Time To Live. -v TOS Type Of Service (IPv4-only. This setting has been deprecated and has no effect on the type of service field in the IP Header). -r count Record route for count hops (IPv4-only). -s count Timestamp for count hops (IPv4-only). -j host-list Loose source route along host-list (IPv4-only). -k host-list Strict source route along host-list (IPv4-only). -w timeout Timeout in milliseconds to wait for each reply. -R Use routing header to test reverse route also (IPv6-only). -S srcaddr Source address to use. -4 Force using IPv4. -6 Force using IPv6. PS C:> $LastExitCode 1 PS C:\> $s = ping 8.8.8.8 PS C:\> $LastExitCode 0 If it has a parameter, then $s.exitCode()
in more detail you can read here
UPD
function StartProcess{ Param ( [String] $path, [String] $arguments = $null, [bool] $wait = $true, ) $exec = "Start-Process `"$path`" -PassThru " $exec = $exec + " -NoNewWindow:`$true " #Start-Process -ArgumentList if($arguments -ne $null){$exec = $exec + " -ArgumentList '" + $arguments + "'"} if($wait -eq $true){$exec = $exec + " -wait"} $p = Invoke-Expression $exec return $p } Now you will check that $myP = StartProcess .....
if( $myP.ExitCode -eq 0){ And for other scripts
StartProcess -path "poershell" "myscript.ps1" ps You can call a function from another script
- And what about the "$?", "Start process"? - N. Turshiev
- And not a big amendment, this exitcode should be processed in the same script - N. Turshiev
- already answered this question. look, please - Senior Pomidor
- Some porridge, this is not the answer to my question, in my opinion there is a general discussion about something else. - N. Turshiev
- forget it. What exactly through the "start process" do you want to do? - Senior Pomidor
If the question is just anonymous with cmd but for normal PowerShell scripts, the answer is: there is no such code and it is not necessary. The PowerShell script generates one object with data, entirely in a variable or part of an object on the screen and possibly one or several error messages. Success can be considered that in the output object there are any specific data. It's like a lot of different completion codes, which I have only dreamed of in cmd for years.
errorlevel- code completion? - nick_n_a