WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

WMI ExecQuery

 Keywords:  

Question:

I am still trying to understand WMI and Winbatch. In VBScript when you query WMI you have the option to narrow your selection statement. In the example below I do a select statement on the Win32_NetworkAdapterConfiguration class to get only the network adapters where IP is enabled. Can I replicate this functionality in Winbatch?

I am automating a build process and am trying to condense the number of external scripts that I am calling.

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objAdapter in colAdapters
Wscript.Echo "[" & "NIC_" & objAdapter.Description & "]"

If Not IsNull(objAdapter.IPAddress) Then
For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress)
Wscript.Echo "IP address=" & objAdapter.IPAddress(i)
Next
End If

If Not IsNull(objAdapter.IPSubnet) Then
For i = LBound(objAdapter.IPSubnet) To UBound(objAdapter.IPSubnet)
Wscript.Echo "Subnet=" & objAdapter.IPSubnet(i)
Next
End If
If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = LBound(objAdapter.DefaultIPGateway) To UBound(objAdapter.DefaultIPGateway)
Wscript.Echo "Default gateway=" & objAdapter.DefaultIPGateway(i)
Next
End If

Wscript.Echo "Physical address=" & objAdapter.MACAddress 
If Not IsNull(objAdapter.DNSServerSearchOrder) Then
For i = LBound(objAdapter.DNSServerSearchOrder) To UBound(objAdapter.DNSServerSearchOrder)
Wscript.Echo "DNS server=" & objAdapter.DNSServerSearchOrder(i)
Next
End If
Wscript.Echo "Primary WINS server=" & objAdapter.WINSPrimaryServer
Wscript.Echo "Secondary WINS server=" & objAdapter.WINSSecondaryServer

Wscript.Echo "DNS domain=" & objAdapter.DNSDomain
Wscript.Echo "DNS suffix search list=" & objAdapter.DNSDomainSuffixSearchOrder


Wscript.Echo "DHCP enabled=" & objAdapter.DHCPEnabled
Wscript.Echo "DHCP server=" & objAdapter.DHCPServer

Wscript.Echo "Lease obtained=" & objAdapter.DHCPLeaseObtained
Wscript.Echo "Lease expires=" & objAdapter.DHCPLeaseExpires & vbCRLF
Next
Wscript.Quit

Answer:

This should work:
Locator = ObjectOpen("WbemScripting.SWbemLocator")
ComputerName = ""
User = ""
Password = ""
Service = Locator.ConnectServer(ComputerName,"root/cimv2",User,Password)
Security = Service.Security_
Security.ImpersonationLevel = 3
colAdapters = Service.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
hEnum = ObjectCollectionOpen(colAdapters)
While 1
	objAdapter = ObjectCollectionNext(hEnum)
	If objAdapter == 0 Then Break
	
	Message("NIC", objAdapter.Description)

	objIPAddress = objAdapter.IPAddress
   cnt = ArrInfo(objIPAddress, 1)
	For i = 0 To cnt-1
		Message("IP address=",objAdapter.IPAddress(i))
	Next


	objIPSubnet = objAdapter.IPSubnet
	cnt = ArrInfo(objIPSubnet, 1)
	For i = 0 To cnt-1
	  Message("Subnet=" ,objAdapter.IPSubnet(i))
	Next

	
	objDefaultIPGateway = objAdapter.DefaultIPGateway
	cnt = ArrInfo(objDefaultIPGateway, 1)
	For i = 0 To cnt-1
	  Message("Default gateway=" ,objAdapter.DefaultIPGateway(i))
	Next

	
	Message("Physical address=" ,objAdapter.MACAddress)

	objDNSServerSearchOrder = objAdapter.DNSServerSearchOrder
	cnt = ArrInfo(objDNSServerSearchOrder, 1)
	For i = 0 To cnt-1
	  Message("DNS server=" ,objAdapter.DNSServerSearchOrder(i))
	Next
	

	Message("Primary WINS server=",objAdapter.WINSPrimaryServer)
	Message("Secondary WINS server=" ,objAdapter.WINSSecondaryServer)
	Message("DNS domain=",objAdapter.DNSDomain)

	objDNSDomainSuffixSearchOrder = objAdapter.DNSDomainSuffixSearchOrder
	cnt = ArrInfo(objDNSDomainSuffixSearchOrder, 1)
	For i = 0 To cnt-1
	  Message("DNS suffix search list=" ,objAdapter.DNSDomainSuffixSearchOrder(i))
	Next

	Message("DHCP enabled=",objAdapter.DHCPEnabled)	
	Message("DHCP server=",objAdapter.DHCPServer)

	Message("Lease obtained=",objAdapter.DHCPLeaseObtained)
	Message("Lease expires=",objAdapter.DHCPLeaseExpires)

  ObjectClose(colAdapters)
EndWhile
ObjectCollectionClose(hEnum)

ObjectClose(Security)
ObjectClose(Service)
ObjectClose(Locator)


Article ID:   W16277
File Created: 2004:08:24:10:55:10
Last Updated: 2004:08:24:10:55:10