Execution Status and Errors in Windows PowerShell Scripts
How to check execution status and errors in Windows PowerShell scripts?
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).
Here is an example script on how to use $? and $Error variables:
Test-Connection "www.www.www" -Count 1 "Status: "+$? "Error: "+$Error
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 t ype was found
2016-10-27, 974👍, 0💬
If you are using a wireless router to connecting to the Internet through a DSL modem, how many IP ad...
What is QLBCTRL.exe - Process - QLB Controller? Process QLBCTRL.exe is the QLB (Quick Launch Button)...
How do I know what cookies a Website is sending to my computer? When you are visiting a Website, it ...
What is "Windows Connect Now - Config Registrar" in my Windows 7 service list? And how is "Windows C...
How to unblock a publisher? 1. Open Internet Explorer. 2. Click the Tools button, and then click Int...