Skip navigation

Using PowerShell in a DNS Migration

I recently received a question from James via email (thanks for sending it - readers, you can send questions to me, too. My email domain is ConcentratedTech.com, and my email alias is powershell, and you can probably figure out the address from that). In a very concise email (thanks again!) he asked:

I was wondering if you may be able to point me in the right direction in order to be able to create a script that would allow us to automate our DNS Migration across our environment.
 
Task:
 
1. Update TCP/IP Settings: DNS addresses, DNS suffix order and DNS connection
2. WINS address and switches (Enable LMHOSTS) and NetBIOS setting switch

Cool. I immediately thought of the Win32_NetworkAdapterConfiguration WMI class. Now, the trick with this class is figuring out how to query just actual Ethernet adapters and not all the fake virtual adapters in Windows. I do it this way:

PS C:\> gwmi win32_networkadapterconfiguration | where {$_.description -like '*intel*' }

That'll obviously differ in your environment, but hopefully you get the idea. From there, you can execute some methods of that class. There's a SetWINSServer method (ugh, WINS - sigh), a SetDNSDomain method, a SetDNSSuffixSearchOrder method, and so on. I don't see "DNS Connection," although I'm not sure what you're looking for on that setting, James. With the right method - and the docs page at  http://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx - you're on the right track. You could implement this as a logon script or something, or just target computers remotely:

PS C:\> gwmi win32_networkadapterconfiguration -computername client1,client2,client3 | where {$_.description -like '*intel*' } | invoke-wmimethod -name setwinsserver -arg '192.168.10.1','192.168.12.5'

That'll get you a lot of what you need. It gets a bit tricky passing in arrays (which is what SetDNSServerSearchOrder requires, for example), but there are plenty of examples out there. Now, the one thing you won't get is the enabling of LMHOSTS, which isn't an adapter setting but is an operating system setting buried in the registry.  http://www.pctools.com/guides/registry/detail/1285 has some good details on which key to use, and you can use the registry drives (HKLM:) in PowerShell to access those settings. This is a perfect time to look into Invoke-Command and PowerShell v2 remoting (assuming you've deployed, or can deploy, PowerShell v2 to your target machines), since it'll let you manipulate the registry on the remote machines very easily.

All that said, when it comes to client computers at least, I'm accustomed to just using DHCP to push out those values. You can do DNS servers, domain name, WINS servers, and lots more. That's a big reason I usually put my servers on DHCP reservations, in fact - so that I can change these options via DHCP without a lot of hassle, but still have the servers getting the same address.

Hope that helps a bit! Anyone else been in this situation? How have you handled it?
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish