2017-09-14 68 views
0

需要获取有关网络接口的一些信息:转换PowerShell的对象JSON,在特定的IP地址

> 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" 
    } 
] 

还需要获取接口状态连接或断开,它存储在NetAdapter.Status。 请帮忙。最好把它写在一行中。

回答

0

不在一条线......

$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" 
    } 
] 

我相信,有更多的短,可能是最佳的方式

2

您需要遍历更深的对象:

Get-NetIPConfiguration | select IPv4Address, InterfaceAlias, InterfaceDescription | 
    ConvertTo-Json -Depth 5 

默认情况下PowerShell在只有3个层次深

+0

谢谢你,这似乎是接近,但不是我所寻找的。 – kyb