The goal of this scripting exercise was to get quick information about the IPv6 Adapterconfigurations in my test-domain. In these last scripts I’m using WSMAN.
Wsman takes care of the initiation of the remote object, in this case the adapterconfiguration. The data received is in XML format. All those configurations are collected in the $items collection.
#
# Powershell Script
# Ton Werner
#
############################################################################################
#
# get the (remote) wmi-object "win32_NetworkAdapterConfiguration"
#
############################################################################################ Main
param($Myengine)
$object = New-Object -ComObject "WSMAN.Automation"
$target = "http://" + $Myengine + "." + $MyDomain
$objectsession = $object.CreateSession($target)
$cimroot="http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/"
$target="win32_NetworkAdapterConfiguration"
$comobject = $cimroot + $target
$objectresource = $comobject
$objectresponse = $objectsession.Enumerate($objectresource)
$global:items = @()
While (!$objectresponse.AtEndOfStream) {
[XML]$xml = $objectresponse.ReadItem()
$item = $xml.Win32_NetworkAdapterConfiguration
$global:items = $items + @($item)
}
############################################################################################ End of Script
But at first I have to detect the domain computers, the hostname and the domain name. Thanks to Chrissy Lemaire I found the most simple solution for the hostname and the domainname. Powershell takes care of that and puts those names in the $env: environment.
For the domaincomputers I used the DirectorySearcher. It’ s one of the beautiful things of the Powershell that if you don’t know anything about an object, you just have to use Get-Member and you instantly know all you have to know about the object you are investigating.
So $DirectorySearcher | gm gives you a fair idea of what can be done with the searcher. I’ve put the AD functions in the Functions_AD.ps1 file.
function searchAdfor {
param($objectclass)
$Searcher = New-Object DirectoryServices.DirectorySearcher
$searcher.filter = "objectClass=" + $objectclass
$searcher.Searchscope = "Subtree"
$list = $Searcher.findall()
return $list
}
function getEnginesinDomain {
$list = @()
foreach ( $engine in ( SearchAdFor computer )) {
$list = $list + $engine.properties.name
}
return $list
}
The Wsman_AdapterConfig.ps1 file gathers the NetworkAdapterConfigurations in the $items collection,; the first time just to get the properties in order to create the labels in the form. In the script Wsman_AdapterConfig_GUI.ps1 I recall this script again with the selected engine as parameter to get the configurations and to fill in the correct values in the textboxes.
#
# Powershell Script
# Ton Werner
#
############################################################################################
#
# Simple window. Shows the ipv6 adapterconfiguration on remote engines.
# use wsman
#
############################################################################################
Function Create_listOfPropertyNames {
$list = @()
foreach ( $line in ( $items | gm -type Property ) ) {
$list = $list + @($line.name )
}
return $list
}
Function CreateListBoxes {
$global:ListBox_Engines = CreateListBox 10 20 100 60
$ListBox_Engines.Items.Clear()
for ( $i = 0 ; $i -lt $engines.count ; $i++ ) {
$ListBox_Engines.Items.Add($engines[$i])
}
$ListBox_Engines.add_click({ ActionOnSelectionEngines })
$global:ListBox_Adapters = CreateListBox 200 20 300 100
$ListBox_Adapters.add_click({ ActionOnAdapterSelection })
}
Function CreateLabels {
param($l, $x, $y)
$collection = @()
for ( $i = 0 ; $i -lt $l.count ; $i++ ) {
if ( $y -eq 560 ) { $x = $x + 320 ; $y = 140 }
$collection = $collection + @(Createlabel $l[$i] $x ($y = $y + 20))
}
return $collection
}
Function CreateTextBoxes {
param($l, $x, $y)
$collection = @()
for ( $i = 0 ; $i -lt $l.count ; $i++ ) {
if ( $y -eq 560 ) { $x = $x + 320 ; $y = 140 }
$collection = $collection + @(CreateTextBox "" $x ($y = $y + 20))
}
return $collection
}
Function Clean_TextBoxes {
for ( $i=0 ; $i -lt $listOfPropertyNames.Count ; $i ++ ) {
$Textboxes[$i].Text = ""
}
}
Function ActionOnAdapterSelection {
$item = $items | where { $_.Description -eq $ListBox_Adapters.SelectedItem }
for ( $i=0 ; $i -lt $listOfPropertyNames.Count ; $i ++ ) {
$Textboxes[$i].Text = $item.($listOfPropertyNames[$i]).ToString()
}
}
Function Check_Winrm {
$argument = "-remote:" + $ListBox_Engines.SelectedItem + "." + $MyDomain
$result = winrm id $argument 2>out-null
if ( $? -eq $true ) {return $true }
else { return $false}
}
Function ActionOnSelectionEngines {
$ListBox_Adapters.Items.Clear()
Clean_TextBoxes
$labelMachine.Text = $ListBox_Engines.SelectedItem
if ( ( Check_Winrm ) -eq $true ) {
$labelMessage.Text = "Winrm seems OK"
./Wsman_AdapterConfig.ps1 $ListBox_Engines.SelectedItem
for ( $i = 0 ; $i -lt $items.count ; $i++ ) {
if ( $items[$i].Ipenabled -eq "true" ) {
$ListBox_Adapters.Items.Add($items[$i].Description)
}
}
}
else { $labelMessage.Text = "Winrm seems not OK" }
}
############################################################################################ main
#
# get the functions
. ./Function_Forms.ps1
. ./Function_AD.ps1
# get the names
$global:MyDomain = $env:userdnsdomain
$global:LocalEngine = $env:computername
$global:engines = getEnginesInDomain
# get the labelnames
./Wsman_AdapterConfig.ps1 $LocalEngine
$global:listOfPropertyNames = Create_listOfPropertyNames
# create the form
CreateForm "NetworkAdapterConfiguration" 1200 700
CreateListBoxes
$global:LabelMachine = CreateLabel "Machine" 800 50
$global:LabelMessage = CreateLabel "Message" 800 100
$global:Labels = CreateLabels $listOfPropertyNames 0 140
$global:TextBoxes = CreateTextBoxes $listOfPropertyNames 200 140
# show it
ShowForm
You can find the Powershell Scripts in the blogpost04.zip file in the download section.