0

I have a super annoying problem, where I am basically doing the following:

Get-ChildItem $rootPath -Recurse | 
    Where {$_.psiscontainer} | 
        Get-Acl | % { 
            Write-Host "Doing some stuff here" 
        }

But I occasionally get an error when calling Get-Acl saying that I don't have access.

That is fine, I can just catch the error and continue right?

So i tried

Get-ChildItem $rootPath -Recurse | 
    Where {$_.psiscontainer} | 
        try {
            Get-Acl | % { 
                Write-Host "Doing some stuff here" 
            }
        } catch {Write-Host "error"}

But now I get

the term 'try' is not recognized

Which is unbelievably unhelpful, since try definitely is a command in PowerShell...

Next I tried to split the command out like so

$folders = Get-ChildItem $rootPath -Recurse | Where {$_.psiscontainer}

foreach ($folder in $folders) {
    try {
        Get-Acl | % { 
            Write-Host "Doing some stuff here" 
        }
    } catch {write-host "error"}
}

Now i'm getting

Get-childitem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

WHY? And how am I supposed to check which folder is failing?

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Not every command accepts pipeline input. You could do `GCI $rootfolder -directory |ForEach{try{Get-Acl $_|}Catch{"Error"}}`. As for your path that is too long, you should be able to check `$errors` to see what it path it threw the error on. – TheMadTechnician Mar 21 '18 at 00:15
  • 1
    `Try` isn't a command, it's a keyword. You can't pipe into `try` like you can't pipe into ([get-help about_language_keywords](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_keywords?view=powershell-6)) `return` or `break` or `InlineScript`. "**WHY?**" - why the limit exists? Backwards compatibility - https://stackoverflow.com/q/1880321/478656 – TessellatingHeckler Mar 21 '18 at 01:43

1 Answers1

0

Perhaps try something like:

$folders = Get-ChildItem $rootPath -Recurse -Directory -ErrorAction SilentlyContinue

foreach ($folder in $folders) {
    try {
        Get-Acl -Path $folder | % { 
            Write-Host "Doing some stuff here" 
        }
    } catch {write-host "error"}
}

where you can use -Directory and also continue on errors (ErrorAction).

Also, you need to pass arguments to Get-Acl now that you have unrolled the pipeline (a good move for debugging).

Kory Gill
  • 6,993
  • 1
  • 25
  • 33