Search This Blog

Thursday, January 05, 2012

Change DNS on multiple computers - Now in PowerShell

I published a script to change DNS on multiple computers a while back (here it is). It was written in VBScript. Now there is an update written in PowerShell by my colleague and friend Jan Egil Ring (PowerShell MVP).

Here is his mail with a link to the module:
--BEGIN--
“Hi Jimmy,

I finally got time to finish the blog-post:


Rather than creating a single script for the specific task I ended up creating a function in PowerShell, including capabilities such as error handling, logging and the option to either replace or add/remove entries from the DNSSearchOrder-list.
I also ended up creating a module I published on Codeplex, since I couldn`t find any complete library for managing network adapters from PowerShell (or other languages for that matter).
So let`s hope people in the community will contribute with more commands to the module…
J (Win 8 will provide native modules for things like this, but I guess they won`t be backward compatible)”
--END--

And here is the Script:

--BEGIN--
I couldn`t resist re-writing your script into PowerShell ;)

It does reset the DNSServerSearchOrder rather than replacing specified IPs, so it`s not exactly the same.




# NAME:Set-DNSServer.ps1
#
# AUTHOR: Jan Egil Ring
# EMAIL: [JImmy removed this]
#
# VERSION HISTORY:
# 1.0 06.10.2011 - Initial release

#Custom variables
$FilePath = "C:\serverNames.txt"
$DNSServerSearchOrder = "10.230.20.9","10.230.20.10"

 foreach ($computer in $FilePath) {

if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
$NICs = Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE AND DHCPEnabled = FALSE" -Computer $computer
if ($NICs) {
foreach ($NIC in $NICs) {
if ($NIC.DNSServerSearchorder) {
$NIC.SetDNSServerSearchOrder($DNSServerSearchOrder)
}
}
}

}
else {
Write-Host "Unable to contact $computer"
}
}
--END--