Share via


In powershell, is there a way to check if the path is valid regardless deny access?

Question

Wednesday, November 30, 2016 10:49 PM

In powershell, is there a way to check if the path is valid regardless deny access?

I ran into this problem when I tried to verify the UNC Path of \ServerName\ShareName using powershell cmdlet "test-path" (see code below). The cmdlet will return "false" to the UNC path although the path does exist (but it has deny access group in place for the person who ran the "test-path").

test-path "\\$serverName\$shareName"

I need a way to check if the path exist or not regardless the deny access group in place.

Do you have one liner (or small block of codes) in powershell to resolve this?

All replies (8)

Wednesday, November 30, 2016 11:53 PM ✅Answered

Several scenarios for this:

1. If you have Share and NTFS access, the result of Test-Path will be true

2. If you have Share access but no NTFS access, the result of Test-Path will still be true

3. If the share exists but you do not have Share access to it (NTFS permissions are not relevant in this case), Test-Path will return False but will also throw an error

4. If the share does not exist, Test-Path will return false without throwing an error

If your case (which is scenario 3.), Test-Path the share and catch any errors, the message is Access is Denied, you will then know that the share exists but you do not have access to it.


Thursday, December 1, 2016 1:48 AM ✅Answered

Thank you so much, jrv!

With help from Emmanuel R and jrv, I finally got my solution. Here is my codes. It will serve for users who can verify the path is legit regardless if they have access or not.

[boolean]$PathExist = $False
        Try { $PathExist = test-path $path -ErrorAction stop }
        catch { $PathExist = $True }

Thursday, December 1, 2016 3:13 AM ✅Answered | 1 vote

Indeed, as jrv pointed out, you need to use -ErrorAction Stop since the error is a non terminating error.

While the code in your last reply is working for you, it could use some improvement since you are assuming that any error that occurs when you are running Test-Path on the path is to be interpreted as the path exists but access denied.

A better way would be, for example:

[boolean]$PathExist = $False
try
{
    $PathExist = Test-Path $path -ErrorAction Stop
}
catch
{
    if($_.Exception.Message -eq 'Access is Denied')
    {
        $PathExist = $True
    }
    else
    {
        #handle the error
    }
}

Wednesday, November 30, 2016 10:52 PM

How can you check something you are not alowed to look at?

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Wednesday, November 30, 2016 11:33 PM

Well, that's a good question...
However, that's exactly I would like to accomplish.

If you have all the "keys" (allow access) for all the "doors" (the UNC paths), there is no problem.  But, you want to identify if there is a "door" regardless if you are holding the "key" or not.  In the case of "test-path" cmdlet, it won't allow you to identify the "doors" without the "keys".  That's the scenario I want to overcome.

Thanks

Liang


Wednesday, November 30, 2016 11:38 PM

Asking for the impossible is a good technique.  Unfortunately without help from Merlin or a good hacker you will not get what you want.

I recommend trying the lottery first.  At least there is some small chance that you will succeed.

\(ツ)_/


Thursday, December 1, 2016 1:01 AM

Hi Emmanuel, that's fantastic observation and you are absolutely correct!

However, I wasn't able to catch the error.  My code is following.

Try {
$PathExist = test-path \\vm-server-101\lalala
}
Catch
{
Write-Verbose "Got it!" -Verbose
}

It failed to go to Catch script block.

Is there a way to catch the error? 


Thursday, December 1, 2016 1:08 AM | 1 vote

If you are denied access then the share is as good as non-existent.

To catch an error you must use "Stop"

$PathExist =test-path \vm-server-101\lalala -ErrorAction Stop

\(ツ)_/