PowerShell for Windows Admins

Apr 21 2010   2:30PM GMT

Shares:Security



Posted by: Richard Siddaway
Tags:
PowerShell v2
Shares

 

In the previous post we saw how to get the Access Mask for the current user for a particular share.  It would be better if we could dump the information for all shares and all security access in one go.

We need to use Win32_LogicalAccess

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
function Get-ShareSecurity {

    Get-WmiObject -Class Win32_LogicalShareAccess |
    foreach {
       $name = ($_.SecuritySetting -split "=")[1]
       $sid = (($_.Trustee -split "=")[1]).Replace(‘"’,)
       $AccessMask = $_.AccessMask
      
       $query =  "ASSOCIATORS OF {Win32_SID.SID=’" `
       + $sid + "’} WHERE ResultClass=Win32_SystemAccount"
      
       $trustee = Get-WmiObject -Query $query 
      
       if($trustee -eq $null){
            $query =  "ASSOCIATORS OF {Win32_SID.SID=’" `
            + $sid + "’} WHERE ResultClass=Win32_UserAccount"
            #$query
            $trustee = Get-WmiObject -Query $query 
       }
      
       if($trustee -eq $null){
            $query =  "ASSOCIATORS OF {Win32_SID.SID=’" `
            + $sid + "’} WHERE ResultClass=Win32_Group"
             $trustee = Get-WmiObject -Query $query 
       }
      
       "`nSHARE: $name USER: $($trustee.Caption) RIGHTS:"
       ## now we need to unravel the rights
        $mask.GetEnumerator()| sort Key | 
        foreach {
            if ($AccessMask -band $_.key){"$($mask[$($_.key)])"}
        }
      
    }
}

 

It returns an object for each share for each user or group that gets access to the share.

For each object returned we get the share name, the SID and the access mask. We use the SID to find the user, system or group account that has that access and then dump out the rights as we saw last time

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: