In the previous article in this series we started exploring what we could do with the Win32_NetworkAdapterConfiguration class. This powerful WMI class has 61 properties and 41 methods that can be used for retrieving and changing TCP/IP networking settings on Windows computers.
To illustrate the power of this class, we took the sample script we developed in the first and second articles and, using information about this class on MSDN, we customized our original script to make it do something different. Specifically, we took a script that changed the IP address of a network adapter and customized it to come up with a new script that disables NetBIOS over TCP/IP (NetBT) on all network adapters that have TCP/IP bound and enabled on them.
We need to explore this powerful WMI class further, and we're going to do so in several future articles. But before we do this, let's talk about The Hump.
Let's go back to the script ChangeIPAddress.vbs that we developed to change the IP address of a network adapter:
Option Explicit Dim objWMIService Dim objNetAdapter Dim strComputer Dim strAddress Dim arrIPAddress Dim arrSubnetMask Dim colNetAdapters Dim errEnableStatic
If WScript.Arguments.Count = 0 Then Wscript.Echo "Usage: ChangeIPAddress.vbs new_IP_address" WScript.Quit End If
strComputer = "." strAddress = Wscript.Arguments.Item(0) arrIPAddress = Array(strAddress) arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next
In the first two articles of this series we examined the basics of using Windows scripting to manage TCP/IP networking settings. In particular, we developed the following simple script to change the IP address of a network adapter:
In the first two articles of this series we saw how to change the IP address of a network adapter on a Windows computer using VBScript. Along the way we learned many of the basic concepts of Windows scripting such as classes, objects, properties, methods, and different types of variables including string variables, integer variables, arrays and collections. We also learned some of the basics of writing a good script such as defining variables, implementing error handling, accepting user input, displaying confirmation output, and documenting your script by adding comments.
Our final script worked as expected, but there may be some aspects of it you still find mysterious. For example, take a look at this particular line:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
In my first article, I made the following comment concerning this particular line: "This connects you to the root\cimv2 namespace on the local computer by defining an object named objWMIService and setting it equal to the handle returned by the GetObject method."
What on earth does that mean? What's WMI and how does it work? And why is it important to learn about if you want to learn how to write scripts to administer Windows computers?
Last month I began a new series of articles here on WindowsNetworking.com intended to demonstrate how to manage different aspects of Windows networks using scripts. The first article introduced some basic scripting concepts like objects, methods, and properties, and the goal of the article was to write a simple script that changed the IP address assigned to your network adapter. Here's what we came up with for our first script, which we called ChangeIPAddress.vbs:
strComputer = "." arrIPAddress = Array("172.16.11.99") arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next
When I ran this script on my Windows server, it successfully changed the IP address of the machine from .45 to .99 as could be seen by running ipconfig before and after running the script. So far, so good.
Unfortunately, while the above script works, it's kind of "messy" as it's missing certain things that well-written scripts should have such as variable definitions, error handling, use input, and confirmation output. So let's "clean up" our script by adding these elements, and along the way we'll learn more about the basics of Windows scripting