Created
January 15, 2023 05:14
-
-
Save emmaly/ff50aa8d063f356cf0dfb144798ee16c to your computer and use it in GitHub Desktop.
Search-FileAccess PowerShell Script
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
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