powershell to get network settings

We are moving our core DNS and WINS server soon, so I need to update all our servers with the new information. I used Hyena to get a list of all of our servers. Then I started looking for a script to gather the current network info so we knew what we needed to change. After some help from http://activedir.org/ I decided to use PowerShell.
My first attempt of hacking together others code looked like this

$colComputers = get-content C:\Clientlist1.txt
foreach ($strComputer in $colComputers)
{
# PowerShell cmdlet to interrogate the Network Adapter
$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
-computername $strComputer | Where{$_.IpEnabled -Match "True"}
write-host $strComputer
foreach ($objItem in $colItems) {
   write-host "MAC Address : " $objItem.MACAddress
   write-host "IPAddress : " $objItem.IPAddress
   write-host "DNS Servers : " $objItem.DNSServerSearchOrder
   Write-host "WINS Server : " $objItem.WINSPrimaryServer
   Write-Host " "
   Write-Host " "
   Write-Host " "
}

}

it worked but it was writing the output to the command window. I looked though file-out and write-out but couldn’t figure those out. I ended up posting my code on http://www.powergui.org ‘s forum and got some more help. In the form of a much better way to do what I needed.

get-content C:\clientlist1.txt | foreach {
   Get-WMIObject Win32_NetworkAdapterConfiguration -computername $_ -filter "IpEnabled='True'"
} | select-object __SERVER,MACAddress,IPAddress,DNSServerSearchOrder,WINSPrimaryServer | out-file c:\IPEnabled.txt

Much cleaner and works great!

Working though this and trying different code snippets I found. Powershell has so sweet output formatting.

Try this for nice formatting (it was just missing the servername and output to a file)

get-wmiobject -query "Select * FROM Win32_NetworkAdapterConfiguration WHERE IpEnabled='TRUE'" -computer (get-content C:\Clientlist1.txt) |  ft MacAddress,IPAddress,DNSServerSearchOrder,WINSPrimaryServer -auto

Thank you to everyone on the activedir list and the powergui forums.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.