In previous techniques you can observe the use of "-erroraction stop" combined with "exception capture command" To catch a PowerShell command error, but after using this mode of operation, the script will stop after the first error occurs.
Here's an example: Use PowerShell to scan a folder recursively. It will not be able to finish capturing all the intermediate exceptions (for example, some subfolders are protected by Access).
Copy Code code as follows:
Try
{
Get-childitem-path $env: Windir-filter *.ps1-recurse-erroraction Stop
}
Catch
{
Write-warning "Error: $_"
}
The code catches the first error when the command stops and does not continue to scan the remaining subfolders.
If you just suppress the error, you will complete execution, but "Exception capture command" will not catch any error messages.
Copy Code code as follows:
Try
{
Get-childitem-path $env: Windir-filter *.ps1-recurse-erroraction silentlycontinue
}
Catch
{
Write-warning "Error: $_"
}
So if you want to execute continuously and get access to a directory without permission, you do not need to use the Exception Capture command, you can specify variables to get all error reports:
Copy Code code as follows:
Get-childitem-path $env: Windir-filter *.ps1-recurse-erroraction silentlycontinue-errorvariable myErrors
Foreach ($incidence in $myErrors)
{
Write-warning ("Unable to access" + $incidence. Categoryinfo.targetname)
}