# Generate CSV file with all shares and share/ntfs permissions
Clear
# Change to the target server
$ComputerName = 'YourServer'
# Change to the path you want the CSV to go to
$OuputPath = 'C:\DisasterRecovery\Backup_Shares\' + $ComputerName + '_Shares.csv'
$SecurityOut = @()
$Shares = Get-WmiObject -Class Win32_Share -ComputerName $ComputerName | Where-Object {$_.Type -eq 0} | Select Name, Path
ForEach ($ShareItem in $Shares) {
$ShareName = $ShareItem.Name
$SharePath = $ShareItem.Path
$ShareUNCPath = "\\$ComputerName\$ShareName"
$Acl = Get-Acl $ShareUNCPath
# Get NTFS permissions for Share
ForEach ($AccessRule in $Acl.Access) {
$SecurityType = 'NTFS'
$SecurityOut += New-Object PSObject -Property @{ShareName=$($ShareName);SharePath=$($SharePath);SecurityType=$($SecurityType);UserName=$($AccessRule.IdentityReference);UserAccess=$($AccessRule.FileSystemRights)}
}
# Get SMB permissions for Share
$Share = Get-WmiObject win32_LogicalShareSecuritySetting -Filter "name='$ShareName'" -ComputerName $ComputerName
If($Share) {
$Obj = @()
$ACLS = $Share.GetSecurityDescriptor().Descriptor.DACL
ForEach($ACL in $ACLS) {
$SecurityType = 'SMB'
$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"}
}
$SecurityOut += New-Object PSObject -Property @{ShareName=$($ShareName);SharePath=$($SharePath);SecurityType=$($SecurityType);UserName=$("$Domain\$User");UserAccess=$($Perm)}
}
}
}
$SecurityOut | Select-Object ShareName, SharePath, SecurityType, UserName, UserAccess | Export-Csv -NoTypeInformation -Path $OuputPath