Collections:
Other Resources:
Execution Status and Errors in Windows PowerShell Scripts
How to check execution status and errors in Windows PowerShell scripts?
✍: FYIcenter.com
PowerShell provides 2 automatic variables to help you checking execution status and errors:
1. $? variable - The $? variable contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
2. $Error variable - The $Error variable contains an array of error objects that represent the most recent errors. The most recent error is the first error object in the array ($Error[0]).
Here is an example script on how to use $? and $Error variables:
Test-Connection "www.www.www" -Count 1 "Status: "+$? "Error: "+$Error[0]
If you run it, you will get:
PS C:\fyicenter> .\My-Ping.ps1
Test-Connection : Testing connection to computer 'www.www.www'
failed: The requested name is valid, but no data of the requested
type was found At C:\fyicenter\My-Ping.ps1:1 char:1
+ Test-Connection "www.www.www" -Count 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (www.www.www:String)
[Test-Connection], PingException
+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell
.Commands.TestConnectionCommand
Status: False
Error: Testing connection to computer 'www.www.www' failed: The
requested name is valid, but no data of the requested type was
found
⇒ Run Script with Arguments in Windows PowerShell
⇐ Loop Hashtable Members in Windows PowerShell Scripts
2016-10-27, ∼2633🔥, 0💬
Popular Posts:
Can I disable Windows service "COM+ Event System" to speedup my computer? Windows service "COM+ Even...
What is service "AEADIFilters" and program "AEADISRV.EXE" on Windows Vista? "AEADIFilters" is a syst...
What is the best way to remove programs from the startup application list? There are a number of way...
What is the difference between a Web page and a filtered Web page? Word 2007 supports 2 Web page for...
Can I disable Windows service "Network DDE" to speedup my computer? Microsoft application service "N...