I work in a lot of different offices and conference venues, in addition to my home office. At home, I need a static DNS server address; everywhere else, it has to be dynamic. Tired of the click-click-clickety-click it takes to set, unset, and reset my network adapter settings, I went looking for a Powershell solution. This is the kind of problem that Powershell is made for, right? Right!
What’s my IP?
For starters, let’s use Get-WmiObject and that very long class name to get our network adapter settings:
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
If we glance over what methods and properties are available in the new object ($wmi | get-member;) the IPAddress property leaps out at me. We can use that to display our current IP address**:
That was easy. You can also get your:
- Subnet mask: $wmi.IPSubnet
- DNS server address: $wmi.DNSServerSearchOrder
- Gateway: $wmi.DefaultIPGateway
Set Static IP, Gateway, DNS server
Here’s where we note that we
SHOULD NOT DO THIS ON A PRODUCTION SYSTEM.
You’re messing with the network settings, remember.
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableStatic("IP address", "Subnet mask"); $wmi.SetGateways("Gateway", 1); $wmi.SetDNSServerSearchOrder("DNS server address");
EnableStatic allows you to set a specific IP address and subnet mask. SetGateways is for the default gateway. And SetDNSServerSearchOrder allows you to set static preferred and/or alternate DNS servers.
As an example:
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableStatic("10.0.0.15", "255.255.255.0"); $wmi.SetGateways("10.0.0.1", 1); $wmi.SetDNSServerSearchOrder("127.1.1.12");
This example results in the settings you see in the image on the right: all static, all the time.
Set Dynamic IP, Gateway, DNS server
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableDHCP(); $wmi.SetDNSServerSearchOrder();
DHCP “allows IP addresses to be dynamically allocated”, and running SetDNSServerSearchOrder*** with no parameters (or alternately, with $null) sets the network adapter to get a DNS server address dynamically. Brilliant!
That’s all there is to it, and you can feel free to stop reading here. Unless, of course, you want to get into the muddle that is Get-CimInstance in Powershell 3.0…
But what about CIM?
Some of you may be shouting about Powershell 3.0 and Get-CimInstance; let me say for now that I had no end of trouble with it, I’m still working on it, and there will be a future blog post. And yes, I did find that chapter in the Windows Powershell Cookbook that spells out the use of CIM for this, and yes, I had trouble with that as well. All I can say is, I’m still working on it, and WMI seems to work just fine, thankyouverymuch.
* WMI stands for “Windows Management Instrumentation“…Windows administrative settings and general stuff.
** By the way, you could get also use test-connection computername, a ping equivalent, to get your IP. But today we’re talking Get-CimInstance.
*** On setting DNS to dynamic: In my searching, I found this solution, but have had zero luck with it effecting the change: SetDynamicDNSRegistration(“TRUE”)