Search-FileAccess PowerShell Script
This file contains 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
function Search-FileAccess { | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$IdentityReferenceRegex, | |
[Parameter(Mandatory=$true)] | |
[string]$Path, | |
[Boolean]$Recurse=$true, | |
[switch]$Visualization | |
) | |
$matchingFolders = Get-ChildItem -Recurse:$Recurse $Path | Where-Object { | |
$isDirectory = $_.Attributes -contains 'Directory' | |
$accessList = $_ | Get-Acl | Select-Object -ExpandProperty Access | |
$matchingAccessList = $accessList | Where-Object { | |
$_.IdentityReference.Value -match $IdentityReferenceRegex | |
} | |
$accessListPersonMatched = $matchingAccessList.Count -gt 0 | |
$matchingAccessList = $matchingAccessList | Where-Object IsInherited -eq $false | |
$fileMatched = $matchingAccessList.Count -gt 0 | |
if ($Visualization) { | |
$symbol = if ($isDirectory) { '#' } else { ':' } | |
$color = if ($fileMatched) { 'Red' } else { | |
if ($accessListPersonMatched) { 'DarkGreen' } else { 'Green' } | |
} | |
Write-Host $symbol -ForegroundColor $color -NoNewline | |
} | |
return $fileMatched | |
} | |
if ($Visualization) { | |
Write-Host "`n`n`n" | |
} | |
if ($VerbosePreference) { | |
$statusColor = if ($matchingFolders.Count -gt 0) { 'Red' } else { 'Cyan'} | |
Write-Host "`n`nMatching folders: $($matchingFolders.Count)" -ForegroundColor $statusColor | |
$matchingFolders | Select-Object -ExpandProperty FullName | Sort-Object | Out-Host | |
} | |
return $matchingFolders | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment