The inability to list a child directory's contents due to lack of permission correctly results in a (non-terminating) error when using Get-ChildItem -Recurse.
Simply adding -Name to change from outputting filesystem-info objects to strings unexpectedly causes these errors to be suppressed.
Steps to reproduce (on Windows)
Run the following Pester test:
Describe "Get-ChildItem: unlistable-directory test" {
BeforeAll {
Push-Location TestDrive:/
# Create a regular and an unlistable directory.
$null = New-Item -Type Directory someDir, someUnlistableDir
$acl = Get-Acl someUnlistableDir
$denyRule = New-Object System.Security.AccessControl.FileSystemAccessRule $env:USERNAME, ListDirectory, Deny
$acl.AddAccessRule($denyRule)
Set-Acl someUnlistableDir $acl
}
AfterAll {
# Make the unlistable directory listable again, so that removal of the temp. folder
# underlying TestDrive: doesn't fail.
$acl = Get-Acl someUnlistableDir
$acl.RemoveAccessRuleSpecific($denyRule)
Set-Acl someUnlistableDir $acl
Pop-Location
}
It "Get-ChildItem -Recurse complains about child directory that cannot be listed." {
(Get-ChildItem -Recurse -ErrorVariable err 2>$null).Name | Should -BeExactly someDir, someUnlistableDir
$err.Count | Should -Be 1
}
It "Get-ChildItem -Recurse -Name complains about child directory that cannot be listed." {
Get-ChildItem -Recurse -Name -ErrorVariable err 2>$null | Should -BeExactly someDir, someUnlistableDir
$err.Count | Should -Be 1
}
}
Expected behavior
Both tests should pass.
Actual behavior
Executing script .....Tests.ps1
Describing Get-ChildItem: unlistable-directory test
[+] Get-ChildItem -Recurse complains about child directory that cannot be listed. 797ms
[-] Get-ChildItem -Recurse -Name complains about child directory that cannot be listed. 161ms
Expected 1, but got 0.
27: $err.Count | Should -Be 1
That is, adding -Name caused the inability to list the contents of someUnlistableDir to be quietly ignored.
Environment data
PowerShell Core 6.2.0-rc.1
Windows PowerShell v5.1.17134.590
The inability to list a child directory's contents due to lack of permission correctly results in a (non-terminating) error when using
Get-ChildItem -Recurse.Simply adding
-Nameto change from outputting filesystem-info objects to strings unexpectedly causes these errors to be suppressed.Steps to reproduce (on Windows)
Run the following Pester test:
Expected behavior
Both tests should pass.
Actual behavior
That is, adding
-Namecaused the inability to list the contents ofsomeUnlistableDirto be quietly ignored.Environment data