HULK
HULK

Reputation: 89

getting ip details in powershell

Am trying to get all the IPv4 address from ipconfig set on the server.

Thanks to Chris for sharing info/solutions on number of ways to get the ip details.

worked solution :

Get-WmiObject @params | Select-Object NetConnectionID, `
        @{Name='IPAddress'; Expression={
            $_.GetRelated('Win32_NetworkAdapterConfiguration') | ForEach-Object {
                [IPAddress[]]$_.IPAddress | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
            }
        }}

Upvotes: 0

Views: 2652

Answers (1)

Chris Dent
Chris Dent

Reputation: 4260

Further to my comments above, these examples use WMI to extract the interface name and IPv4 addresses.

There are different ways to derive this depending on what's most important. That is, pick the version with the most convenient filtering style.

Win32_NetworkAdapterConfiguration and IPEnabled = TRUE

Useful if you are interested in all IP-enabled interfaces.

Get-CimInstance Win32_NetworkAdapterConfiguration -Filter "IPEnabled='TRUE'" |
    Select-Object @{n='NetConnectionID';e={ ($_ | Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapter).NetConnectionID }},
                  @{n='IPAddress';e={ [IPAddress[]]$_.IPAddress | Where-Object AddressFamily -eq 'InterNetwork' }}

Win32_NetworkAdapter and a named interface

Perhaps best if you need to target an interface by name and get the IP.

Get-CimInstance Win32_NetworkAdapter -Filter "NetConnectionID='Ethernet'" |
    Select-Object NetConnectionID,
                @{n='IPAddress';e={
                    [IPAddress[]]($_ | Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration).IPAddress |
                        Where-Object AddressFamily -eq 'InterNetwork'
                }}

Get-WmiObject

Get-WmiObject has been superseded, but if you prefer the syntax of that, here's the first example again.

Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled='TRUE'" |
    Select-Object @{n='NetConnectionID';e={ $_.GetRelated("Win32_NetworkAdapter") | ForEach-Object { $_.NetConnectionID } }},
                  @{n='IPAddress';e={ [IPAddress[]]$_.IPAddress | Where-Object { $_.AddressFamily -eq 'InterNetwork' } }}

Using System.Net.NetworkInformation

Because WMI isn't the only way, this also provides direct access with very little work.

$Name = 'MGT'
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() |
    Where-Object Name -eq $Name |
    Select-Object Name,
            @{n='IPAddress';e={ $_.GetIPProperties().UnicastAddresses.Address |
                Where-Object AddressFamily -eq 'Internetwork'
            }}

Get-NetworkInterface function (PowerShell 2 compatible)

This function allows you to pick network interfaces by name so you can check the IP addresses. By default it returns information for all named interfaces.

function Get-NetworkInterface {
    param(
        [String]$Name = '*',

        [String]$ComputerName
    )

    $params = @{
        Class = 'Win32_NetworkAdapter'
    }
    if ($Name.IndexOf('*') -gt -1) {
        $Name = $Name -replace '\*', '%'
        $params.Add('Filter', "NetConnectionID LIKE '$Name'")
    } else {
        $params.Add('Filter', "NetConnectionID='$Name'")
    }
    if ($ComputerName -ne '') {
        $params.Add('ComputerName', $ComputerName)
    }

    Get-WmiObject @params | Select-Object NetConnectionID, `
        @{Name='IPAddress'; Expression={
            $_.GetRelated('Win32_NetworkAdapterConfiguration') | ForEach-Object {
                [IPAddress[]]$_.IPAddress | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
            }
        }}
}

Operational validation

This part is not PowerShell 2 compatible. This is how you might leverage a unit testing framework, pester in this case, to perform configuration validation.

It shows how I imagine you might use the function above to validate IP configuration.

Import-Module Pester
Describe 'SomeServer' {
    $ComputerName = 'SomeServer'

    Context 'Network configuration' {
        It 'Should have the management interface set to 10.1.2.3' {
            (Get-NetworkInterface -Name 'MGMT' -ComputerName 'SomeServer').IPAddress | Should Be '10.1.2.3'
        }
    }
}

Upvotes: 1

Related Questions