Powershell 将数据转换为 json 进行解析
Powershell convert data to json for parsing
我正在执行一个程序,该程序正在 return 将这种格式的数据发送到 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
当我得到类型时,它是:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
我可以像这样按数组解析:
$computerdata[0]
但这会 return:
Name : Computername
它将每一行存储在数组中。
如果我使用:
$computerdata | ConvertTo-Json
我得到:
[
"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"
]
从那里我不确定去哪里,因为我可以看到我仍然会抓住整条线。
如何解析这种格式的数据或转换成更好的格式json?
最好的 cmdlet 是 Get-ComputerInfo
然后您可以将其通过管道传输到 ConvertTo-Json
cmdlet
Get-ComputerInfo | ConvertTo-Json
但是如果您需要从应用程序的输出中收集信息,您可以将输出重定向到一个文件,使用 get-content
将其读入,将其存储在一个名为 output 的变量中,将数据拆分为冒号,trim 你的键值对,然后将它们存储在哈希表中,然后将其转换为 JSON 对象。类似于下面...
$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
我正在执行一个程序,该程序正在 return 将这种格式的数据发送到 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
当我得到类型时,它是:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
我可以像这样按数组解析:
$computerdata[0]
但这会 return:
Name : Computername
它将每一行存储在数组中。
如果我使用:
$computerdata | ConvertTo-Json
我得到:
[
"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"
]
从那里我不确定去哪里,因为我可以看到我仍然会抓住整条线。
如何解析这种格式的数据或转换成更好的格式json?
最好的 cmdlet 是 Get-ComputerInfo
然后您可以将其通过管道传输到 ConvertTo-Json
cmdlet
Get-ComputerInfo | ConvertTo-Json
但是如果您需要从应用程序的输出中收集信息,您可以将输出重定向到一个文件,使用 get-content
将其读入,将其存储在一个名为 output 的变量中,将数据拆分为冒号,trim 你的键值对,然后将它们存储在哈希表中,然后将其转换为 JSON 对象。类似于下面...
$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