Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function RedditAnswer {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory=$true,
- Position=0,
- ParameterSetName="ReturnAll",
- ValueFromPipelineByPropertyName=$true,
- HelpMessage="Literal path to one or more locations.")]
- [Parameter(Mandatory=$true,
- Position=0,
- ParameterSetName="ReturnFiles",
- ValueFromPipelineByPropertyName=$true,
- HelpMessage="Literal path to one or more locations.")]
- [Parameter(Mandatory=$true,
- Position=0,
- ParameterSetName="ReturnDirectories",
- ValueFromPipelineByPropertyName=$true,
- HelpMessage="Literal path to one or more locations.")]
- [Alias("PSPath")]
- [ValidateNotNullOrEmpty()]
- [string]
- $LiteralPath,
- [int]
- $Depth,
- [switch]
- $Recurse,
- [Parameter(Mandatory=$true,
- ParameterSetName="ReturnFiles")]
- [switch]
- $File,
- [Parameter(Mandatory=$true,
- ParameterSetName="ReturnDirectories")]
- [switch]
- $Directory
- )
- begin {
- enum ItemType {
- File
- Directory
- EmptyDirectory
- AccessDenied
- }
- switch ($PSCmdlet.ParameterSetName) {
- 'ReturnAll' { $ReturnDirectories, $ReturnFiles = $true, $true }
- 'ReturnDirectories' { $ReturnDirectories = $true, $true }
- 'ReturnFiles' { $ReturnFiles = $true }
- }
- function CheckDirectory {
- [CmdletBinding()]
- param ([string]$Directory, [switch]$SkipSelfCheck)
- try {
- $DirectorySecurity = [System.Security.AccessControl.DirectorySecurity]::new($Directory, [System.Security.AccessControl.AccessControlSections]::Access)
- # Enumerate files
- foreach ($Item in [System.IO.Directory]::EnumerateFiles($Directory)) {
- if ($ReturnFiles) {
- $FileSecurity = [System.Security.AccessControl.FileSecurity]::new($Item, [System.Security.AccessControl.AccessControlSections]::Access)
- foreach ($Access in $FileSecurity.Access) {
- [ordered] @{
- Directory = Split-Path -Path $Item -Parent
- Name = Split-Path -Path $Item -Leaf
- Type = [ItemType]::File
- Identity = $Access.IdentityReference
- Permissions = $Access.FileSystemRights
- Inherited = $Access.IsInherited
- }
- }
- }
- }
- # Enumerate directories
- if ($ReturnDirectories) {
- foreach ($Item in [System.IO.Directory]::EnumerateDirectories($Directory)) {
- try {
- $DirectorySecurity = [System.Security.AccessControl.DirectorySecurity]::new($Item, [System.Security.AccessControl.AccessControlSections]::Access)
- foreach ($Access in $DirectorySecurity.Access) {
- [ordered] @{
- Directory = Split-Path -Path $Item -Parent
- Name = Split-Path -Path $Item -Leaf
- Type = [ItemType]::Directory
- Identity = $Access.IdentityReference
- Permissions = $Access.FileSystemRights
- Inherited = $Access.IsInherited
- }
- }
- }
- catch {
- [ordered] @{
- Directory = Split-Path -Path $Item -Parent
- Name = Split-Path -Path $Item -Leaf
- Type = [ItemType]::AccessDenied
- Identity = $null
- Permissions = $null
- Inherited = $null
- }
- }
- }
- }
- # Recursion
- if ($Recurse) {
- foreach ($Item in [System.IO.Directory]::EnumerateDirectories($Directory)) {
- CheckDirectory -Directory $Item -SkipSelfCheck
- }
- }
- elseif($script:CurrentDepth -lt $Depth) {
- $script:CurrentDepth++
- foreach ($Item in [System.IO.Directory]::EnumerateDirectories($Directory)) {
- CheckDirectory -Directory $Item -SkipSelfCheck
- }
- $script:CurrentDepth--
- }
- }
- catch {
- if (-not $SkipSelfCheck) {
- [ordered] @{
- Directory = Split-Path -Path $Directory -Parent
- Name = Split-Path -Path $Directory -Leaf
- Type = [ItemType]::AccessDenied
- Identity = $null
- Permissions = $null
- Inherited = $null
- }
- }
- }
- }
- }
- process {
- New-Variable -Scope 'Local' -Name 'CurrentDepth' -Value 0
- CheckDirectory -Directory $LiteralPath | ForEach-Object { [pscustomobject] $_ }
- Remove-Variable -Scope 'Local' -Name 'CurrentDepth'
- }
- end {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement