Search code examples
powershellpowershell-v5.1

Results in single string, need one per line


Trying to combine some properties from get-netadaptor with get-netipaddress

tried everything I know but the results of the variable $getnet-name are all on one line

${getnet-name} = New-Object psobject -Property @{
Status = $null
AdminStatus = $null
LinkLayerAddress = $null
}

${getnet-name}.Status = (Get-NetAdapter -Physical).Status
${getnet-name}.AdminStatus = (Get-NetAdapter -Physical).AdminStatus
${getnet-name}.LinkLayerAddress = (Get-NetAdapter -Physical).LinkLayerAddress

Get-NetAdapter | Get-NetIPAddress | ft interfacealias , ipaddress , prefixorigin ${getnet-name}.AdminStatus -AutoSize
PS C:\Users\username> ${getnet-name}

Status                               AdminStatus      LinkLayerAddress                                                            
------                               -----------      ----------------                                                            
{Disconnected, Up, Disconnected, Up} {Up, Up, Up, Up} {14-xF-8A-xC-73-00, 0x-50-B6-xx-F0-EB, 10-x5-30-x1-56-B7, 02-00-xC-4x-4F-50}

This is where I got stuck with looping

${getnet-name} = New-Object psobject -Property @{
Status = $null
AdminStatus = $null
LinkLayerAddress = $null
}

${getnet-name}.Status = (Get-NetAdapter).Status
${getnet-name}.AdminStatus = (Get-NetAdapter).AdminStatus
${getnet-name}.LinkLayerAddress = (Get-NetAdapter).LinkLayerAddress

${getnet-obj} = Get-NetAdapter -Physical | Select-Object -Property {${getnet-name}.AdminStatus} , {${getnet-name}.Status} , {${getnet-name}.LinkLayerAddress}

$getnetobj = Get-NetAdapter -Physical

foreach ($InterfaceIndex in $getnetobj) 
{
Get-NetIPAddress | ft interfacealias , ipaddress , prefixorigin , {${getnet-name}.AdminStatus} , {${getnet-name}.Status} , {${getnet-name}.LinkLayerAddress} -AutoSize
}

it repeats the same result per interface


hoping to eventually get this to also filter for IPv4 addresses only using the Get -AddressFamily filter


Currently working, but this really might not be a good way of doing this. Alternative solutions that are more elegant would be helpful if you would like to add them!

$results = @()

foreach ($adapter in (Get-NetAdapter -Physical))
{
    $ipaddresses = $adapter | Get-NetIPAddress -AddressFamily IPv4

    foreach ($ipaddress in $ipaddresses)
    {
        #build the object for reach record you want
        $netadapteraddress = New-Object psobject -Property @{
        Status = $null
        AdminStatus = $null
        LinkLayerAddress = $null
        InterfaceAlias = $null
        IPAddress = $null
        PrefixOrigin = $null
        }

        #properties for the adapter
        $netadapteraddress.Status = $adapter.Status
        $netadapteraddress.AdminStatus = $adapter.AdminStatus
        $netadapteraddress.LinkLayerAddress = $adapter.LinkLayerAddress

        #properties for the ipaddress
        $netadapteraddress.InterfaceAlias = $ipaddress.InterfaceAlias
        $netadapteraddress.IPAddress = $ipaddress.IPAddress
        $netadapteraddress.PrefixOrigin = $ipaddress.PrefixOrigin

        #add to results
        $results += $netadapteraddress
    }

}

$results | ft -wrap 

Thanks!


Solution

  • There might be a better way, but this is how I would go about it. Loop through all physical adapters, loop through each ipaddress for that adapter, create an object to hold the data you want, populate the data. add object to an array, return the array.

    $results = @()
    
    foreach ($adapter in (Get-NetAdapter -Physical))
    {
        $ipaddresses = $adapter | Get-NetIPAddress
    
        foreach ($ipaddress in $ipaddresses)
        {
            #build the object for reach record you want
            $netadapteraddress = New-Object psobject -Property @{
            Status = $null
            AdminStatus = $null
            LinkLayerAddress = $null
            InterfaceAlias = $null
            IPAddress = $null
            PrefixOrigin = $null
            }
    
            #properties for the adapter
            $netadapteraddress.Status = $adapter.Status
            $netadapteraddress.AdminStatus = $adapter.AdminStatus
            $netadapteraddress.LinkLayerAddress = $adapter.LinkLayerAddress
    
            #properties for the ipaddress
            $netadapteraddress.InterfaceAlias = $ipaddress.InterfaceAlias
            $netadapteraddress.IPAddress = $ipaddress.IPAddress
            $netadapteraddress.PrefixOrigin = $ipaddress.PrefixOrigin
    
            #add to results
            $results += $netadapteraddress
        }
    
    }
    
    $results | format-table -AutoSize