dcsimg

Setting Network Parameters Using WMI (Part 2)

By Marcin Policht (Send Email)
Posted Dec 12, 2002


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:

  • SetWINSServer sets the IP addresses of the primary and secondary WINS servers.
  • SetDNSServerSearchOrder sets the IP addresses of the DNS servers in the order in which they will be used. The input parameter of this method is the array of strings containing individual IP addresses of DNS servers.
  • SetDNSDomain sets the connection specific DNS suffix (this is global setting on Windows NT 4.0)
  • SetTCPIPNetBIOS sets the option to use NETBIOS over TCP/IP. This method accepts one of two values as its parameter: 1 (NetBIOS enabled) or 2 (NetBIOS disabled).

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. 
Error Number " & oOutParam.returnValue End If Set oMethod = oAssociator.Methods_("SetDNSServerSearchOrder") Set oInParam = oMethod.InParameters.SpawnInstance_() oInParam.DNSServerSearchOrder = aDNSServers Set oOutParam = oAssociator.ExecMethod_("SetDNSServerSearchOrder", oInParam) If oOutParam.returnValue = 0 Then WScript.Echo UCase("SetDNSServerSearchOrder") & " method completed successfully" Else WScript.Echo UCase("SetDNSServerSearchOrder") & " method failed.
Error Number " & oOutParam.returnValue End If Set oMethod = oAssociator.Methods_("SetDNSDomain") Set oInParam = oMethod.InParameters.SpawnInstance_() oInParam.DNSDomain = sDNSDomain Set oOutParam = oAssociator.ExecMethod_("SetDNSDomain", oInParam) If oOutParam.returnValue = 0 Then WScript.Echo UCase("SetDNSDomain") & " method completed successfully" Else WScript.Echo UCase("SetDNSDomain") & " method failed.
Error Number " & oOutParam.returnValue End If Set oMethod = oAssociator.Methods_("SetTCPIPNetBIOS") Set oInParam = oMethod.InParameters.SpawnInstance_() oInParam.TcpipNetBIOSOptions = iNetBIOS Set oOutParam = oAssociator.ExecMethod_("SetTCPIPNetBIOS", oInParam) If oOutParam.returnValue = 0 Then WScript.Echo UCase("SetTCPIPNetBIOS") & " method completed successfully" Else WScript.Echo UCase("SetTCPIPNetBIOS") & " method failed.
Error Number " & oOutParam.returnValue End If Next Next

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.

Page 1 of 1


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.