PowerShell for Windows Admins

Nov 5 2011   5:56AM GMT

Working with IPv6



Posted by: Richard Siddaway
Tags:
Network
PowerShell v2
WMI

A comment was left on one of the articles in my recent series on working with the hosts file asking about using IPv6.  I’ve not used it really as I’ve never had any reason but it seems a good idea to dig into it a bit as IPv6 is enabled by default in Windows Vista and above.

Looking at my development machine

PS> Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.IPAddress} | fl Description, IPAddress, IPSubnet

Description : NVIDIA nForce 10/100/1000 Mbps Networking Controller
IPAddress   : {10.10.54.202, fe80::4547:ee51:7aac:521e}
IPSubnet    : {255.255.255.0, 64}

Description : Atheros AR5007 802.11b/g WiFi Adapter
IPAddress   : {192.168.1.2, fe80::6d95:b824:6a72:a0a9}
IPSubnet    : {255.255.255.0, 64}

 

There are two addresses defined for each adapter. The Ipv4 addresses should be recognisable as two of the private address spaces available in IPv4.

The two IPv6 addresses

fe80::4547:ee51:7aac:521e

fe80::6d95:b824:6a72:a0a9

are known as Link-Local addresses and are equivalent to the APIPA (169.254.x.x) addresses we get when adapters are configured to  get their address from a DHCP server and can’t find it.

IPv6 addresses are 128 bit addresses (IPv4 is 32bit) with each 16bit block converted to hexadecimal and separated by a colon ( : )  which should give us 8 blocks. But there are only 5 blocks in the addresses I found.  That because multiple contiguous blocks of 0000 can be replaced by ::  And just to add to the confusion we can drop leading 0’s on a block.

Time to expand the addresses to see what they really look like

 

function get-IPAddress {            
[CmdletBinding()]            
 param (            
  [string]$computer="$env:COMPUTERNAME"            
 )            
  Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $computer | where {$_.IPAddress} |             
  foreach {            
  $address = $_.IPAddress[1]            
              
  Write-Debug "Test for ::"            
  if ($_.IPAddress[1].Contains("::")){            
   $blocks = $_.IPAddress[1] -split ":"            
   $count = $blocks.Count            
   $replace = 8 - $count + 1            
   for ($i=0; $i -le $count-1; $i++){            
    if ($blocks[$i] -eq ""){            
      $blocks[$i] = ("0000:" * $replace).TrimEnd(":")            
    }            
   }            
   $address = $blocks -join ":"            
  }            
            
  Write-Debug "Check leading 0 in place"            
  $blocks = $address -split ":"            
  for ($i=0; $i -le $blocks.Count-1; $i++){            
    if ($blocks[$i].length -ne 4){            
      $blocks[$i] = $blocks[$i].Padleft(4,"0")            
    }            
   }            
   $address = $blocks -join ":"            
            
   $ipconfig = New-Object -TypeName PSObject -Property @{            
     Description = $($_.Description)            
     IPv4Address = $($_.IPAddress[0])            
     IPv4Subnet = $($_.IPSubnet[0])            
     IPv6Address = $address            
     IPv6Subnet = $($_.IPSubnet[1])            
   } | select Description, IPv4Address, IPv4Subnet, IPv6Address, IPv6Subnet            
   $ipconfig             
  }              
}

Start by using Get-WmiObject on the NetworkAdapterConfiguration class. We only want those adapters where an IP address has been configured.

For each adapter – the first part of the address and subnet is Ipv4 & the second is IPv6

Take the IPv6 address and if it contains “::” we know there are blocks missing.  Split the address on “:” and the “::” generates an empty string which we can replace with the relevant number of “0000” blocks.

Each block in the  address is then tested for length and any leading zeros that might be missing are added back in.

Join the blocks together to create a complete address and display

 

PS> get-IPAddress


Description : NVIDIA nForce 10/100/1000 Mbps Networking Controller

IPv4Address : 10.10.54.202

IPv4Subnet  : 255.255.255.0

IPv6Address : fe80:0000:0000:0000:4547:ee51:7aac:521e

IPv6Subnet  : 64

Description : Atheros AR5007 802.11b/g WiFi Adapter

IPv4Address : 192.168.1.2

IPv4Subnet  : 255.255.255.0

IPv6Address : fe80:0000:0000:0000:6d95:b824:6a72:a0a9

IPv6Subnet  : 64

 

Next time we will look at the IPv6 subnet and what it means

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: