我正在嘗試Get-NetIPConfiguration在 Python 中決議 PowerShell 的結果。
結果包含我想要的默認格式 (Format-List) 中的值,但在轉換為 JSON 時不包含,這是我想要使用的格式。
請注意如何DNSServer序列化Format-List:
PS C:\Users\BoppreH> Get-NetIPConfiguration | Format-List
InterfaceAlias : VirtualBox Host-Only Network
InterfaceIndex : 23
InterfaceDescription : VirtualBox Host-Only Ethernet Adapter
IPv4Address : 192.168.56.1
IPv6DefaultGateway :
IPv4DefaultGateway :
DNSServer : fec0:0:0:ffff::1
fec0:0:0:ffff::2
fec0:0:0:ffff::3
[...]
whileConvertTo-Json -Depth 1以不同的方式序列化 DNSServer 屬性(以完全無用的方式):
PS C:\Users\BoppreH> Get-NetIPConfiguration | ConvertTo-Json -Depth 1
[
{
"Detailed": false,
"ComputerName": "BOPPREH-DESKTOP",
"InterfaceAlias": "VirtualBox Host-Only Network",
"InterfaceIndex": 23,
"InterfaceDescription": "VirtualBox Host-Only Ethernet Adapter",
"CompartmentId": 1,
"NetAdapter": "MSFT_NetAdapter (CreationClassName = \"MSFT_NetAdapter\", DeviceID = \"{EAF79493-7C78-44D2-ADB4-F3EF196D2F49}\", SystemCreationClassName = \"CIM_NetworkPort\", SystemName = \"boppreh-desktop\")",
"NetCompartment": "MSFT_NetCompartment (InstanceID = \";55;\")",
"NetIPv6Interface": "MSFT_NetIPInterface (Name = \"??55??55;\", CreationClassName = \"\", SystemCreationClassName = \"\", SystemName = \"\")",
"NetIPv4Interface": "MSFT_NetIPInterface (Name = \"??55?55;\", CreationClassName = \"\", SystemCreationClassName = \"\", SystemName = \"\")",
"NetProfile": null,
"AllIPAddresses": "192.168.56.1 fe80::d83f:9609:86ff:2b57#",
"IPv6Address": "",
"IPv6TemporaryAddress": "",
"IPv6LinkLocalAddress": "fe80::d83f:9609:86ff:2b57#",
"IPv4Address": "192.168.56.1",
"IPv6DefaultGateway": null,
"IPv4DefaultGateway": null,
"DNSServer": "MSFT_DNSClientServerAddress (Name = \"23\", CreationClassName = \"\", SystemCreationClassName = \"\", SystemName = \"23\") MSFT_DNSClientServerAddress (Name = \"23\", CreationClassName = \"\", SystemCreationClassName = \"\", SystemName = \"2\")"
},
[...]
直到深度級別 4,地址才變得可見,但到那時輸出會大幾倍并且更難導航。
我目前的替代方法是將結果輸入管道Select-Object并使用計算的屬性來自己轉換值(在 DNSServer 的情況下,它是$_.DNSServer.ServerAddresses -join " "),但這對每個屬性來說都很麻煩,并且會使其他屬性的序列化也不同。
如何強制 JSON 序列化程式像串列格式化程式一樣格式化值?
uj5u.com熱心網友回復:
注意如何
DNSServer序列化Format-List
該Format-*cmdlet的不連載,他們生產的顯示字串表示,使用PowerShell的輸出格式化系統(而不是它的序列化基礎)。
這些表示不打算用于程式處理,但如果您確實想將它們作為字串處理,則可以將它們通過管道傳輸到Out-String:
# Returns a single-line string; add -Stream to get an array of lines.
# Add -Width to explicitly specify a line width (console window width is the default).
# Note: Since Format-List is used for formatting *by default*,
# you don't strictly need the Format-List here.
# Alternatively, use Format-Table for a *table* representation
$stringRep = Get-NetIPConfiguration | Format-List | Out-String
如何強制 JSON 序列化程式像串列格式化程式一樣格式化值?
您唯一的選擇確實是通過構建您自己的[pscustomobject]實體來簡化物件圖,例如通過Select-Object和計算屬性,或者在ForEach-Object帶有[pscustomobject] 文字的回圈中(例如
[pscustomobject] @{ foo = 'bar'; baz = 'quux' })。
例如:
Get-NetIPConfiguration | ForEach-Object {
[pscustomobject] @{
InterfaceAlias = $_.InterfaceAlias
InterfaceIndex = $_.InterfaceIndex
InterfaceDescription = $_.InterfaceDescription
'NetProfile.Name' = $_.NetProfile.Name
IPv4Address = $_.IPv4Address -join "`n"
IPv6DefaultGateway = $_.IPv6DefaultGateway.NextHop
IPv4DefaultGateway = $_.IPv4DefaultGateway.NextHop
}
}
管道以上ConvertTo-Json給出了一個合理的表示。
請注意,Get-NetIPConfiguration的格式化資料內置了額外的邏輯,可以根據配接器型別改變其顯示表示中的輸出欄位,上面沒有考慮到這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359705.html
