Need help to check if hash table are empty

Front end code:

                    $OULevel = Get-PSUCache -Key "ComputerOUCache" -Integrated
                    $Session:SelectOULvl1 = 1
                    $Session:SelectOULvl2 = 1
                    $Session:SelectOULvl3 = 1
                    New-UDGrid -Item -ExtraLargeSize 3 -LargeSize 3 -MediumSize 3 -SmallSize 3 -Children {
                        New-UDDynamic -Id 'OUPlacementLvl1' -content {
                            New-UDSelect -Id 'SelectOULvl1' -Label "Ange OU placering Level1 (*)" -FullWidth -Option {
                                New-UDSelectOption -Name "Välj..." -Value 1
                                foreach ($lvl1 in $OULevel.keys) {
                                    New-UDSelectOption -Name $lvl1 -Value $OULevel[$lvl1].DistinguishedName
                                }
                            } -DefaultValue $Session:SelectOULvl1 -OnChange {
                                Sync-UDElement -Id "OUPlacementLvl2"
                                Sync-UDElement -Id "OUPlacementLvl3"
                            }
                        } -LoadingComponent {
                            New-UDProgress -Circular
                        }
                    }
                    New-UDGrid -Item -ExtraLargeSize 1 -LargeSize 1 -MediumSize 1 -SmallSize 1 -Children { }
                    New-UDGrid -Item -ExtraLargeSize 3 -LargeSize 3 -MediumSize 3 -SmallSize 3 -Children {
                        New-UDDynamic -Id 'OUPlacementLvl2' -content {
                            New-UDSelect -Id 'SelectOULvl2' -Label "Ange OU placering Level2 (*)" -FullWidth -Option {
                                New-UDSelectOption -Name "Välj..." -Value 1
                                if ($Null -ne (Get-UDElement -Id "SelectOULvl1").value -or (Get-UDElement -Id "SelectOULvl1").value -ne 1) {
                                    $OUPlacementLvl1 = (Get-UDElement -Id "SelectOULvl1").value.Split(",").Replace("OU=", "")[0]
                                    foreach ($lvl2 in $OULevel[$OUPlacementLvl1].SubOU) {
                                        New-UDSelectOption -Name $lvl2.Name -Value $lvl2.DistinguishedName
                                    }
                                }
                            } -DefaultValue $Session:SelectOULvl2 -OnChange {
                                Sync-UDElement -Id "OUPlacementLvl3"
                            }
                        } -LoadingComponent {
                            New-UDProgress -Circular
                        }
                    }
                    New-UDGrid -Item -ExtraLargeSize 1 -LargeSize 1 -MediumSize 1 -SmallSize 1 -Children { }
                    New-UDGrid -Item -ExtraLargeSize 4 -LargeSize 4 -MediumSize 4 -SmallSize 4 -Children {
                        New-UDDynamic -Id 'OUPlacementLvl3' -content {
                            New-UDSelect -Id 'SelectOULvl3' -Label "Ange OU placering Level3 (*)" -FullWidth -Option {
                                New-UDSelectOption -Name "Välj..." -Value 1
                                if ($Null -ne (Get-UDElement -Id "SelectOULvl1").value -or (Get-UDElement -Id "SelectOULvl1").value -ne 1) {
                                    if ($Null -ne (Get-UDElement -Id "SelectOULvl2").value -or (Get-UDElement -Id "SelectOULvl2").value -ne 1) {
                                        $OUPlacementDisLvl2 = (Get-UDElement -Id "SelectOULvl2").value
                                        $OUPlacementLvl2 = (Get-UDElement -Id "SelectOULvl1").value.Split(",").Replace("OU=", "")[0]
                                        foreach ($lvl3 in ($OULevel[$OUPlacementLvl2].SubOU | Where-Object { $_.DistinguishedName -eq $OUPlacementDisLvl2 }).SubOU) {
                                            New-UDSelectOption -Name $lvl3.Name -Value $lvl3.DistinguishedName
                                        }
                                    }
                                }
                            } -DefaultValue $Session:SelectOULvl3
                        } -LoadingComponent {
                            New-UDProgress -Circular
                        }
                    }

Ok, so I have a hash table in the backend and if OU in “SelectOULvl2” are missing I get a error for the SelectOULvl3 Select and it don’t even show in the GUI.

So I was thinking that I somehow need to stop the foreach to run in each selection element if the hash table for that subOU is empty.

foreach ($lvl2 in $OULevel[$OUPlacementLvl1].SubOU) {
     New-UDSelectOption -Name $lvl2.Name -Value $lvl2.DistinguishedName
}

So basically it’s whit this line $OULevel[$OUPlacementLvl1].SubOU I’m collecting the SubOU but I can’t figure out how I can halt it if it’s empty. In plain PowerShell I can make it halt but not in PowerShell Universal and I don’t know why. @Adam do you know?

And here is the backend: ( you need to change the xx to your base ou path in $BaseOU)

    $BaseOU = xxx
    $OUInfo = @{}
    Get-ADOrganizationalUnit -Filter 'Name -like "*"' -SearchBase $BaseOU -SearchScope OneLevel | Foreach-Object {
        $OU = $_
        $subOU = Get-ADOrganizationalUnit -Filter 'Name -like "*"' -SearchScope OneLevel -SearchBase $_.DistinguishedName | Foreach-Object {
            $OU2 = $_
            $hasSubOU = $false
            if (($subOU2 = Get-ADOrganizationalUnit -Filter 'Name -like "*"' -SearchBase $_.DistinguishedName -SearchScope Subtree | Select-Object Name, DistinguishedName).Count -gt 1) {
                $hasSubOU = $true
            }
            [PSCustomObject]@{
                Name              = $_.Name
                DistinguishedName = $_.DistinguishedName
                HasSubOU          = $hasSubOU
                SubOU             = $subOU2 | Where-Object { $_.DistinguishedName -ne $OU2.DistinguishedName }
            }
        }
        $obj = [PSCustomObject]@{
            DistinguishedName = $_.DistinguishedName
            SubOU             = $subOU | Where-Object { $_.DistinguishedName -ne $OU.DistinguishedName }
        }
        $OUInfo.add($_.Name, $obj)
    }