Powershell将json输出转换为小写
我使用下面的命令生成下面的输出Powershell将json输出转换为小写,powershell,Powershell,我使用下面的命令生成下面的输出 $VMHost | ConvertTo-json | Out-File -encoding "UTF8" -FilePath ".\$VMHostName.report" 但我需要所有的小写键和值,如下所示 "HostNumaStatus": [ { "ComputerName": "TEMSA10", "MemoryAvailable": 3119, "MemoryTotal": 6075,
$VMHost | ConvertTo-json | Out-File -encoding "UTF8" -FilePath ".\$VMHostName.report"
但我需要所有的小写键和值,如下所示
"HostNumaStatus": [
{
"ComputerName": "TEMSA10",
"MemoryAvailable": 3119,
"MemoryTotal": 6075,
"NodeId": 0,
"ProcessorsAvailability": "35 41 56 58"
}
]
到
将其放在$VMHost和converttojson之间
Select -Property @{N='computername';E={$_.ComputerName}}, @{N='memoryavailable';E={$_.MemoryAvailable}}, @{N='memorytotal';E={$_.MemoryTotal}}, @{N='nodeid';E={$_.NodeId}}, @{N='processorsavailability';E={$_.ProcessorsAvailability}}
由于列不是静态的,请尝试以下操作:
$cols = $VMHost | select * | Get-Member | ForEach-Object {@{N=$_.Name.ToLower();E=$_.Name}}
$VMHost | Select -Property $cols | ConvertTo-Json | Out-File -encoding "UTF8" -FilePath ".\$VMHostName.report"
我真的很想在转换之前编辑对象,但这会让您得到您想要的最终结果:
# Get the JSON text
$JSON = $VMHost | ConvertTo-Json
# Loop through each line of the JSON output
$JSON.Split("`n") | ForEach-Object {
# Split the line on the ":", grab the first portion, and trim the space
$value = $_.Split(":")[0].Trim()
# Check to see if both the start and end characters are quotes (these should be the key fields)
if (($value.Substring(0,1) -eq "`"") -and ($value.Substring($value.Length-1,1) -eq "`"")) {
# If it's a key make it lowercase
$_.Replace($value,$value.ToLower())
} else {
# Otherwise leave it as-is
$_
}
# Output
} | Out-File -encoding "UTF8" -FilePath ".\$VMHostName.report"
将其放在$VMHost和converttojson之间
Select -Property @{N='computername';E={$_.ComputerName}}, @{N='memoryavailable';E={$_.MemoryAvailable}}, @{N='memorytotal';E={$_.MemoryTotal}}, @{N='nodeid';E={$_.NodeId}}, @{N='processorsavailability';E={$_.ProcessorsAvailability}}
由于列不是静态的,请尝试以下操作:
$cols = $VMHost | select * | Get-Member | ForEach-Object {@{N=$_.Name.ToLower();E=$_.Name}}
$VMHost | Select -Property $cols | ConvertTo-Json | Out-File -encoding "UTF8" -FilePath ".\$VMHostName.report"
我真的很想在转换之前编辑对象,但这会让您得到您想要的最终结果:
# Get the JSON text
$JSON = $VMHost | ConvertTo-Json
# Loop through each line of the JSON output
$JSON.Split("`n") | ForEach-Object {
# Split the line on the ":", grab the first portion, and trim the space
$value = $_.Split(":")[0].Trim()
# Check to see if both the start and end characters are quotes (these should be the key fields)
if (($value.Substring(0,1) -eq "`"") -and ($value.Substring($value.Length-1,1) -eq "`"")) {
# If it's a key make it lowercase
$_.Replace($value,$value.ToLower())
} else {
# Otherwise leave it as-is
$_
}
# Output
} | Out-File -encoding "UTF8" -FilePath ".\$VMHostName.report"
这不是最好的解决方案,而是等待更好解决方案的拐杖
# Get the JSON text
$JSON = $VMHost | ConvertTo-Json
$JSON.Split("`n") | % {if($_ -match "(.*):(.*)"){$_ -replace '(.*):(.*)',"$($matches[1].ToLower()):$($matches[2])"}else{$_}}
我使用正则表达式将所有键转换为小写,这必须使用各种JSON进行测试
也许有人可以提供更好的语法。这不是最好的解决方案,而是等待更好语法的拐杖
# Get the JSON text
$JSON = $VMHost | ConvertTo-Json
$JSON.Split("`n") | % {if($_ -match "(.*):(.*)"){$_ -replace '(.*):(.*)',"$($matches[1].ToLower()):$($matches[2])"}else{$_}}
我使用正则表达式将所有键转换为小写,这必须使用各种JSON进行测试
也许有人能提供更好的语法。我会使用
[Regex]
静态方法替换:
$Json = $VMHost | ConvertTo-Json
[regex]::Replace(
$Json,
'(?<=")(\w+)(?=":)',
{
$args[0].Groups[1].Value.ToLower()
}
)
$Json=$VMHost |转换为Json
[regex]::替换(
$Json,
“(?我将使用[Regex]
静态方法替换
:
$Json = $VMHost | ConvertTo-Json
[regex]::Replace(
$Json,
'(?<=")(\w+)(?=":)',
{
$args[0].Groups[1].Value.ToLower()
}
)
$Json=$VMHost |转换为Json
[regex]::替换(
$Json,
“(?这对我来说很有效..希望这有帮助
$data | ConvertTo-Json |Out-File "jsonfile.json"
(Get-Content -Path ".\jsonfile.json" -Raw).ToLower() |Out-File "jsonfile.json" -force -Encoding "UTF8"
这对我很管用希望这对我有帮助
$data | ConvertTo-Json |Out-File "jsonfile.json"
(Get-Content -Path ".\jsonfile.json" -Raw).ToLower() |Out-File "jsonfile.json" -force -Encoding "UTF8"
谢谢Tim的回答,但我需要将整个输出(仅键)转换为smallcase。这只是从输出文件中提取的。我有一种感觉就是这样。尝试第二个选项。Tim,它对父对象(例如hostnumastatus)有效,但对子对象无效。“hostnumastatus”:[{“ComputerName”:“TEMSA10”,“MemoryAvailable”:3119,“MemoryTotal”:6075,“NodeId”:0,“ProcessorAvailability”:“35 41 56”}]+1,因为我使用正则表达式参与您的解决方案。感谢Tim的回答,但我需要转换整个输出(仅键)进入smallcase。这只是输出文件的摘录。我有一种感觉就是这样。尝试第二个选项。Tim,它对父对象(例如hostnumastatus)有效,但对子对象无效。“hostnumastatus”:[{“ComputerName”:“TEMSA10”,“MemoryAvailable”:3119,“MemoryTotal”“:6075,“NodeId”:0,“ProcessorsAvailability”:“35 41 56”}]+1,因为我使用正则表达式参与您的解决方案。