PowerShell - Permission walk by UNC
- Jon Boyette
- Jan 10, 2022
- 1 min read
Save the code as a .ps1, run it and enter the \\server\share to discover, outputs to the profile that runs its directory.
#############################
$TargetDirectory = Read-Host "Enter - \\server_name\share"
#############################
$OutFile = ($TargetDirectory.Substring(2)).Replace("\","_") + "_PermissionsWalk.csv"
If (Get-Item ".\$($OutFile)") {Remove-Item ".\$($OutFile)" -Confirm:$false -Force}
$RootFolder = Get-Item -Path $TargetDirectory
Write-Host $RootFolder.FullName
Get-Acl $RootFolder.FullName | %{
$BlockInheritance = $_.AreAccessRulesProtected
$_.Access | %{
New-Object -TypeName PSObject -Property @{
Path = $RootFolder.FullName
Identity = $_.IdentityReference
AccessType = $_.AccessControlType
Permission = $_.FileSystemRights
Inherited = $_.IsInherited
Inheritance = $_.InheritanceFlags
BlockInheritance = $BlockInheritance
} | Select Path,Identity,AccessType,Permission,Inherited,Inheritance,BlockInheritance | Export-Csv $OutFile -NoTypeInformation -Append
}
}
Get-ChildItem -Directory -Path $TargetDirectory -Recurse -Force | %{
Write-Host $_.FullName
$FolderName = $_.FullName
Get-Acl $_.FullName | %{
$BlockInheritance = $_.AreAccessRulesProtected
$_.Access | %{
New-Object -TypeName PSObject -Property @{
Path = $FolderName
Identity = $_.IdentityReference
AccessType = $_.AccessControlType
Permission = $_.FileSystemRights
Inherited = $_.IsInherited
Inheritance = $_.InheritanceFlags
BlockInheritance = $BlockInheritance
} | Select Path,Identity,AccessType,Permission,Inherited,Inheritance,BlockInheritance | Export-Csv $OutFile -NoTypeInformation -Append
}
}
}
Comments