Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell ErrorAction SilentlyContinue NOT working

The code below works when path exists but if it encounters a path that does not exits it fails and stops I would like for it to continue bad paths here are expected because server could have been decommed

(Get-ChildItem -Filter $SearchString  -Path $RootString -Recurse  | Measure-Object -ErrorAction SilentlyContinue).Count

This is my error

Get-ChildItem : The network path was not found
At line:1 char:1
+ Get-ChildItem -Filter $SearchString  -Path $RootString -Recurse  | Me ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetChildItemCommand

How can I write this so it continues if it errors.
I am on PS 5.1.17

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your -ErrorAction parameter is on your Measure-Object command, and not your Get-ChildItem command. I would guess this means any errors encountered with Get-ChildItem are treated normally, and any errors encountered by Measure-Object will silently continue since that’s where you’ve placed the parameter.

This is what I would try next in your testing.

(Get-ChildItem -Filter $SearchString -Path $RootString -Recurse -ErrorAction SilentlyContinue | Measure-Object).Count
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading