Get-ACL , Is there a way to validate the UNC path folder by folder and assigning it to the variable

Copper Contributor

Get-ACL , Is there a way to validate the UNC path folder by folder and assigning it to the variable.

 

I am having issue performing Get-ACL for the Certain paths, sometimes i am able to get the info, sometime it fails stating folder doesnt exists.

3 Replies

You can run the path through Test-Path first, which will give you a true or false value depending on if the path exists.

 

One caveat here, which is especially relevant with UNC paths, is that you need to worry about access and credentials. You can't test or enumerate the ACLs on a path you're not authenticated against.

 

Assuming rights are all in check you could do something like this:

 

$Paths = 'imagine there are paths here'

foreach ($Path in $Paths) {
    if (Test-Path $Path -ErrorAction SilentlyContinue) {
        Get-Acl $Path
    } else {
        Write-Warning "The path $Path doesn't exist"
    }
}

The error action on Test-Path is there to suppress errors if access is denied, just to keep things tidier.

 

If you do need to worry about authenticating against some paths, you might want to check out example 4 on New-PSDrive.

@Joshua King  Thank you for taking time to respond to this, I am currently using the same approach. My issue here is . Though the provided path is correct I get the value as false, but later if I try couple of times on same path I get "true", I tried the PSdrive way as well by mapping the path and testing the path, which works the same way. I have tried the Dot notation way as well. This issue is intermediate. Is there an advanced way we can achieve this. if you can provide me info on why I would get "false" that would also narrow my concern.

Thank you in advance

 

Huh, that is odd. I sometimes have issues when pinging servers that they don't reply on the first echo request (hence I always do a count of 2 just to be sure.)

 

I don't think I can diagnose the intermittent false negative, but it may be workable to throw in a workaround. If it's working after a couple of attempts you could implement a retry mechanic.

 

Here's an example from Adam Bertram: https://adamtheautomator.com/create-wait-anything-function-powershell/

 

And another from Prateek Singh, though this one relies on the command throwing an error: https://ridicurious.com/2019/02/01/retry-command-in-powershell/

 

Basically, retry say 5 times. If it's still false move onto the next path.