0

I'm executing a program that is returning data in this format to the window:

Name              : Computername
ID                : number
Host Name         : Hostname
Added             : date
Online            : True
Uptime            : 3 hours
Boot Time         : time
Current User      : user
IP Address        : ip
MAC Address       : mac
Time Zone         : Eastern Standard Time

Operating System
Name              : Microsoft Windows 11 Enterprise Insider Preview
Version           : 10.0.22463.1000
SP / Release      : Dev
Installed         : time
Serial Numbe      : serial
System Drive      : C
PowerShell Version: 5.1.22463.1000
IE Version        : 11.1000.22463.0
Architecture      : 64-bit
.NET Versions     : 2.0 SP 2, 3.0 SP 2, 3.5 SP 1, 4.0 Client, 4.0 Full, 4.8 (or later)

Active Directory
Path              : AD path
Description       : desc
Domain            : domain
Created           : data

System
Manufacturer      : Dell Inc.
Model             : model
Memory            : 32 GB
Serial Number     : serial
BIOS Version      : version
BIOS Manufacturer : Dell Inc.
BIOS Asset Tag    : 00000000

When I get the type it is:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

I can parse by array like:

$computerdata[0]

But it that would return:

Name              : Computername

It stores each line in the array.

If I use:

$computerdata | ConvertTo-Json

I get:

[
  "Name              : Computername",
  "ID                : number",
  "Host Name         : hostname",
  "Added             : date",
  "Online            : True",
  "Uptime            : 3 hours",
  "Boot Time         : time",
  "Current User      : user",
  "IP Address        : ip",
  "MAC Address       : mac",
  "Time Zone         : Eastern Standard Time",
  "",
  "Operating System",
  "Name              : Microsoft Windows 11 Enterprise Insider Preview",
  "Version           : 10.0.22463.1000",
  "SP / Release      : Dev",
  "Installed         : time",
  "Serial Numbe      : serial",
  "System Drive      : C",
  "PowerShell Version: 5.1.22463.1000",
  "IE Version        : 11.1000.22463.0",
  "Architecture      : 64-bit",
  ".NET Versions     : 2.0 SP 2, 3.0 SP 2, 3.5 SP 1, 4.0 Client, 4.0 Full, 4.8 (or later)",
  "",
  "Active Directory",
  "Path              : AD path,
  "Description       : desc",
  "Domain            : domain",
  "Created           : data",
  "",
  "System",
  "Manufacturer      : Dell Inc.",
  "Model             : model",
  "Memory            : 32 GB",
  "Serial Number     : serial",
  "BIOS Version      : version",
  "BIOS Manufacturer : Dell Inc.",
  "BIOS Asset Tag    : 00000000"
]

From there I wasn't sure where to go because I can see that I'd still be grabbing the whole line.

How do you parse through this format of data or convert to a better formatted json?

1
  • Looks like the result of get-content. I would suggest parsing the text with -match or ConvertFrom-String into a pscustomobject, then export to json. There's a lot of questions about parsing logs. Sep 30, 2021 at 19:22

1 Answer 1

0

The best cmdlet for this is Get-ComputerInfo

You can then pipe that to the ConvertTo-Json cmdlet

Get-ComputerInfo | ConvertTo-Json

But if you need to gather your information from your app's output, you can redirect the output to a file, read it in with get-content, store it in a variable named output, split the data by the colon, trim your key-value pairs before storing them in a hashtable, and then convert it to a JSON object. similar to below...

$output = Get-Content -Path C:\output.txt
$hashObj = @{}
$hashObj2 = @{}
$category = "Host"
foreach($line in $output){
    if($line -cnotmatch ":" -and $line -ne " "){
        $category = $line
        $categoryChanged = $true
        $hashObj2 = @{}
    }
    if($line -cmatch ":"){
        $data = $line.Split(":")
        $key = $($data[0].trim(' '))
        $val = $($data[1].trim(' '))
        $hashObj2.Add("$key","$val")
        if(!($hashObj.ContainsKey($category))){
            $hashObj.Add("$category",$hashObj2)
        }
        else{
            $hashObj."$category".Add("$key","$val")
        }
        $hashObj2 = @{}
    }
}

$jsonObj = $hashObj | convertto-json
1
  • Maybe I should of mentioned that I'm a newbie. Haven't learned about hashes yet :) But this worked great! What I did at the end is convert back from Json and then I can parse it the way I'm familiar with. Thank you!
    – cosmis
    Oct 4, 2021 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.