Search code examples
powershellfor-loopforeachhashtable

How to print the contents of the objects taking one value as input in powershell?


I need to display the contents related to each id number by taking id as input. The original format was in json, as below:

{
    "ids": [
        {
            "id": "121100",
            "Libraries": [
                "cpa_sample_code_s.so",
                "stv_test_code_s.so"
            ],
            "Commands": [
                "qaeMemInit",
                "icp_sal_userStartMultiProcess(\"SSL\",CPA_FALSE)",
                "rsaPerformanceTest(1,0x02,2,10,1000) [RSA API]"
            ],
            "Label": "rsaPerformanceTest-Test"
        },
        {
            "id": "121103",
            "Libraries": [
                "cpa_sample_code_s.so",
                "stv_test_code_s.so"
            ],
            "Commands": [
                "qaeMemInit",
                "icp_sal_userStartMultiProcess(\"SSL\",CPA_FALSE)",
                "dhPerformanceTest(1,0x02,10,10000)"
            ],
            "Label": "dhPerformanceTest-Test"
        },
        {
            "id": "121202",
            "Libraries": [
                "cpa_sample_code_s.so",
                "stv_test_code_s.so"
            ],
            "Commands": [
                "qaeMemInit",
                "icp_sal_userStartMultiProcess(\"SSL\",CPA_FALSE)",
                "runDcTestPerf(3,0,2,1,1,1,65536,1,100)"
            ],
            "Label": "runDcTestPerf-Test"
        }
    ]
}

I converted the above format from a json file to the below mentioned format in $myVar. My variable has a hash table but I am unable to display the values using $myvar["id"]. I am very new to powershell. Can anyone please help?

$myFile = get-content C:\Users\ssc\Desktop\powershell\activity.json
$myvar = $myFile | ConvertFrom-Json
PS C:\Windows\system32> $myvar
ids                                                                                                                                                                                                                                                                                             
---                                                                                                                                                                                                                                                                                             
{@{id=121100; Libraries=System.Object[]; Commands=System.Object[]; Label=rsaPerformanceTest-Test}, @{id=121103; Libraries=System.Object[]; Commands=System.Object[]; Label=dhPerformanceTest-Test}, @{id=121202; Libraries=System.Object[]; Commands=System.Object[]; Label=runDcTestPerf-Test}}
PS C:\Windows\system32> 

Solution

  • $myvar.ids currently contains an array of objects - but you can populate your own hashtable, using the id property as the key, like this:

    $myHashtable = @{}
    $myvar.ids |ForEach-Object { $myHashtable[$_.id] = $_ }
    

    At which point you should be able to resolve each by id:

    PS ~> $myHashtable["121100"]
    
    id     Libraries                                  Commands
    --     ---------                                  --------
    121100 {cpa_sample_code_s.so, stv_test_code_s.so} {qaeMemInit, icp_sal_userStartMultiProcess("SSL",CPA_FALSE), ...}