将 PowerShell 对象转换为 JSON,特别是 IP 地址
Convert PowerShell object to JSON, in particular IP address
需要获取一些关于网络接口的信息:
> Get-NetIPConfiguration | select IPv4Address, InterfaceAlias, InterfaceDes cription | ConvertTo-Json
回复
[
{
"IPv4Address": [
"MSFT_NetIPAddress (Name = \";C?8;@B8CC8;??55;:55;55;\", CreationClassName = \"\", SystemCr
eationClassName = \"\", SystemName = \"\")"
],
"InterfaceAlias": "wifi",
"InterfaceDescription": "Qualcomm Atheros AR5BWB222 Wireless Network Adapter"
},
{
"IPv4Address": [
"MSFT_NetIPAddress (Name = \";@C8???8;??8??B55;@55;55;\", CreationClassName = \"\", SystemC
reationClassName = \"\", SystemName = \"\")"
],
"InterfaceAlias": "Ethernet",
"InterfaceDescription": "Realtek PCIe GBE Family Controller"
}
]
这看起来够奇怪的。我希望是这样的:
[
{
"IPv4Address": "12.3.3.4",
"InterfaceAlias": "wifi",
"InterfaceDescription": "Qualcomm Atheros AR5BWB222 Wireless Network Adapter"
"NetAdapter.Status" : "connected"
},
{
"IPv4Address": "192.168.0.1",
"InterfaceAlias": "Ethernet",
"InterfaceDescription": "Realtek PCIe GBE Family Controller"
"NetAdapter.Status" : "connected"
}
]
还需要获取接口状态connected或disconnected,它存储在NetAdapter.Status
。
请帮忙。最好写成一行
您需要更深入地遍历对象:
Get-NetIPConfiguration | select IPv4Address, InterfaceAlias, InterfaceDescription |
ConvertTo-Json -Depth 5
默认情况下 powershell 仅深入 3 级
不在一行中...
$n = Get-NetIPConfiguration | select InterfaceIndex, IPv4Address, InterfaceAlias, InterfaceDescription, NetAdapter
ForEach( $a in $n ){
$a.Ipv4Address = $a.Ipv4Address.IpAddress
$a | Add-Member -type NoteProperty -name Status -value $a.NetAdapter.Status
$a.PSObject.Properties.Remove('NetAdapter')
}
$n
$n | ConvertTo-Json
结果:
InterfaceIndex : 10
IPv4Address : 192.168.99.135
InterfaceAlias : wifi
InterfaceDescription : Qualcomm Atheros AR5BWB222 Wireless Network Adapter
Status : Up
InterfaceIndex : 16
IPv4Address : 169.254.153.248
InterfaceAlias : Ethernet
InterfaceDescription : Realtek PCIe GBE Family Controller
Status : Disconnected
[
{
"InterfaceIndex": 10,
"IPv4Address": "192.168.99.135",
"InterfaceAlias": "wifi",
"InterfaceDescription": "Qualcomm Atheros AR5BWB222 Wireless Network Adapter",
"Status": "Up"
},
{
"InterfaceIndex": 16,
"IPv4Address": "169.254.153.248",
"InterfaceAlias": "Ethernet",
"InterfaceDescription": "Realtek PCIe GBE Family Controller",
"Status": "Disconnected"
}
]
我相信,还有更短的可能是最佳方式
需要获取一些关于网络接口的信息:
> Get-NetIPConfiguration | select IPv4Address, InterfaceAlias, InterfaceDes cription | ConvertTo-Json
回复
[
{
"IPv4Address": [
"MSFT_NetIPAddress (Name = \";C?8;@B8CC8;??55;:55;55;\", CreationClassName = \"\", SystemCr
eationClassName = \"\", SystemName = \"\")"
],
"InterfaceAlias": "wifi",
"InterfaceDescription": "Qualcomm Atheros AR5BWB222 Wireless Network Adapter"
},
{
"IPv4Address": [
"MSFT_NetIPAddress (Name = \";@C8???8;??8??B55;@55;55;\", CreationClassName = \"\", SystemC
reationClassName = \"\", SystemName = \"\")"
],
"InterfaceAlias": "Ethernet",
"InterfaceDescription": "Realtek PCIe GBE Family Controller"
}
]
这看起来够奇怪的。我希望是这样的:
[
{
"IPv4Address": "12.3.3.4",
"InterfaceAlias": "wifi",
"InterfaceDescription": "Qualcomm Atheros AR5BWB222 Wireless Network Adapter"
"NetAdapter.Status" : "connected"
},
{
"IPv4Address": "192.168.0.1",
"InterfaceAlias": "Ethernet",
"InterfaceDescription": "Realtek PCIe GBE Family Controller"
"NetAdapter.Status" : "connected"
}
]
还需要获取接口状态connected或disconnected,它存储在NetAdapter.Status
。
请帮忙。最好写成一行
您需要更深入地遍历对象:
Get-NetIPConfiguration | select IPv4Address, InterfaceAlias, InterfaceDescription |
ConvertTo-Json -Depth 5
默认情况下 powershell 仅深入 3 级
不在一行中...
$n = Get-NetIPConfiguration | select InterfaceIndex, IPv4Address, InterfaceAlias, InterfaceDescription, NetAdapter
ForEach( $a in $n ){
$a.Ipv4Address = $a.Ipv4Address.IpAddress
$a | Add-Member -type NoteProperty -name Status -value $a.NetAdapter.Status
$a.PSObject.Properties.Remove('NetAdapter')
}
$n
$n | ConvertTo-Json
结果:
InterfaceIndex : 10
IPv4Address : 192.168.99.135
InterfaceAlias : wifi
InterfaceDescription : Qualcomm Atheros AR5BWB222 Wireless Network Adapter
Status : Up
InterfaceIndex : 16
IPv4Address : 169.254.153.248
InterfaceAlias : Ethernet
InterfaceDescription : Realtek PCIe GBE Family Controller
Status : Disconnected
[
{
"InterfaceIndex": 10,
"IPv4Address": "192.168.99.135",
"InterfaceAlias": "wifi",
"InterfaceDescription": "Qualcomm Atheros AR5BWB222 Wireless Network Adapter",
"Status": "Up"
},
{
"InterfaceIndex": 16,
"IPv4Address": "169.254.153.248",
"InterfaceAlias": "Ethernet",
"InterfaceDescription": "Realtek PCIe GBE Family Controller",
"Status": "Disconnected"
}
]
我相信,还有更短的可能是最佳方式