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

%3CLINGO-SUB%20id%3D%22lingo-sub-1163653%22%20slang%3D%22en-US%22%3EGet-ACL%20%2C%20Is%20there%20a%20way%20to%20validate%20the%20UNC%20path%20folder%20by%20folder%20and%20assigning%20it%20to%20the%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1163653%22%20slang%3D%22en-US%22%3E%3CP%3EGet-ACL%20%2C%20Is%20there%20a%20way%20to%20validate%20the%20UNC%20path%20folder%20by%20folder%20and%20assigning%20it%20to%20the%20variable.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20having%20issue%20performing%20Get-ACL%20for%20the%20Certain%20paths%2C%20sometimes%20i%20am%20able%20to%20get%20the%20info%2C%20sometime%20it%20fails%20stating%20folder%20doesnt%20exists.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1163653%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1205975%22%20slang%3D%22en-US%22%3ERe%3A%20Get-ACL%20%2C%20Is%20there%20a%20way%20to%20validate%20the%20UNC%20path%20folder%20by%20folder%20and%20assigning%20it%20to%20the%20varia%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1205975%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20run%20the%20path%20through%20%3CEM%3ETest-Path%3C%2FEM%3E%20first%2C%20which%20will%20give%20you%20a%20true%20or%20false%20value%20depending%20on%20if%20the%20path%20exists.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20caveat%20here%2C%20which%20is%20especially%20relevant%20with%20UNC%20paths%2C%20is%20that%20you%20need%20to%20worry%20about%20access%20and%20credentials.%20You%20can't%20test%20or%20enumerate%20the%20ACLs%20on%20a%20path%20you're%20not%20authenticated%20against.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAssuming%20rights%20are%20all%20in%20check%20you%20could%20do%20something%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%24Paths%20%3D%20'imagine%20there%20are%20paths%20here'%0A%0Aforeach%20(%24Path%20in%20%24Paths)%20%7B%0A%20%20%20%20if%20(Test-Path%20%24Path%20-ErrorAction%20SilentlyContinue)%20%7B%0A%20%20%20%20%20%20%20%20Get-Acl%20%24Path%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20Write-Warning%20%22The%20path%20%24Path%20doesn't%20exist%22%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20error%20action%20on%20Test-Path%20is%20there%20to%20suppress%20errors%20if%20access%20is%20denied%2C%20just%20to%20keep%20things%20tidier.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20do%20need%20to%20worry%20about%20authenticating%20against%20some%20paths%2C%20you%20might%20want%20to%20check%20out%20example%204%20on%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.management%2Fnew-psdrive%3Fview%3Dpowershell-7%23examples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ENew-PSDrive%3C%2FA%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1205993%22%20slang%3D%22en-US%22%3ERe%3A%20Get-ACL%20%2C%20Is%20there%20a%20way%20to%20validate%20the%20UNC%20path%20folder%20by%20folder%20and%20assigning%20it%20to%20the%20varia%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1205993%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F62408%22%20target%3D%22_blank%22%3E%40Joshua%20King%3C%2FA%3E%26nbsp%3B%20Thank%20you%20for%26nbsp%3Btaking%20time%20to%20respond%20to%20this%2C%20I%20am%20currently%20using%20the%20same%20approach.%20My%20issue%20here%20is%20.%20Though%20the%20provided%20path%20is%20correct%20I%20get%20the%20value%20as%20false%2C%20but%20later%20if%20I%20try%20couple%20of%20times%20on%20same%20path%20I%20get%20%22true%22%2C%20I%20tried%20the%20PSdrive%20way%20as%20well%20by%20mapping%20the%20path%20and%20testing%20the%20path%2C%20which%20works%20the%20same%20way.%20I%20have%20tried%20the%20Dot%20notation%20way%20as%20well.%20This%20issue%20is%20intermediate.%20Is%20there%20an%20advanced%20way%20we%20can%20achieve%20this.%20if%20you%20can%20provide%20me%20info%20on%20why%20I%20would%20get%20%22false%22%20that%20would%20also%20narrow%20my%20concern.%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1206037%22%20slang%3D%22en-US%22%3ERe%3A%20Get-ACL%20%2C%20Is%20there%20a%20way%20to%20validate%20the%20UNC%20path%20folder%20by%20folder%20and%20assigning%20it%20to%20the%20varia%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1206037%22%20slang%3D%22en-US%22%3E%3CP%3EHuh%2C%20that%20is%20odd.%20I%20sometimes%20have%20issues%20when%20pinging%20servers%20that%20they%20don't%20reply%20on%20the%20first%20echo%20request%20(hence%20I%20always%20do%20a%20count%20of%202%20just%20to%20be%20sure.)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20don't%20think%20I%20can%20diagnose%20the%20intermittent%20false%20negative%2C%20but%20it%20may%20be%20workable%20to%20throw%20in%20a%20workaround.%20If%20it's%20working%20after%20a%20couple%20of%20attempts%20you%20could%20implement%20a%20retry%20mechanic.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere's%20an%20example%20from%20Adam%20Bertram%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fadamtheautomator.com%2Fcreate-wait-anything-function-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fadamtheautomator.com%2Fcreate-wait-anything-function-powershell%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20another%20from%26nbsp%3BPrateek%20Singh%2C%20though%20this%20one%20relies%20on%20the%20command%20throwing%20an%20error%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fridicurious.com%2F2019%2F02%2F01%2Fretry-command-in-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fridicurious.com%2F2019%2F02%2F01%2Fretry-command-in-powershell%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBasically%2C%20retry%20say%205%20times.%20If%20it's%20still%20false%20move%20onto%20the%20next%20path.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted

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.

Highlighted

@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

 

Highlighted

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.