你的问题对我来说缺乏清晰度。
根据您的帖子,我的理解是:
- 您想遍历
$topMgFolderPath
作为根文件夹的文件夹列表。
- 你
-Recurse
在你的代码示例中使用了所以我假设你想一直沿着树走
- 根据您的第二个代码行结尾
| should -BeNullOrEmpty
,如果有任何不符合您的条件的内容,您希望触发 Pester 测试失败。
- 您希望文件夹有一个
rbac.json
文件,在这种情况下,该文件夹中的所有子文件夹将被任何进一步处理忽略,或者至少 1 个子文件夹本身包含一个 rbac.json 文件或多个子文件夹,这些子文件夹将导致这样的文件。
- 在所有情况下,
.policy
文件夹都将被忽略
如果我的前提不正确,请用更多细节更新您的问题或澄清情况。
无论如何,你想要做的事情是可能的,但不是通过一个单一的Get-ChildItem
陈述。
我的做法是,因为您想要一个带有多个检查的递归操作,一旦它被验证就停止处理一个文件夹,这是一个自制的,-recurse
通过一个单层Get-ChildItem
与一个队列一起完成,其中递归是“手动完成的” ",在一个while循环中一次一层,一直持续到队列被清除。
$topMgFolderPath = 'C:\temp\'
$queue = [System.Collections.Queue]::new()
$InvalidFolders = [System.Collections.Generic.List[PSobject]]::new()
$Directories = Get-ChildItem -path $topMgFolderPath -Exclude '.policy' -Directory
$Directories | % { $queue.Enqueue($_.FullName) }
while ($Queue.Count -gt 0) {
$dir = $queue.Dequeue()
$HasRbacFile = Test-Path -Path "$dir\rbac.json"
# If Rbac file exist, we stop processing this item
if ($HasRbacFile) { Continue }
$SubDirectories = Get-ChildItem -Path $dir -Directory -Exclude '.policy'
# If the rbac file was not found and no subfolders exist, then it is invalid
if ($SubDirectories.count -eq 0) {
$InvalidFolders.Add($dir)
} else {
# Subdirectories found are enqueued so we can check them
$SubDirectories | % {$queue.Enqueue($_.FullName)}
}
}
# Based on your second line of code where you performed that validation.
$InvalidFolders | should -BeNullOrEmpty
所有重要的事情都发生在主要逻辑的while循环中
- 有没有rbac文件?
- 如果没有,是否有要检查的子文件夹(不是 .policy)?
参考
队列类