How Can I Change the IP Address Assigned to a Computer?

How Can I Change the IP Address Assigned to a Computer?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the IP address assigned to a computer?

-- RW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RW. To tell you the truth, this is a very difficult question for the Scripting Guys to answer. Not because this is a hard script to write; as you’ll see in a minute, it’s actually pretty easy. No, this is hard for the Scripting Guys because it involves change, and when you’re a Scripting Guy, well, why would you ever want to change anything? As you might expect, we all drive imported sports cars, we all live in opulent mansions, and we all light our cigars with $100 bills. Do we have the perfect lives? You bet we do: even our IP addresses are perfect. Why would we want to change any of that, IP addresses included?

Note. OK, we admit it: we may have exaggerated a little here. Sometimes - and we’re a bit embarrassed to admit this - we light our cigars using $10 bills. The Scripting Guys deeply regret any inconvenience our previous statements might have caused.

Of course, unlike most Microsoft people the Scripting Guys aren’t totally out-of-touch with the real world: we realize that there are a few people here and there who don’t have several million dollars in their bank account. (Not that we’ve met any of these people, mind you, but we saw some of them on TV. Or at least we think we did; we quickly switched the channel to Lifestyles of the Rich and Famous Scripters.) That means that, for some people, change is good. And it goes without saying that changing their IP address is even better:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")

strIPAddress = Array("192.168.1.0")
strSubnetMask = Array("255.255.255.0")

For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
Next

Before we explain how this script works we should issue one important caveat. We’re assuming (based simply on the fact that you didn’t say otherwise) that all you need to do is change the IP address; in other words, we’re assuming that you don’t need to change your IP gateway, that you don’t need to change your DNS servers, that you don’t need to change your WINS servers, etc. All of those things (and more) can be modified using a script, but it would take a few hundred pages for us to detail all the possibilities. And we’d be willing to do that, too, except that the legendary Peter Costantini has already done it for us; check out Peter’s classic tome Automating TCP/IP Networking on Clients for more information.

And no, although he could have, Peter didn’t hire any consultants: he came up with that title all by himself.

Really.

Now, back to the business at hand. Like we said, this is a pretty simple little script. We start out by connecting to the WMI service on the local computer, although we could use this same script to assign a new IP address to a remote machine. (How? Just assign the name of the remote machine to the variable strComputer.) We then use this line of code to return a collection of all the IP-enabled network adapters on the computer:

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")

Here’s another caveat. This script assumes you have only one IP-enabled network adapter on the computer. In this day-and-age, however, it’s very possible for you to have more than one network adapter installed in a machine. If that’s the case then you should modify this query to return just the network adapter that’s supposed to get the new IP address. How do you do that? Well, an easy way is to specify the adapter Description in the query:

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
        "'Broadcom NetLink (TM) Gigabit Ethernet'")

So couldn’t we have used the Description property in our query to begin with? Sure, it’s just that we haven’t memorized the descriptions for our network adapters. (Good point: what were we thinking?) Because of that it was easier for us to use IPEnabled=True. Like we said, though, that won’t work if you have more than one IP-enabled network adapter installed. You’ll have to figure out what works best for you.

Scripting Guys Tip. When you’re trying to figure this out, we recommend that you write the query and then test it by having the script do something harmless, like echo back the Description for each item in the collection. Don’t actually start changing IP addresses until you know for sure which adapters will be affected by the script and which ones won’t.

Now, where were we? Oh, right: these two lines of code:

strIPAddress = Array("192.168.1.0")
strSubnetMask = Array("255.255.255.0")

All we’re doing here is assigning the new IP address (192.168.1.0) to a variable named strIPAddress and the corresponding subnet mask (255.255.255.0) to a variable named strSubnetMask. The tricky part is that we have to assign these values as arrays; hence the use of the Array function. And, yes, we know that we have only one IP address and one subnet mask. That doesn’t matter; we still have to configure these as arrays or the script will fail.

After setting up our variables we have just one thing left to do: actually assign the new IP address and subnet mask. To do that we set up a For Each loop to loop through the collection of network adapters (see why it’s important that our collection have only one such adapter in it?) and then call the EnableStatic method to assign the new IP address:

For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
Next

Yes, very simple: all we do is pass EnableStatic our two variables (strIPAddress and strSubnetMask) and we’re done.

Like we said, there are plenty of other network adapter settings that can be changed using a script. However, we’ll have to refer you to Peter’s white paper for information on that. Right now we need to call room service and order another tray of cigars and a stack of $100 bills.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • can i get the flow chart and pseudo code for this script