Logging All Errors

by Apr 18, 2014

In a previous tip you have seen that cmdlet errors can only be caught by an error handler if the -ErrorAction is set to "Stop". Doing this alters the way the cmdlet works, though. It will then stop at the first error encountered.

Take the next example: it scans the windows folder for PowerShell scripts recursively. If you wanted to catch errors (accessing protected subfolders, for example), this would not work:

try
{
  Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction Stop
}
catch
{
  Write-Warning "Error: $_"
} 

The code would catch the first error, but the cmdlet would stop and not continue to scan the remaining subfolders.

If you just suppressed errors, you would get complete results, but now your error handler would not catch anything:

try
{
  Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue
}
catch
{
  Write-Warning "Error: $_"
} 

So if you want a cmdlet to run uninterruptedly, and still get a list of all folders that you were unable to access, do not use an error handler at all. Instead, use -ErrorVariable and log the errors silently to a variable.

After the cmdlet has finished, you can evaluate the variable and get an error report:

Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue -ErrorVariable myErrors

Foreach ($incidence in $myErrors)
{
    Write-Warning ("Unable to access " + $incidence.CategoryInfo.TargetName)
} 


Twitter This Tip! ReTweet this Tip!