A. PowerShell has an error action option that you can add to commands. The switch
will cause PowerShell to take no action when it encounters an error. This can be useful if you're checking for data that may not exist and would normally return an error.
For example, the command
get-hotfix -id KB974332 -computername savdaldc11
will return an error if the specified computer doesn't have hotfix KB974332 installed. If you instead use the command
get-hotfix -id KB974332 -computername savdaldc11 -ea 0
nothing will happen and you won't get an error message.
The -ea 0 switch is actually equivalent to the command -ErrorAction SilentlyContinue, as in
get-hotfix -id KB974332 -computername savdaldc11 -ErrorAction SilentlyContinue
Other options for the error action are stop (1), continue (2) and inquire (3). The default action is continue, which you can check by viewing the $ErrorActionPreference value with the command
The stop option halts execution in the event of an error and inquire asks the user whether to continue or halt.
- Q. How can I have a script check if a certain patch is installed?
- Solving PowerShell Argument Input Errors with Wrapper Scripts
- Use PowerShell to Execute Commands on Remote Machines
- New Features in Windows Server 2008 R2
Check out hundreds more useful Q&As like this in John Savill's FAQ for Windows. Also, watch instructional videos made by John at ITTV.net.