Powershell I can not figure out how to process several hash-tables to desired output with help of inlayed foreach cycles

84 Views Asked by At

I have next data structure I can not change. I try to "parce" it with foreach cycle

$Data5 = [ordered]@{
    'LocalInfo' = [ordered]@{
        'Date' = '21.12.2024';
        'SysName' = 'Jopa213'
    };
    'Baseboard(1155)' = [ordered]@{
        'Manufacturer' = 'Asus';
        'Model' = 'P5BPro'; 'RAM Slots' = '4'
    };
    'CPU0' = [ordered]@{
        'Name' = 'Intel Core i-5 2500';
        'Cores/Threads' = '4/4';
    };
    'RAM(DDR3)' = [ordered]@{
        'SLOT01' = [ordered]@{
            'Name' = 'DIMM01'; 
            'Size' = '1024 Mb'; 
            'Speed' = '800 Mhz'
        };
        'SLOT02' = [ordered]@{
            'Name' = 'DIMM02'; 
            'Size' = '1024 Mb'; 
            'Speed' = '800 Mhz'
        };
        'SLOT03' = [ordered]@{
            'Name' = 'DIMM03'; 
            'Size' = '1024 Mb'; 
            'Speed' = '800 Mhz'
        };
        'SLOT04' = [ordered]@{
            'Name' = 'DIMM01'; 
            'Size' = '1024 Mb'; 
            'Speed' = '800 Mhz'
        };
    }
    'HDD01' = [ordered]@{
        'Bus(Type)' = '(SATA)HDD'; 
        'Name' = 'Toshiba 101'; 
        'Size' = '500 Gb'; 
        'Status' = 'Healty';
    };
    'HDD02' = [ordered]@{
        'Bus(Type)' = '(NVMe)SSD'; 
        'Name' = 'Samsung Evo'; 
        'Size' = '128 Gb'; 
        'Status' = 'Healty';
    };
    'GPUs' = [ordered]@{
        'GPU0' = [ordered]@{
            'Name' = 'nVIDIA Vanta 8Mb'; 
            'RAM' = '1024 Mb';
        };
        'GPU1' = [ordered]@{
            'Name' = 'ATI Radeon RX7'; 
            'RAM' = '2048 Mb';
        };
    };
    'NetCard01' = [ordered]@{
        Name = 'Realtek Jopa Edition 1Gb'; 
        MAC = '00:11:22:33:44:55'
    };
    'NetCard02' = [ordered]@{
        Name = 'Intel Bebra Edition 2Gb'; 
        MAC = '11:44:44:44:55:66'
    };
    'NetConnection01' = [ordered]@{'Name' = 'NetConnection01(Active)'; 
        'MAC' = '11:44:44:44:55:66'; 
        'IPAddress' = '192.168.0.32'; 
        'SubNetMask' = '255.255.255.0'; 
        'Gateway' = '192.168.0.1';
    };
    'NetConnection02' = [ordered]@{'Name' = 'NetConnection01(Active)'; 
        'MAC' = '11:44:44:44:55:66'; 
        'IPAddress' = '192.168.0.32'; 
        'SubNetMask' = '255.255.255.0'; 
        'Gateway' = '192.168.0.1';
    };
    OpertatingSystem = [ordered]@{
        'Name' = 'MacroHard Lindows 10.2'; 
        'Version' = '10.2.1923';
    };
}

$Open = "OpenRow"
$Close = "CloseRow"

foreach ($HashTable in $Data5) {
    foreach ($L1Key in $HashTable.Keys) {
        Write-host $Open $L1Key $Close
        foreach ($L2Key in $HashTable.$L1Key.Keys) {
            IF ($HashTable.$L1Key.$L2Key.GetType() -like "*String*") {
                Write-Host "OpenDiv" $HashTable.$L1Key.$L2Key "CloseDiv" 
            } ELSEIF ($HashTable.$L1Key.$L2Key.GetType() -like "*Dictionary*") { 
                Write-Host "OpenRow"
                foreach ($L3Key in $HashTable.$L1Key.$L2Key.Keys) {
                    Write-Host "OpenDiv" $L3Key "CloseDiv"
                }
                Write-Host "CloseRow"
            }
        }
    }
}

But the output clearly show that I do something wrong

