Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Error handling through the pipeline

$
0
0

Generally, to deal with non-terminating errors (where there's no expected terminating errors happening), I prefer to use the -ErrorVariable parameter. For example:

Get-ADUser -Filter {Enabled = 'False'} -Properties Modified |
Where Modified -LT (Get-Date).AddDays(-30) |
Remove-ADUser -Confirm:$false -ErrorVariable removeUserErrors
 
# Here, $removeUserErrors will be an empty collection if everything worked, or will contain
# one or more ErrorRecord objects if any of the calls to Remove-ADUser failed.
 
if ($removeUserErrors.Count -gt 0) {
    $removeUserErrors | Out-File some-File.txt
}

Viewing all articles
Browse latest Browse all 13067

Trending Articles