Last active
April 1, 2023 17:10
-
-
Save bluPhy/a4cac4df74c58b84e513152eb854cdfc to your computer and use it in GitHub Desktop.
List all share and NTFS permissions in the local machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ComputerName = $env:computername | |
| $AllShares = Get-WmiObject -Class win32_share -ComputerName $ComputerName | Select-Object -ExpandProperty Name | |
| Function GetShareSecurity { | |
| Param([string]$path = $(throw"$path required.")) | |
| Write-Host "--------------------------------------------------" | |
| $pathparts = $path.split("\") | |
| $ComputerName = $pathparts[2] | |
| $ShareName = $pathparts[3] | |
| Write-Host "File Sharing Permissions Report - $path" | |
| $acl = Get-Acl $path | |
| Write-Host "File/NTFS Permissions" | |
| foreach ($accessRule in $acl.Access) { | |
| Write-Host " " $accessRule.IdentityReference $accessRule.FileSystemRights | |
| } | |
| Write-Host | |
| Write-Host "Share/SMB Permissions" | |
| $Share = Get-WmiObject win32_LogicalShareSecuritySetting -Filter "name='$ShareName'" -ComputerName $ComputerName | |
| if ($Share) { | |
| #$obj = @() | |
| $ACLS = $Share.GetSecurityDescriptor().Descriptor.DACL | |
| foreach ($ACL in $ACLS) { | |
| $User = $ACL.Trustee.Name | |
| if (!($user)) { $user = $ACL.Trustee.SID } | |
| $Domain = $ACL.Trustee.Domain | |
| switch ($ACL.AccessMask) { | |
| 2032127 { $Perm = "Full Control" } | |
| 1245631 { $Perm = "Change" } | |
| 1179817 { $Perm = "Read" } | |
| } | |
| Write-Host " $Domain\$user $Perm" | |
| } | |
| } | |
| else { | |
| Write-Host "Share $ShareName not found" | |
| } | |
| Write-Host | |
| } | |
| foreach ($ShareItem in $AllShares) { | |
| If ($ShareItem -eq "IPC$") { | |
| Write-Host "Default Share for Remote IPC... nothing to do" | |
| } | |
| else { | |
| $path = "\\" + $ComputerName + "\" + $ShareItem | |
| try { | |
| GetShareSecurity -path $path | |
| } | |
| catch { | |
| $ErrorMessage = $_.Exception.Message | |
| Write-Host "Something went wrong with $path" | |
| Write-Host "Error Message: $ErrorMessage" | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment