File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1153,19 +1153,41 @@ function GetItemsWithDepth {
11531153
11541154 if ($CurrentDepth -ge 0 ) {
11551155 # Attempt to list the current directory's contents
1156- $test = $CurrentPath
11571156 try {
1158- Log debug " Reading $ ( $CurrentPath ) "
1159- try { $items = Get-ChildItem - LiteralPath $CurrentPath - Directory - Force - ErrorAction Stop | ForEach-Object {
1160- foreach ($exclude in $Excludes ) {
1161- if ($_.FullName -ilike $exclude ) { return }
1162- }
1163- $_
1164- } } catch {
1165- $errorMsg = " Failed to access contents of: [$ ( $CurrentPath ) ] - $ ( $_ ) "
1166- Log error $errorMsg
1157+ Log verbose " Reading $ ( $CurrentPath ) "
1158+ try {
1159+ $childItems = Get-ChildItem - LiteralPath $CurrentPath - Directory - Force - ErrorVariable gciErrors - ErrorAction SilentlyContinue
1160+
1161+ $items = @ ()
1162+ foreach ($item in $childItems ) {
1163+ $excludeMatch = $false
1164+ foreach ($exclude in $Excludes ) {
1165+ if ($item.FullName -ilike $exclude ) {
1166+ $excludeMatch = $true
1167+ break
1168+ }
1169+ }
1170+
1171+ if (-not $excludeMatch ) {
1172+ $items += $item
1173+ }
1174+ }
1175+
1176+ if ($gciErrors ) {
1177+ foreach ($err in $gciErrors ) {
1178+ if ($err.Exception -is [System.UnauthorizedAccessException ]) {
1179+ Log warning " Access denied to item in [$CurrentPath ]: $ ( $err.Exception.Message ) "
1180+ } else {
1181+ Log warning " Unexpected error accessing [$CurrentPath ]: $ ( $err.Exception.Message ) "
1182+ }
1183+ }
1184+ }
1185+
1186+ } catch {
1187+ $errorMsg = " Failed to access contents of: [$CurrentPath ] - $ ( $_.Exception.Message ) "
1188+ Log error $errorMsg
11671189 throw $errorMsg
1168- }
1190+ }
11691191
11701192 # Output the items from the current directory
11711193 $items
You can’t perform that action at this time.
0 commit comments