Error Handling on Network Drives

I scan our network drives routinely to generate usage statistics. Sometimes I encounter situations where a user acidentally removed my permissions

Note: Giving users Full Control is not my call and it cannot be changed

I created a litle script that recursively looks through the network drive to record those denied permissions. It works fine on my local D and C drives, but is not recording the error when run against a network drive. The fields in $error are the same regardless of the drive scanned so I am unsure what I’m missing.

Here’s the code

$error.clear()
gci '\\Server6\share$\site' -recurse -errorvariable strOOPS
If ($stroops -ne $null)
    {
    $erec=$stroops
    add-content -value $erec c:\outputfiles\DENIED.TXT
    }
Else
    {
    Write-host "No Errors"
    }

Have you tried a standard Try\Catch?

try {
    gci '\\Server6\share$\site' -recurse 
    "Success"
}
catch {
    "Error occurred: {0}" -f $_.Exception.Message
}

You’ll need to set the erroraction to stop on get-childitem

I managed to get it working pretty well after some futzing around with it.

write-host -NoNewline "Enter UNC Path or Drive, such as \\fileserver10\region\Site\ or S:\ or Z:\ "                                                                     
$vStartingPath=read-host

gci $vStartingPath -recurse|where{$_.psiscontainer -eq $True} | foreach {
 try
 {
  $path = $_
  $_|get-acl|select mode
#  $_|get-acl|select pschildname
 }
 catch
 {
  $path.FullName | Out-File c:\outputfiles\PermDenied.txt -Append
 }    
}

It isn’t the fastest tool to use but it is the only one I have to check permissions so I’m not going to complain.