If a PowerShell script halts, or a
portion of the code does not work, what action do you want the error to
trigger? One popular solution is to tell the script to
silently continue.
# PowerShell -ErrorAction SilentlyContinue example
Clear-Host
$SrvName = "Printer Spooler" $Service =
Get-Service -display $SrvName -ErrorAction
SilentlyContinue if (-Not $Service) {$SrvName + " is
NOT
installed check the name."} else
{$SrvName + " is installed." $SrvName + "'s status is: " + $service.Status
}
Note 1: While this example shows -ErrorAction, you must decide if it's better than
the built-In message ObjectNotFound. Action Point remove -ErrorAction
SilentlyContinue from the above script.
Note 2: The actual name of this service is Print Spooler
(not Printer). I made this deliberate mistake so
as to create the error message.
Scenario you create a PowerShell script which will kill several
processes. The problem arises when the first process does not
exist, consequently the script comes to a halt prematurely.
"Cannot find a process with the process identifier 5132"
Zapping processes is a classic job for SilentlyContinue ... provided you know what you're
doing! If you would like to try this for real, then launch Task
Manager and note the PID (process ID) of one real and two fictitious
processes. Then substitute your PIDs for 5132, 5075, 5072 in my script below.
Note 3: Please don't use Stop-Process unless you
understand what you are doing, for instance, you make a 'walk-though' of
stopping the process with task
manager.
Guy
Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft's operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory, or Exchange Server. Give this WMI monitor a
try - it's free.
Problem: finding files in the System32 folder using a 'where .extension' clause. Specifically, I
got this error: Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup' is
denied.
Solution: add -ErrorAction SilentlyContinue after -recurse.
# PowerShell example: Find executables under the System32
folder Clear-Host $Path = "C:\Windows\System32"
Get-ChildItem $Path -recurse -ErrorAction SilentlyContinue `
| where {$_.Extension -Match "exe"}
Note 6: My friend 'Mad' Mick says you can cure the problem
by simply saying: Get-ChildItem C:\Windows\System32\*.exe -recurse
Guy responds: It's true that Mick's method is superior,
but I wanted a simple problem that -ErrorAction could cure.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Network Performance Monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
What I like best is the way NPM suggests solutions to network
problems. Its also has the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating
network maps, then I recommend that you try NPM now.
Researching the about_commonParameters file help file will explain why
these also work: -EA 1 Continue -EA 2 Inquire -EA 3 Confirm
-EA 4 Stop
»
Summary of PowerShell -ErrorAction
If a script pauses to produce an error message there maybe times when
you want to suppress such system messages. Alternatively, you
may want -ErrorAction to stop the script.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.