Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell -ErrorAction SilentlyContinue

PowerShell -ErrorAction SilentlyContinue

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.

Windows PowerShell -ErrorAction SilentlyContinue

 ♣

-ErrorAction Example:  Check If a Service Has Been Installed

# 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.

Problems With Stop-Process

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.

# PowerShell SilentlyContinue
Clear-Host
Stop-Process 5132, 5075, 5072 -ErrorAction SilentlyContinue

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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

Download your free copy of WMI Monitor

Problems Displaying Registry Hives

One common problem when enumerating hives in the registry is that the permissions on the security hive cause an error in the PowerShell script.

# PowerShell Registry Listing ErrorAction
Clear-Host
Set-Location HKLM:\
Get-Childitem -ErrorAction SilentlyContinue

Note 4: -ErrorAction SilentlyContinue suppresses the error message 'PermissionDenied to the SECURITY hive'.

Note 5:  In the output 'SKC' means SubKey count and 'VC' means Value count.

More PowerShell Problems -ErrorAction Solutions

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:  Solarwinds' Log & Event Management ToolSolarwinds Log and Event Management Tool

LEM will alert you to problems such as when a key application on a particular server is unavailable.  It can also detect when services have stopped, or if there is a network latency problem.  Perhaps this log and event management tool's most interesting ability is to take corrective action, for example by restarting services, or isolating the source of a maleware attack.

Yet perhaps the killer reason why people use LEM is for its compliance capability, with a little help from you, it will ensure that your organization complies with industry standards such as CISP or FERPA.  LEM is a really smart application that can make correlations between data in different logs, then use its built-in logic to take corrective action, to restart services, or thwart potential security breaches.

Download your FREE trial of Solarwinds Log & Event Management tool.

Research -ErrorAction Stop

This is how I found that -ErrorAction has an alternative to SilentlyContinue:
-ErrorAction Stop

# Research PowerShell -ErrorAction
Clear-Host
Get-Help about_commonParameters

Note 7:  Incidentally, Help about_common* works just as well.

Note 8:  Other scripts may benefit from substituting Stop or Inquire for the action to SilentlyContinue.

-ErrorAction Abbreviations or Aliases

Instead of ErrorAction SilentlyContinue you can try : -EA 0

# PowerShell Registry Listing
Clear-Host
Set-Location HKLM:\
Get-Childitem -EA 0

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

 


See more PowerShell examples

PowerShell Home   • Foreach loops   • PowerShell Foreach   • Foreach-Object cmdlet

Syntax   • Variables   • -whatIf   • -ErrorAction   • Windows PowerShell   • PowerShell 2.0

PowerShell Functions   • [System.Math]   • Get-Credential   • Windows 7 PowerShell 2.0

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.