The first article of this series described two Windows Management Instrumentation (WMI) classes that contain components needed to configure TCP/IP settings for a network interface: the Win32_NetworkAdapter class and the Win32_NetworkAdapterConfiguration class.
Windows Management Instrumentation provides a number of ways to collect information about network configuration and manage its parameters, making scripting configuration in Windows 2000 and Windows XP much easier. In the second of this two-part article, Marcin Policht discusses the methods used by the Win32_NetworkAdapterConfiguration class and offers a script to employ them.
The Win32_NetworkAdapter class contains properties that allow you to identify the network card based on a number of criteria and was described in depth in the first part of this two-part series. The most commonly used would be AdapterType (e.g., Ethernet 802.3), MAC Address, or NetConnectionStatus (two representing connected states).
The Win32_NetworkAdapterConfiguration class contains properties that provide network-protocol-related parameters (such as IP address, DNS, and WINS settings) and methods that allow manipulation of these parameters. The way to execute the methods is standard: Simply specify the correct input parameters, perform the method call, and verify that the method executed properly by examining the return values. However, you also must take advantage of the link between the instance of Win32_NetworkAdapter class (representing network interface) and the instance of Win32_NetworkAdapterConfiguration class (representing configuration of this network interface). This is accomplished using the Associators_ method and is illustrated in the script below.
The script first specifies the criteria that will be used to locate the network adapter which is supposed to be configured. I decided to use the AdapterType (Ethernet 802.3) and NetConnectionStatus (2), but, obviously, you can choose any other valid property. Next, I form a WQL query that will be used to find collection of instances with properties matching my criteria. WHERE clause of the WQL query contains one or more conditions comparing property names and their desired values. For each instance returned from the query, I execute Associators_ method, which gives the associated instance of Win32_NetworkAdapterConfiguration class. Once this instance is available, I execute the appropriate networking method, which sets its network properties.
The script uses the following methods:
Note that the assumption is the network adapter is configured with statically assigned IP parameters (WINS and DNS related) that must be altered.
To adjust the script to your environment, make sure you change the values of the sComputer, sPriWINS, sSecWINS, sDNSServers, sDNSDomain, and iNetBIOS variables. You might also want to consider whether the sample properties and values used here will correctly identify the target network adapter that needs to be configured. If this is not the case, modify the sProperty1, sProperty2, sValue1, and iValue2 accordingly.
sNameSpace = "root/CIMV2"
sTargetClass = "Win32_NetworkAdapterConfiguration"
sClass = "Win32_NetworkAdapter"
sProperty1 = "AdapterType"
sValue1 = "Ethernet 802.3"
sProperty2 = "NetConnectionStatus"
iValue2 = 2
sComputer = "SWYNKPC0001"
sPriWINS = "1.2.3.4"
sSecWINS = "1.2.4.3"
aDNSServers = Array("1.2.2.1","1.2.3.5")
sDNSDomain = "swynk.com"
iNetBIOS = 1
sWQLQuery = "SELECT * FROM " & sClass & " WHERE " & sProperty1 & "=" & Chr(34) & sValue1 & Chr(34) & _
" AND " & sProperty2 & "=" & iValue2
Set cInstances = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/" & sNameSpace).ExecQuery(sWQLQuery, "WQL")
For Each oInstance In cInstances
Set cAssociators = oInstance.Associators_(,sTargetClass)
For Each oAssociator In cAssociators
Set oMethod = oAssociator.Methods_("SetWINSServer")
Set oInParam = oMethod.InParameters.SpawnInstance_()
oInParam.WINSPrimaryServer = sPriWINS
oInParam.WINSSecondaryServer = sSecWINS
Set oOutParam = oAssociator.ExecMethod_("SetWINSServer", oInParam)
If oOutParam.returnValue = 0 Then
WScript.Echo UCase("SetWINSServer") & " method completed successfully"
Else
WScript.Echo UCase("SetWINSServer") & " method failed.
|
The Win32_NetworkAdapterConfiguration class contains a number of other methods, including some that allow you to switch between static and DHCP assigned configuration, enable or disable IPSecurity, and change more granular IP (e.g., MTU size, discovery, default TTL, Dead Gateway detection, and TCP Window size) and IPX related parameters (IPX network frame type and network number).
Discuss WMI and other Windows 2000 issues in the ServerWatch Discussion Forum.
Marcin Policht obtained his Master of Computer Science degree about 20 years ago and has been since then working in the Information Technology field, handling variety of responsibilities, but focusing primarily on the areas of identity and access management, virtualization, system management, and, more recently private, hybrid, and public cloud services. He has authored the first book dedicated to Windows Management Instrumentation and co-written several others dealing with subjects ranging from core operating system features to high-availability solutions. His articles have been published on such Web sites as ServerWatch.com and DatabaseJournal.com. For his contributions to the Microsoft technical community, he has been awarded the title of Microsoft MVP over the last ten years.
Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.