Use PowerShell to Configure Static IP and DNS Settings

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:

  1. The script execution policy in my Exchange Server does not permit the running of scripts.
  2. The server is completely isolated and cannot contact my script share.

So what can I do? Well, I opened the Windows PowerShell ISE and typed the commands that follow into the script pane.

SetStaticIP.ps1

$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(“10.0.0.100”)

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.

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter “ipenabled = ‘true'”

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.

$wmi.EnableStatic(“10.0.0.15”, “255.255.255.0”)

$wmi.SetGateways(“10.0.0.1”, 1)

$wmi.SetDNSServerSearchOrder(“10.0.0.100”)

The figure that is shown here illustrates the four lines of code, and the output from those commands.

Image of command output

When I ran the code, I could verify the IP address via the GUI tool. The actual IP configuration is shown here.

Image of IP properties

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 scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy