In powershell, is there a way to check if the path is valid regardless deny access?
in powershell, there way check if path valid regardless deny access?
i ran problem when tried verify unc path of \$servername\$sharename using powershell cmdlet "test-path" (see code below). cmdlet return "false" unc path although path exist (but has deny access group in place person ran "test-path").
test-path "\\$servername\$sharename"
i need way check if path exist or not regardless deny access group in place.
do have 1 liner (or small block of codes) in powershell resolve this?
indeed, jrv pointed out, need use -erroraction stop since error non terminating error.
while code in last reply working you, use improvement since assuming error occurs when running test-path on path interpreted path exists access denied.
a better way be, example:
[boolean]$pathexist = $false
try
{
$pathexist = test-path $path -erroraction stop
}
catch
{
if($_.exception.message -eq 'access denied')
{
$pathexist = $true
}
else
{
#handle error
}
}
Windows Server > Windows PowerShell
Comments
Post a Comment