代码之家  ›  专栏  ›  技术社区  ›  kyb thursdaysDove

将PowerShell对象转换为JSON,特别是IP地址

  •  0
  • kyb thursdaysDove  · 技术社区  · 7 年前

    > 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 请帮忙。最好把它写在一行里。

    2 回复  |  直到 7 年前
        1
  •  3
  •   4c74356b41    7 年前

    您需要更深入地遍历对象:

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

    默认情况下,powershell只有3层深度

        2
  •  1
  •   kyb thursdaysDove    7 年前

    不是一行。。。

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

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

    推荐文章