jsonpowershellobject-graph

PowerShell code returning correct variables. Enhance program by converting code output to string, concatenate to existing string


Good evening. Stack Overflow and Powershell and ObjectGraph novice here.

I am looking to enhance a program and append the output of a piece of PowerShell code into a string, then concatenate that string into $Item1_txt.Text = (


  1. Can you save the output of this PowerShell code into a string (Let's call it $apple ) based on this JSON and PowerShell code? I believe you need |Out-String and .Trim() somewhere in the pipeline but I am struggling


JSON Code:
  {
     "id": "4fd338f7-746b-420b-9d7e-b41709e9a641",
     "RD": {
   
       "PDR": []
     },
     "TimeStamp": "2025-02-14T16:55:20.1433467Z",
     "NNTl": {
       "CD": [
         {
           "EntityType": "Occurrence",
           "Characteristics": [
             "OFGE"
             ],
           "Cardinality": "Many"
         },
         {
           "EntityType": "Operator",
           "Characteristics": [
             "ADL",
             "EsRO"],
           "Cardinality": "Many"
         }
       ],
         "RRXC": true
     
       },
       "EntitiesData": {
       "Entities": [
         {
           "Id": "b0000000-0000-0000-0000-00000000000b",
           "EntityType": "IHUR",
           "Ordinal": 0,
           "Characteristics": {
             "PBIL": {
               "StringValue": "DDDD"
             }
           }
         },
         {
           "Id": "833e2a7f-c173-4062-9cd6-2196432e0001",
           "EntityType": "Operator",
           "Ordinal": 0,
           "Characteristics": {
             "FN": {
               "StringValue": "Da"
             },
              "RatedOI": {
               "BoolValue": true
             },
              "fdfdf": {
               "DDFDF": "SDFDS43R5"
             }
           },
           "EntityCreationDateTime": "2025-02-12T19:42:59.2852792Z"
         },
         {
           "Id": "6fe06340-072f-49b2-8d5a-449e3db80002",
           "EntityType": "Operator",
           "Characteristics": {
             "Deleted": {
               "StringValue": "0"
             },
                  
              "RatedOI": {
               "BoolValue": false 
              }
           }
                 }
         
       ]
     },
     "OR": "FFFFF01"
    } 

PowerShell Code:

   Set-Location C:\Users\A187515\Downloads\
   $file = Get-ChildItem -Path C:\Users\A187515\Downloads\ -Filter *.json | Sort-Object LastAccessTime -Descending | Select-Object -First 1
      (Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
          foreach ($entity in $_.EntitiesData.Entities) {
               if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -issnot [array] -and $entity.Characteristics.RatedOI) {
       $entity | Select-Object `
        id, 
        @{ Name = 'RatedOI'; Expression = { 
          $_.characteristics.RatedOI.boolValue } 
          }
            }
          }
    } 


This is linked to my question here: PowerShell code returning correct value. However, looking to enhance program by capturing the ID tag listed above the desired value


Solution

  • # ...
    # To *add to* a preexisting string stored in $apple, use += in lieu of =
    $apple =
      (Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
        foreach ($entity in $_.EntitiesData.Entities) {
          if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -isnot [array] -and $entity.Characteristics.RatedOI) {
            $entity | Select-Object `
              id, 
              @{ Name = 'RatedOI'; Expression = { 
                  $_.characteristics.RatedOI.boolValue } 
              }
          }
        }
      } | Out-String | ForEach-Object Trim