Addressing the Issue
Whether adding workstations or reconfiguring your network, this script helps ensure connectivity (and may save you some time, to boot!)
- By Chris Brooke
- June 01, 2002
Several weeks ago, I received an e-mail from a reader named Michael Tosh.
He was looking for a script to automate changing the IP address of a computer.
He was attempting to accomplish this feat by using the WSHShell object’s
RegWrite method. Before I could reply to him that WMI was a better fit,
he discovered this for himself and sent me the details, including a small
script that performed this task on the local computer. As this isn’t the
first IP-related request for scripting advice I’ve received, the featured
script this month is based on what Michael sent me (although I’ve taken
the liberty of expanding it somewhat and using my own flavor of Hungarian
Notation). You’ll find the code in Listing 1.
Listing 1. A script to automate setting the IP address of the
computer. (To
download a text version of this listing, right-click here
and "Save As".)
<?xml version="1.0" ?>
<package>
<comment>
This script allows you to set a static IP address
On the local computer
</comment>
<job>
<runtime>
<description>
This script allows you to set a static
IP address
for your local computer
</description>
<example>
C:\cscript SetIP.wsf
</example>
</runtime>
<script language="VBScript">
<![CDATA[
Option Explicit
Dim objLocator, objService, objEnumerator,
strIPAddr, strSNMask
Set objLocator=CreateObject("WbemScripting.SWbemLocator")
Set objService=objLocator.ConnectServer(
"localhost","root\cimv2",,0,context)
Set objEnumerator=objService.ExecQuery("Select
* From Win32_NetworkAdapterConfiguration _
Where DatabasePath IS NOT NULL”)
strIPAddr=Inputbox("Please enter this PC's
IP Address:",
"IP
CONFIGURATION","192.168.1.100")
strSNMask=Inputbox("Please enter this PC's
Subnet Mask:",
"IP
CONFIGURATION","255.255.255.0")
strIPaddr=Array(strIPaddr)
strSNMask=Array(strSNMask)
objService.Security_.impersonationlevel = 3
For Each objInstance in objEnumerator
intStatus = objInstance.EnableStatic(strIPaddr,
strSNMask)
Next
WScript.quit
]]>
</script>
</job>
</package>
How’s This Thing Work?
For the purposes of this script, I used InputBox to allow the user
to enter the IP address and subnet mask. These could just as easily been
taken from a file or entered on the command line. Once they’ve been entered,
they’re converted to single-element arrays. As you no doubt know, you
can bind multiple IP addresses to a single NIC. As such, Win32_NetworkAdapterConfiguration
expects to receive an array when setting this value. The For Each…Next
loop applies the IP address and subnet mask to each adapter in the computer,
which (generally, for workstations, anyway) will only be one.
This script is most useful for configuring multiple workstations at once.
When adding only one or two machines to your network, it’s simple enough
to specify the IP settings at install time. Where this script really shines
is on those occasions where you may be adding a lot of new workstations.
Another possibility is that you’re reconfiguring the internal network
addressing in your organization. (In fact, the latter is more likely.)
Something is Different
Good eye! Yes, something is, indeed, different. Last month, when
I used WMI to make a registry change, I connected using GetObject and
a moniker. I mentioned that there was another way to connect to WMI. This
script demonstrates that method by using CreateObject to bind to the SWbemLocator
object. Connecting in this manner does require a few more lines of code,
but it’s a bit easier to understand what each line is doing. Whether you
connect to WMI using a moniker or the SWbemLocator object is—for the most
part—a matter of personal preference.
I’ve used the SWbemLocator object here in order to show you how it works.
My preference is to connect using a moniker. Most of my WMI scripts take
this approach, but I just want to make sure you know your options.
Improving on Perfection
While Michael’s script does a good job of setting the IP address
of the local machine, there are still a few areas that could do with a
bit of tweaking:
- Accessing other subnets or the Internet—Simply setting an
IP address and subnet mask is fine if all you want to do is communicate
with other local hosts. However, if you want these computers to have
access to the Internet or even to other subnets in your organization,
you’ll need to specify a default gateway.
- Name resolution—Unless you want to spend all of your time
putting HOSTS and LMHOSTS files on each computer (which kind of defeats
the whole purpose of saving time using a script, doesn’t it?), you’ll
need to configure their IP settings to point to DNS and WINS servers.
- Save it for the Stairmaster—Rather than running from machine
to machine to run this script locally, we can use the remote access
abilities of WMI and make the changes to all computers at once, from
the comfort of our own desks!
- Pre-defined addresses—While this script saves a great deal
of time when compared to manually entering IP settings using the network
applet, it still requires you to enter each IP address and subnet mask
manually for each computer. A better approach would be to retrieve the
numbers from a file and automatically enter them.
- Selectively assigning static IP addresses—As an extension
to the previous item, you could use a spreadsheet to hold computer names
and corresponding IP addresses. Chances are that, even if you’re renumbering
your entire network, you’re probably using DHCP for most workstations.
Setting static IP addresses will probably occur only on select servers
and workstations that you can identify by name.
The Best is Yet to Come
Over the next two months, we’ll take a closer look at the above
improvements and implement them in various scripts. It will be stepped
approach, with each script tailored for a specific implementation scenario.
Along the way, we’ll learn more about WMI’s capabilities.
Stay tuned. You won’t want to miss it.