OpenRow LocalInfo CloseRow
OpenDiv 21.12.2024 CloseDiv
OpenDiv Jopa213 CloseDiv
OpenRow Baseboard(1155) CloseRow
OpenDiv Asus CloseDiv
OpenDiv P5BPro CloseDiv
OpenDiv 4 CloseDiv
OpenRow CPU0 CloseRow
OpenDiv Intel Core i-5 2500 CloseDiv
OpenDiv 4/4 CloseDiv
OpenRow RAM(DDR3) CloseRow
OpenRow
OpenDiv Name CloseDiv
OpenDiv Size CloseDiv
OpenDiv Speed CloseDiv
CloseRow
OpenRow
OpenDiv Name CloseDiv
OpenDiv Size CloseDiv
OpenDiv Speed CloseDiv
CloseRow
OpenRow
OpenDiv Name CloseDiv
OpenDiv Size CloseDiv
OpenDiv Speed CloseDiv
CloseRow
OpenRow
OpenDiv Name CloseDiv
OpenDiv Size CloseDiv
OpenDiv Speed CloseDiv
CloseRow
OpenRow HDD01 CloseRow
OpenDiv (SATA)HDD CloseDiv
OpenDiv Toshiba 101 CloseDiv
OpenDiv 500 Gb CloseDiv
OpenDiv Healty CloseDiv
OpenRow HDD02 CloseRow
OpenDiv (NVMe)SSD CloseDiv
OpenDiv Samsung Evo CloseDiv
OpenDiv 128 Gb CloseDiv
OpenDiv Healty CloseDiv
OpenRow GPUs CloseRow
OpenRow
OpenDiv Name CloseDiv
OpenDiv RAM CloseDiv
CloseRow
OpenRow
OpenDiv Name CloseDiv
OpenDiv RAM CloseDiv
CloseRow
OpenRow NetCard01 CloseRow
OpenDiv Realtek Jopa Edition 1Gb CloseDiv
OpenDiv 00:11:22:33:44:55:66 CloseDiv
OpenRow NetCard02 CloseRow
OpenDiv Intel Bebra Edition 2Gb CloseDiv
OpenDiv 11:44:44:44:55:66 CloseDiv
OpenRow NetConnections CloseRow
OpenDiv NetConnection01(Active) CloseDiv
OpenDiv 11:44:44:44:55:66 CloseDiv
OpenDiv 192.168.0.32 CloseDiv
OpenDiv 255.255.255.0 CloseDiv
OpenDiv 192.168.0.1 CloseDiv
OpenRow OpertatingSystem CloseRow
OpenDiv MacroHard Lindows 10.2 CloseDiv
OpenDiv 10.2.1923 CloseDiv

Cause such parcing will lead to wrong html-markup. Because, for example, problem with this lines

OpenRow LocalInfo CloseRow
---Problem - two lines lover are not open with OpenRow
OpenDiv 21.12.2024 CloseDiv
OpenDiv Jopa213 CloseDiv
---Problem - Not Ecnlosed with CloseRow
OpenRow Baseboard(1155) CloseRow

Ufff. I hope there is a solution to this, cause otherwise I will have to change data structure.

1

There are 1 best solutions below

0
iRon On

You might take a look at the Object Graph Tools set I am building to ease the Object Graph iteration:

Install-Module -Name ObjectGraphTools

$Data5 | Get-ChildNode | Foreach-Object {
    "--- $($_.Name) ---"
    $ChildNodes = $_.ChildNodes
    if ($ChildNodes[0] -is [PSMapNode]) {
        $ChildNodes.foreach{
            $_.ChildNodes.Value -Join ' | '
        }
    }
    else { $ChildNodes.Value -Join ' | ' }
}

Result:

--- LocalInfo ---
21.12.2024 | Jopa213
--- Baseboard(1155) ---
Asus | P5BPro | 4
--- CPU0 ---
Intel Core i-5 2500 | 4/4
--- RAM(DDR3) ---
DIMM01 | 1024 Mb | 800 Mhz
DIMM02 | 1024 Mb | 800 Mhz
DIMM03 | 1024 Mb | 800 Mhz
DIMM01 | 1024 Mb | 800 Mhz
--- HDD01 ---
(SATA)HDD | Toshiba 101 | 500 Gb | Healty
--- HDD02 ---
(NVMe)SSD | Samsung Evo | 128 Gb | Healty
--- GPUs ---
nVIDIA Vanta 8Mb | 1024 Mb
ATI Radeon RX7 | 2048 Mb
--- NetCard01 ---
Realtek Jopa Edition 1Gb | 00:11:22:33:44:55
--- NetCard02 ---
Intel Bebra Edition 2Gb | 11:44:44:44:55:66
--- NetConnection01 ---
NetConnection01(Active) | 11:44:44:44:55:66 | 192.168.0.32 | 255.255.255.0 | 192.168.0.1
--- NetConnection02 ---
NetConnection01(Active) | 11:44:44:44:55:66 | 192.168.0.32 | 255.255.255.0 | 192.168.0.1
--- OpertatingSystem ---
MacroHard Lindows 10.2 | 10.2.1923