Home > Software design >  Powershell convert data to json for parsing
Powershell convert data to json for parsing

Time:10-05

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?

CodePudding user response:

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
  • Related