Exploring the Powershell with Netsh and IPv6 (Part Two)
In the previous post I scripted an interface, based on the command Netsh Interface IPv6 show interfaces. But that display showed very little information. The command Netsh Interface IPv6 show interface <interfacename> shows some more information. So I changed the NetshInterface_v2.ps1 script.
# Powershell Script
# Ton Werner
#
############################################################################################
#
# Create an (Array of) Objects from : "netsh interface ipv6 show interfaces"
#
# Create an (Array of) Objects from : "netsh ipv6 interface show interface <interfacename>"
# where <interfacename> is the $MyGlobalInterfaces.Name property.
#
# Those arrays have a global scope
# so can be used in the Powershell console after running this file
#
########################################################################## part one ########
$global:MyGlobalInterfaces = @()
$global:interface = netsh interface ipv6 show interfaces
$global:property=[regex]::replace($interface[1],'[ ]+'," ").Split(" ")
for ( $i = 3 ; $i -lt ($interface.count - 1) ;$i++ ) {
$MyInterface = New-Object PSObject
$global:content = [regex]::replace($interface[$i],'[ ]+'," ").Split(" ")
for ( $j = 0 ; $j -lt 5 ; $j++ ) {
$global:thiscontent = $content[$j+1]
if ( $j -eq 4 ) {
$idx = $interface[$i].indexOf($content[($j + 1)])
$global:thiscontent = $interface[$i].substring($idx)
}
$MyInterface | Add-Member NoteProperty $property[$j] $thiscontent
}
$global:MyGlobalInterfaces = $MyGlobalInterfaces + @($MyInterface)
}
########################################################################## part two ########
$global:MyDetailedInterfaces = @()
foreach ( $object in $MyGlobalInterfaces ) {
$MyObject = New-Object PSObject
$ipv6 = netsh interface ipv6 show interface $object.idx
for ( $i = 3 ; $i -lt ( $ipv6.count - 1 ) ; $i++ ) {
$content= $ipv6[$i].Split(":")
$MyObject | Add-Member NoteProperty $content[0].Trim() $content[1].Trim()
}
$global:MyDetailedInterfaces = $MyDetailedInterfaces + @($MyObject)
}
################################################################################## end of script
What I really do like is the New-Object PSObject command. That gives me the opportunity to collect information, to compose my own objects and to proceed with those specific objects. The string handling is still clumsy, but that will improve in time.
Also the graphical interface NetshInterfaces_GUI_v2.ps1 is rewritten to show the extended information. All properties and values are derived from the object collections. What pleases me is the way the object collections can be used an manipulated with just a few code lines.
#
# Powershell Script
# Ton Werner
#
############################################################################################
#
# Simple window. Shows the ipv6 interfaces on the local engine.
# from : "netsh interface ipv6 show interfaces"
# from : "netsh interface ipv6 show interface <interfacename>"
#
############################################################################################
Function Get_LabelNames {
$global:MyGlobalLabelNames= CreateLabelNames $MyGlobalInterfaces
$global:MyDetailedLabelNames = CreateLabelNames $MyDetailedInterfaces
}
Function CreateLabelNames {
param($object)
$MyLabelNames = @()
$content = $object | gm -type Noteproperty
for ( $i = 0 ; $i -lt $content.count ; $i++ )
{ $MyLabelNames = $MyLabelNames + @($content[$i].Name) }
return $MyLabelNames
}
Function Get_labels {
$global:MyDetailedLabels = CreateLabels $MyDetailedLabelNames 20 120 200 20
$global:MyGlobalLabels = CreateLabels $MyGlobalLabelNames 300 0 40 20
}
Function CreateLabels {
param($listofNames, $x, $y, $w , $h)
$labels = @()
for ( $i = 0 ; $i -lt $listofNames.count ; $i++ ) {
$labels = $labels + @(Createlabel $listofNames[$i] $x ($y = $y + 20) $w $h)
}
return $Labels
}
Function Get_TextBoxes {
$global:TextBoxesDetailed= CreateTextBoxes $MyDetailedLabelNames 300 120 100 20
$global:TextBoxesGlobal = CreateTextBoxes $MyGlobalLabelNames 400 0 160 20
}
Function CreateTextBoxes {
param($listofNames, $x, $y, $w, $h)
$textboxes = @()
for ( $i = 0 ; $i -lt $listofNames.count ; $i++ ) {
$textboxes = $textboxes + @(Createtextbox "" $x ($y = $y + 20) $w $h)
}
return $textboxes
}
Function ShowTextBoxesDetailed{
$global:id = $thisobject.idx
$object = $MyDetailedInterfaces | where { $_.IfIndex -eq $id }
for ( $i = 0 ; $i -lt $MyDetailedLabelNames.count ; $i++ ) {
$TextBoxesDetailed[$i].Text = $object.($MyDetailedLabelNames[$i])
}
}
Function ShowTextBoxesGlobal {
$global:thisobject = $MyGlobalInterfaces[$ListBoxGlobal.selectedindex]
$content = $thisobject | gm -type Noteproperty
for ( $i = 0 ; $i -lt $content.count ; $i++ ) {
$TextBoxesGlobal[$i].Text = $thisobject.($content[$i].Name)
}
}
Function ShowListBoxGlobal {
$ListBoxGlobal.Items.Clear()
for ( $i = 0 ; $i -lt $MyGlobalInterfaces.count ; $i++ ) {
$ListBoxGlobal.Items.Add($MyGlobalInterfaces[$i].Name)
}
}
############################################################################################ Main
. ./Function_Forms_v2.ps1
./NetshInterfaces_v2.ps1
CreateForm "Netsh Interfaces" 600 700
$global:ListBoxGlobal = CreateListBox 10 20 200 60
$ListBoxGlobal.add_click({ ShowTextBoxesGlobal ; ShowTextBoxesDetailed})
Get_LabelNames ; Get_labels
Get_TextBoxes
ShowListBoxGlobal
ShowForm
############################################################################################ End of Script
This example acts only on the local engine. But the purpose is to work with IPv6 in the test-environment. So next blog I would like to take a look at the IPv6 adapter-configurations on the virtual machines in the test-environment with WSMAN.
You can find the Powershell Scripts in the file NetshInterfaces_v2 in the download section.