PnP
PnP

Reputation: 3185

Display NIC Info

In the following code, the $ipAddress stores both the IPV4 and IPV6. I only want the IPV4 displayed, is there anyway this can be done? Maybe with a split?

Also, the subnet mask prints 255.255.255.0 64 - where is this rogue 64 coming from?

Code:

ForEach($NIC in $env:computername) {
    $intIndex = 1
    $NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
    $caption = $NICInfo.Description 
    $ipAddress = $NICInfo.IPAddress
    $ipSubnet = $NICInfo.IpSubnet 
    $ipGateWay = $NICInfo.DefaultIPGateway 
    $macAddress = $NICInfo.MACAddress 
    Write-Host "Interface Name: $caption"
    Write-Host "IP Addresses: $ipAddress" 
    Write-Host "Subnet Mask: $ipSubnet"
    Write-Host "Default Gateway: $ipGateway"
    Write-Host "MAC: $macAddress"
    $intIndex += 1
}

Upvotes: 1

Views: 1074

Answers (1)

Kyle Muir
Kyle Muir

Reputation: 3925

Subnets work differently for IPv6, so the rogue 64 you are seeing is the IPv6's subnet mask - not the IPv4's.

The prefix-length in IPv6 is the equivalent of the subnet mask in IPv4. However, rather than being expressed in 4 octets like it is in IPv4, it is expressed as an integer between 1-128. For example: 2001:db8:abcd:0012::0/64

See here: http://publib.boulder.ibm.com/infocenter/ts3500tl/v1r0/index.jsp?topic=%2Fcom.ibm.storage.ts3500.doc%2Fopg_3584_IPv4_IPv6_prefix_subnet_mask.html

In order to remove it you can try the following (massive assumption made that IPv4 always comes first, but in all my experimenting it hasn't come second yet ;))

ForEach($NIC in $env:computername) {
    $intIndex = 1
    $NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
    $caption = $NICInfo.Description
    #Only interested in the first IP Address - the IPv4 Address
    $ipAddress = $NICInfo.IPAddress[0]
    #Only interested in the first IP Subnet - the IPv4 Subnet    
    $ipSubnet = $NICInfo.IpSubnet[0] 
    $ipGateWay = $NICInfo.DefaultIPGateway 
    $macAddress = $NICInfo.MACAddress 
    Write-Host "Interface Name: $caption"
    Write-Host "IP Addresses: $ipAddress" 
    Write-Host "Subnet Mask: $ipSubnet"
    Write-Host "Default Gateway: $ipGateway"
    Write-Host "MAC: $macAddress"
    $intIndex += 1
}

Hope this helps!

Upvotes: 3

Related Questions