Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to set the static IP and DNS addresses on a server.
Microsoft Scripting Guy, Ed Wilson, is here. One of the really cool things about computers is that you never get bored. At least for me this is true. For example, I have a server that has been running Exchange Server absolutely perfectly for more than a year. Today, it acted like a 150 pound St. Bernard that had become bored. That’s right, it threw a fit. Why did it do so? Well, I had changed the IP network configuration, and I did not change the IP address on this machine. For some reason, the IP changes caused a race condition in Exchange Server, and I could hardly get control of the box. I logged on to the computer, but I was unable to use the graphical tools to set a new IP address on the box—things would spin in circles, and then disappear. Dude, what now?
Well, I thought I would run my Set-StaticIPAddress script on the machine to set the address, but there were two problems:
So what can I do? Well, I opened the Windows PowerShell ISE and typed the commands that follow into the script pane.
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
This is the cool part, even when the Windows PowerShell script execution policy it set to Restricted, which disallows the execution of scripts, it is still possible to open the Windows PowerShell ISE and run commands. This technique allowed me to type the four previous commands, and to execute them all at once.
What do the four previous commands do?
The first command uses the Get-WmiObject cmdlet to retrieve the network adapter configuration for all the network adapters that are enabled for use with IP. To do this, I use the Win32_NetworkAdapterConfiguration WMI class. This class has a number of extremely useful methods—that is, it can do a lot! I store the resulting object in a variable I called $wmi. This line of code is shown here.
When the network adapter configuration object is stored in the $wmi variable, it is easy to use the following three methods: EnableStatic, SetGateways, and SetDnsServerSearchOrder. All that is required is to supply the required values. The EnableStatic method requires the static IP address in addition to a subnet mask. Each of these values are strings. The SetGateways method also requires two strings. The first parameter is the IP address of the gateway, and the second parameter is the metric. The last method I used is the SetDnsServerSearchOrder method. This code is shown here.
The figure that is shown here illustrates the four lines of code, and the output from those commands.
When I ran the code, I could verify the IP address via the GUI tool. The actual IP configuration is shown here.
I am not sure why the race condition appeared in my Exchange Server. My guess is that it was trying really hard to contact domain controllers, DNS servers and the like, and it was unable to do so because of the IP changes. When I fixed the problem, Exchange Server settled down. The cool thing is that I was able to use Windows PowerShell to fix the problem, even though I could not run a script. The four commands I used are the essence of a script, but because I did not save them as a .ps1 file prior to execution, Windows PowerShell saw them as just another group of commands.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
a very interesting post!
The WMI class win32_networkadapterconfiguration is definitely an interesting object!
Perhaps I should do more research on the available WMI classes to see what they offer ...
But it really is not alway obvious, how to find the right class that does the job.
Perhaps we should have a regular WMI day on the blog to get some more valuable ideas!
Excellent info, thank you for sharing. I could see this coming in very useful in those tight spots we get ourselves into on occasion.
Excellent script .. question. with how do I add WINS Servers?
I have 4 i can add in the GUI and I want to add using Powershell
Does not work for me. I get this error:
PS C:\Windows\system32> $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
Method invocation failed because [System.Object] doesn't contain a method named 'EnableStatic'.
At line:2 char:18
+ $wmi.EnableStatic <<<< ("10.0.0.15", "255.255.255.0")
+ CategoryInfo : InvalidOperation: (EnableStatic:String) , RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Object] doesn't contain a method named 'SetGateways'.
At line:3 char:17
+ $wmi.SetGateways <<<< ("10.0.0.1", 1)
+ CategoryInfo : InvalidOperation: (SetGateways:String) , RuntimeException
Method invocation failed because [System.Object] doesn't contain a method named 'SetDNSServerSearchOrder'.
At line:4 char:29
+ $wmi.SetDNSServerSearchOrder <<<< ("10.0.0.100")
+ CategoryInfo : InvalidOperation: (SetDNSServerSearchOrder:String) , RuntimeException
Is there any way to set the IP that is assigned by dhcp to a static IP.
If dhcp assigns
I would like a script that can take that and set it statically
is that even possible?
It sure would be good to see the Latest PowerShell cmdlets being used here to set IP addresses.
It seems I need a (INetCfgLock::AcquireWriteLock) to perform these actions. How is this done in Powershell? Is there a way around it? Our DNS server is buggy, so I am having to visit my users clients to change the DNS setting from dynamic (for internet access) to static (for LAN access) and back, depending on their needs. I was hoping to save my self a lot of running around by placing a script on their desktops that would let them toggle it off and on until I can get this issue resolved.
None of that is necessary. PowerShell takes care of all access correctly with WMI. You are looking at code that does not apply to this situation.
I recommend fixing your DNS server.
Please update the link for setting IPv6 Address.