PnP
PnP

Reputation: 3185

Testing if a directory is browseable

I am mounting backup images captured via ShadowProtect to Explorer mountpoints. My initial test is to look for a file that would exist on the drive via Test-File in PowerShell. If TRUE, we can assume the backup image was mounted and is "good".

My issue comes with SystemReserved partitions that have no files I can test for existence. Is there any function in PowerShell (or NET?) that can simply test if the directory can be browsed (as opposed to looking for a certain file?). Or any other ideas that could achieve a similar thing?

Thanks!

NOTE: The images are read-only.

Upvotes: 0

Views: 47

Answers (1)

JohnLBevan
JohnLBevan

Reputation: 24430

$myPath = \\backupserver\share\folder\
$canRead = $true
try {
    Get-ChildItem $myPath -ErrorAction Stop | out-null
} catch [System.Exception] { #could be more specific on type of error here, but I'm guessing most errors would suggest some issue equivelant to a read permissions issue.
    $canRead = $false
}
if($canRead) { ":)" } else { ":(" }

Upvotes: 1

Related Questions