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
CodePudding user response:
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