Search code examples
jsonpowershellnetwork-programmingipv4

Convert PowerShell object to JSON, in particular IP address


Need to get some information about network interfaces:

> Get-NetIPConfiguration | select IPv4Address, InterfaceAlias, InterfaceDes    cription | ConvertTo-Json

Respond

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

This look like strange enough. I expect something like:

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

Also need to get interface status connected or disconnected, it is stored in NetAdapter.Status. Please help. It shold be better to write it all in one line.


Solution

  • Not in one line...

    $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
    

    Result:

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

    I am sure, there is more short and may be optimal way