Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function RedditAnswer {
  2.         [CmdletBinding()]
  3.         param (
  4.             [Parameter(Mandatory=$true,
  5.                         Position=0,
  6.                         ParameterSetName="ReturnAll",
  7.                         ValueFromPipelineByPropertyName=$true,
  8.                         HelpMessage="Literal path to one or more locations.")]
  9.             [Parameter(Mandatory=$true,
  10.                         Position=0,
  11.                         ParameterSetName="ReturnFiles",
  12.                         ValueFromPipelineByPropertyName=$true,
  13.                         HelpMessage="Literal path to one or more locations.")]
  14.             [Parameter(Mandatory=$true,
  15.                         Position=0,
  16.                         ParameterSetName="ReturnDirectories",
  17.                         ValueFromPipelineByPropertyName=$true,
  18.                         HelpMessage="Literal path to one or more locations.")]
  19.             [Alias("PSPath")]
  20.             [ValidateNotNullOrEmpty()]
  21.             [string]
  22.             $LiteralPath,
  23.  
  24.             [int]
  25.             $Depth,
  26.  
  27.             [switch]
  28.             $Recurse,
  29.  
  30.             [Parameter(Mandatory=$true,
  31.                         ParameterSetName="ReturnFiles")]
  32.             [switch]
  33.             $File,
  34.  
  35.             [Parameter(Mandatory=$true,
  36.                         ParameterSetName="ReturnDirectories")]
  37.             [switch]
  38.             $Directory
  39.         )
  40.        
  41.         begin {
  42.             enum ItemType {
  43.                 File
  44.                 Directory
  45.                 EmptyDirectory
  46.                 AccessDenied
  47.             }
  48.  
  49.             switch ($PSCmdlet.ParameterSetName) {
  50.                 'ReturnAll' { $ReturnDirectories, $ReturnFiles = $true, $true }
  51.                 'ReturnDirectories' { $ReturnDirectories = $true, $true }
  52.                 'ReturnFiles' { $ReturnFiles = $true }
  53.             }
  54.             function CheckDirectory {
  55.                 [CmdletBinding()]
  56.                 param ([string]$Directory, [switch]$SkipSelfCheck)
  57.                 try {
  58.                     $DirectorySecurity = [System.Security.AccessControl.DirectorySecurity]::new($Directory, [System.Security.AccessControl.AccessControlSections]::Access)
  59.  
  60.                     # Enumerate files
  61.                     foreach ($Item in [System.IO.Directory]::EnumerateFiles($Directory)) {
  62.                         if ($ReturnFiles) {
  63.                             $FileSecurity = [System.Security.AccessControl.FileSecurity]::new($Item, [System.Security.AccessControl.AccessControlSections]::Access)
  64.                             foreach ($Access in $FileSecurity.Access) {
  65.                                 [ordered] @{
  66.                                     Directory = Split-Path -Path $Item -Parent
  67.                                     Name = Split-Path -Path $Item -Leaf
  68.                                     Type = [ItemType]::File
  69.                                     Identity = $Access.IdentityReference
  70.                                     Permissions = $Access.FileSystemRights
  71.                                     Inherited = $Access.IsInherited
  72.                                 }
  73.                             }
  74.                         }
  75.                     }
  76.                    
  77.                     # Enumerate directories
  78.                     if ($ReturnDirectories) {
  79.                         foreach ($Item in [System.IO.Directory]::EnumerateDirectories($Directory)) {
  80.                             try {
  81.                                 $DirectorySecurity = [System.Security.AccessControl.DirectorySecurity]::new($Item, [System.Security.AccessControl.AccessControlSections]::Access)
  82.                                 foreach ($Access in $DirectorySecurity.Access) {
  83.                                     [ordered] @{
  84.                                         Directory = Split-Path -Path $Item -Parent
  85.                                         Name = Split-Path -Path $Item -Leaf
  86.                                         Type = [ItemType]::Directory
  87.                                         Identity = $Access.IdentityReference
  88.                                         Permissions = $Access.FileSystemRights
  89.                                         Inherited = $Access.IsInherited
  90.                                     }  
  91.                                 }
  92.                             }
  93.                             catch {
  94.                                 [ordered] @{
  95.                                     Directory = Split-Path -Path $Item -Parent
  96.                                     Name = Split-Path -Path $Item -Leaf
  97.                                     Type = [ItemType]::AccessDenied
  98.                                     Identity = $null
  99.                                     Permissions = $null
  100.                                     Inherited = $null
  101.                                 }
  102.                             }
  103.                         }
  104.                     }
  105.                    
  106.                     # Recursion
  107.                     if ($Recurse) {
  108.                         foreach ($Item in [System.IO.Directory]::EnumerateDirectories($Directory)) {
  109.                             CheckDirectory -Directory $Item -SkipSelfCheck
  110.                         }
  111.                     }
  112.                     elseif($script:CurrentDepth -lt $Depth) {
  113.                         $script:CurrentDepth++
  114.                         foreach ($Item in [System.IO.Directory]::EnumerateDirectories($Directory)) {
  115.                             CheckDirectory -Directory $Item -SkipSelfCheck
  116.                         }
  117.                         $script:CurrentDepth--
  118.                     }
  119.                 }
  120.                 catch {
  121.                     if (-not $SkipSelfCheck) {
  122.                         [ordered] @{
  123.                             Directory = Split-Path -Path $Directory -Parent
  124.                             Name = Split-Path -Path $Directory -Leaf
  125.                             Type = [ItemType]::AccessDenied
  126.                             Identity = $null
  127.                             Permissions = $null
  128.                             Inherited = $null
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.        
  135.         process {
  136.             New-Variable -Scope 'Local' -Name 'CurrentDepth' -Value 0
  137.             CheckDirectory -Directory $LiteralPath | ForEach-Object { [pscustomobject] $_ }
  138.             Remove-Variable -Scope 'Local' -Name 'CurrentDepth'
  139.         }
  140.        
  141.         end {
  142.         }
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement