Error Handling on Network Drives

Welcome Forums General PowerShell Q&A Error Handling on Network Drives

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
5 years, 7 months ago.

  • Author
    Posts
  • #10115

    Participant
    Topics: 8
    Replies: 33
    Points: 0
    Rank: Member

    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"
        }
    
  • #10118

    Participant
    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    Have you tried a standard Try\Catch?

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

    Participant
    Topics: 0
    Replies: 669
    Points: 0
    Rank: Member

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

  • #10266

    Participant
    Topics: 8
    Replies: 33
    Points: 0
    Rank: Member

    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.

The topic ‘Error Handling on Network Drives’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort