0

I want to check if all folders contains either a subfolder or an rbac.jsonc file.

  • I want to iterate through a list of folder $topMgFolderPath being the root folder.
  • I want to go all the way down the tree in the folders
  • I am are looking to trigger a Pester test failure if there is anything that do not correspond to my condition.
  • I expect folders to have either a rbac.json file, in which case all the subfolders in that folder will be ignored from any further processing or at least 1 subfolder that will itself contains either a rbac.jsonc file or more subfolders that will lead down the line to such a file.
  • In all cases, .policy folder is to be ignored

is this somehow possible ?

Nadia Hansen
  • 697
  • 2
  • 6
  • 16
  • 1
    "it just need either to have a subfolder or the "rbac.jsonc" file to be valid." - so if the file is there along AND one or more subfolders is also present, then you're not interested in the folder? Is that correctly understood? – Mathias R. Jessen Nov 22 '21 at 10:20
  • Your question is not totally clear to me as it stand. Check out my answer and tag me if I misunderstood what you were going for. In any case, what you want is very possible but not through a simple call to `Get-ChildItem` – Sage Pourpre Nov 22 '21 at 11:57

1 Answers1

1

Your question lack some clarity for me as it stand.

Based on your post, what I understand is:

  • You want to iterate through a list of folder $topMgFolderPath being the root folder.
  • You used -Recurse in your code sample so I assumed you want to go all the way down the tree
  • Based on your second code line ending of | should -BeNullOrEmpty, you are looking to trigger a Pester test failure if there is anything that do not correspond to your condition.
  • You expect folders to have either a rbac.json file, in which case all the subfolders in that folder will be ignored from any further processing or at least 1 subfolder that will itself contains either a rbac.json file or more subfolders that will lead down the line to such a file.
  • In all cases, .policy folder is to be ignored

Please update your question with additional details or clarify the situation if I didn't get the premise right.

In any case, what you seek to do is possible but not through a single Get-ChildItem statement.

The way I'd go about it, since you want a recurse operation with multiple checks that stop processing a folder once it has been validated, is an home-made -recurse done through a single-layer Get-ChildItem alongside a queue where the recursion is done "manually", one layer at a time within a while loop that persist until the queue is cleared out.

$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

Everything important happens in the while loop where the main logic

  • Is there a rbac file ?
  • If not, is there any subfolders (not .policy) to check ?

References

Queue Class

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
  • Thank you @Sage seems like it is working and yes this is a pester test. do i have to write the loop again in the else statement or is it also checking them - for me it just looks like it queueing the subdirs, but I have never used the queue collection before so i am not sure how it works? – Nadia Hansen Nov 23 '21 at 09:52
  • 1
    @NadiaHansen I added a reference to ms doc for the queue. Simply, it is a first-in, first-out collection of objects. Oldest item put in is the first one going out. Only a single loop is added as we parse the main directory, then add subdir. to the queues, which are now treated by the queue and their subdir. get fetched until we either meet a condition where we want to stop processing the folder (so the subdir. do not get added to the queues) or we are at the bottom of the tree. – Sage Pourpre Nov 23 '21 at 10:08
  • 1
    You could totally replace the queue by a list and remove the item at index 0. That would have the same end result. The queue (First-in, First-Out) / stack (Last-In, First-out) are just more optimized for that kind of scenario. hence why I chose it. The most important is that you need a while loop and not a `foreach` because we are adding and removing stuff dynamically (while looping) from the collection. – Sage Pourpre Nov 23 '21 at 10:11