@@ -510,47 +510,39 @@ function Idm-FoldersRead {
510510 $access_profiles = GetAccessProfiles $system_params $function_params
511511
512512 foreach ($path_spec in $system_params.paths_spec ) {
513- Log debug " Path: $ ( $path_spec.path ) "
513+ Log debug " Path: $ ( $path_spec.path ) "
514514 $path_with_backslash = AppendBackslashToPath $path_spec.path
515515
516516 $gci_args = @ {
517- Directory = $true
518- Force = $true
519- LiteralPath = $path_spec.path
520- Recurse = $system_params.recursive
517+ Path = $path_spec.path
521518 Depth = 0
522- ErrorAction = ' SilentlyContinue '
519+ Exclude = $system_params .excludes
523520 }
524-
521+
525522 if ($system_params.recursive ) {
523+ Log debug " Setting depth [$ ( $system_params.recursion_depth ) ]"
526524 $gci_args.Depth = $system_params.recursion_depth
527525 }
528526
529527 if ($path_spec.depth -ge 0 ) {
530528 $gci_args.Depth = $path_spec.depth
531529 }
532530
533- LogIO info ' Get-ChildItem ' -In @gci_args - Exclude $system_params .excludes - Properties $function_params.properties
531+ LogIO info ' GetItemsWithDepth ' -In @gci_args - Properties $function_params.properties
534532
535533 try {
536- # This is to correct error messages, e.g.:
537- # "Cannot find drive. A drive with the name 'x' does not exist" instead of
538- # "A parameter cannot be found that matches parameter name 'Directory'".
539- Get-ChildItem - Force - LiteralPath $path_spec.path > $null
540-
541- # For directories, Get-ChildItem returns [System.IO.DirectoryInfo]
542- Get-ChildItem @gci_args | ForEach-Object {
534+ # For directories, Get-ChildItem returns [System.IO.DirectoryInfo]
535+ GetItemsWithDepth @gci_args | ForEach-Object {
543536 foreach ($exclude in $system_params.excludes ) {
544537 if ($_.FullName -ilike $exclude ) { return }
545538 }
546-
547539 $_
548540 } | ForEach-Object {
549- # For directories, GetAccessControl() returns [System.Security.AccessControl.DirectorySecurity],
541+ # For directories, GetAccessControl() returns [System.Security.AccessControl.DirectorySecurity],
550542 # which is the same as Get-Acl returns.
551543 if ($system_params.skipFolderACL ) {
552544 $ht = @ {
553- Attributes = ($_.Attributes -split ' ,' | ForEach-Object { $h = $_.Trim () ; if ($h.Length -gt 0 ) { $h.Substring (0 , 1 ).Toupper() } }) -join ' '
545+ Attributes = ($_.Attributes -split ' ,' | ForEach-Object { $h = $_ ; if ($h.Length -gt 0 ) { $h.Substring (0 , 1 ).Toupper() } }) -join ' '
554546 Depth = $_.FullName.Substring ($path_with_backslash.length ).Split(' \' ).Count - 1
555547 InheritanceEnable = ' '
556548 Owner = ' '
@@ -560,7 +552,7 @@ function Idm-FoldersRead {
560552 $sd = $_.GetAccessControl ()
561553
562554 $ht = @ {
563- Attributes = ($_.Attributes -split ' ,' | ForEach-Object { $h = $_.Trim () ; if ($h.Length -gt 0 ) { $h.Substring (0 , 1 ).Toupper() } }) -join ' '
555+ Attributes = ($_.Attributes -split ' ,' | ForEach-Object { $h = $_ ; if ($h.Length -gt 0 ) { $h.Substring (0 , 1 ).Toupper() } }) -join ' '
564556 Depth = $_.FullName.Substring ($path_with_backslash.length ).Split(' \' ).Count - 1
565557 InheritanceEnable = $sd.AreAccessRulesProtected -eq $false
566558 Owner = $sd.GetOwner ($system_params.principal_type ).Value
@@ -697,7 +689,7 @@ function ConvertSystemParams {
697689
698690 $params.paths_spec = @ (
699691 $params.paths_spec.Split (' |' ) | ForEach-Object {
700- $value = $_.Trim ()
692+ $value = $_
701693 if ($value.length -eq 0 ) { return }
702694
703695 $p = $value.LastIndexOf (' :' )
@@ -720,7 +712,7 @@ function ConvertSystemParams {
720712
721713 $params.excludes = @ (
722714 $params.excludes.Split (' |' ) | ForEach-Object {
723- $value = $_.Trim ()
715+ $value = $_
724716 if ($value.length -eq 0 ) { return }
725717
726718 $value
@@ -987,3 +979,57 @@ function ModifyFileSecurityDescriptor {
987979 $rv
988980 }
989981}
982+
983+
984+
985+ function GetItemsWithDepth {
986+ param (
987+ [string ]$Path ,
988+ [int ]$Depth ,
989+ [array ]$Excludes
990+ )
991+
992+ # Helper function to recursively list items
993+ function InternalGetItems {
994+ param (
995+ [string ]$CurrentPath ,
996+ [int ]$CurrentDepth ,
997+ [array ]$Exclude
998+ )
999+
1000+ if ($CurrentDepth -ge 0 ) {
1001+ # Attempt to list the current directory's contents
1002+ $test = $CurrentPath
1003+ try {
1004+ Log debug " Reading $ ( $CurrentPath ) "
1005+ try { $items = Get-ChildItem - LiteralPath $CurrentPath - Directory - Force - ErrorAction Stop | ForEach-Object {
1006+ foreach ($exclude in $Excludes ) {
1007+ if ($_.FullName -ilike $exclude ) { return }
1008+ }
1009+ $_
1010+ } } catch {
1011+ $error = " Failed to access contents of: [$ ( $CurrentPath ) ] - $_ "
1012+ Log error $error
1013+ throw $error
1014+ }
1015+
1016+ # Output the items from the current directory
1017+ $items
1018+
1019+ # If the depth allows, recurse into subdirectories
1020+ if ($CurrentDepth -gt 0 ) {
1021+ foreach ($item in $items ) {
1022+ if ($item.PSIsContainer ) {
1023+ InternalGetItems - CurrentPath $item.FullName - CurrentDepth ($CurrentDepth - 1 ) - Exclude $Exclude
1024+ }
1025+ }
1026+ }
1027+ } catch {
1028+ throw $_
1029+ }
1030+ }
1031+ }
1032+
1033+ # Start the recursive listing from the initial path and depth
1034+ InternalGetItems - CurrentPath $Path - CurrentDepth $Depth
1035+ }
0 commit comments