Use PowerShell to Configure Static IP and DNS Settings

Use PowerShell to Configure Static IP and DNS Settings

  • Comments 14
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    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!

    Klaus (Schulte)

  • 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'"

    $wmi.EnableStatic("10.0.0.15", "255.255.255.0")

    $wmi.SetGateways("10.0.0.1", 1)

    $wmi.SetDNSServerSearchOrder("10.0.0.100")

    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

       + FullyQualifiedErrorId : MethodNotFound

    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

       + FullyQualifiedErrorId : MethodNotFound

  • Is there any way to set the IP that is assigned by dhcp to a static IP.

    If dhcp assigns

    192.168.1.10

    255.255.255.0

    192.168.1.1

    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.

  • Hey Ed,

    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.

    Thanks.

    Marshall

  • @Marshall

    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.

  • Hi, it would be nice to update this post to reflect changes in PowerShell v4 and new commands existing there (at least with some link to new commands)

  • How can we set the secondary DNS?

    @nitinsalgar

  • Thank you! Ed Wilson, you helped me a lot!

    I did a similar script using your tips take a look later please!
    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/edaa0fc3-65eb-4756-bad7-c7727b25d790/change-ipmaskgateway-with-default-value?forum=ITCG

  • @nitinsalgar

    You first have to store the primary and secondary DNS Server values in a variable, then pass that to SetDNSServerSearchOrder like so:

    $DNS ="10.0.0.100","10.0.0.200"

    $wmi.SetDNSServerSearchOrder($DNS)

    Roger

  • @seasonal vegetables

    that didn't work form me by just putting the the ip's with out making it an array. Ex:

    $DNS= @("192.168.1.1", "192.168.1.2")

    Then I ran

    $wmi.setdnsserversearchorder("DNS")