Link to home
Start Free TrialLog in
Avatar of WinPE
WinPE

asked on

vbscript help - creating a custom wmi query.

I want the vbscrip to query wmi for the NetConnectionStatus name who's NetConnectionStatus = 2 (connected). And store that name into a variable.

Here's the two queries I just don't know how to loop them

SELECT NetConnectionStatus FROM Win32_NetworkAdapter
I want it to look for where value = 2

SELECT NetConnectionID FROM Win32_NetworkAdapter
Should be local area connection 1 or 2.
Avatar of Paka
Paka

The names of the adapters you are looking for will in an array (colItems).

Option Explicit
Dim objWMIService, ObjItem
Dim strComputer, colItems
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where netConnectionStatus = 2",,48)
 
For Each objItem in colItems
  WScript.Echo "Name: " & objItem.Name 
Next
 
WScript.Quit

Open in new window

Sorry, didn't see the Local Area Connection requirement.  This code will filter the list to those connections named "Local Area Connection".
Option Explicit
Dim objWMIService, ObjItem
Dim strComputer, colItems
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where netConnectionStatus = 2",,48)
 
For Each objItem in colItems
If InStr(ObjItem.name, "Local Area Connection") <> 0 Then
  WScript.Echo "Name: " & objItem.Name
End If
  
  Next
 
WScript.Quit

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Paka
Paka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WinPE

ASKER

Thanks Paka, just what I was looking for. I was actualy just wanting the netconnectionid to be displayed, as modified below.

However I have a problem... Right now I'm betting that there will only be one nic with a connection. But if by chance two are plugged in, how can I work around that? Any thoughts?

If there's some way I can make sure objItem.NetConnectionID will only have one ID instead of two (even if two are plugged in), I can setup a job to disable the second nic and ip only one.

Dim objWMIService, ObjItem
Dim strComputer, colItems
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where netConnectionStatus = 2",,48)
 
For Each objItem in colItems
  strConnection = objItem.NetConnectionID 
Next
 
strIP = Array("1.22.2.2")
strMask = Array("255.255.255.0")
strGatewayIP = Array("1.2.3.3")
 
set colNA = objWMIService.ExecQuery("select * " & _
                            " from Win32_NetworkAdapter " & _
                            " where NetConnectionID = '" & strConnection & "'" ) 
for each objNA in colNA
   set colNAConfig = objWMIService.ExecQuery _
      ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _
        objNA.DeviceID & "'} " & _
      " WHERE resultClass = win32_NetworkAdapterConfiguration ")
   for each objNAConfig in colNAConfig
      intRC = objNAConfig.EnableStatic(strIP,strMask)
      intRC2 = objNAConfig.SetGateways(strGatewayIP)
      if intRC = 0 and intRC2 = 0 then
         WScript.Echo "IP address configured for " & strConnection
      elseif intRC = 1 or intRC2 = 1 then
         WScript.Echo "You must reboot for the changes to take effect for " & _
                      strConnection
      else
         WScript.Echo "There was an error configuring IP for " & _
                      strconnection & ": " & intRC & " and " & intRC2
      end if
   next
next

Open in new window

Avatar of WinPE

ASKER

Ok I think its almost finished.

I just need to do one more check.

For this part:
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where netConnectionStatus = 2",,48)
For Each objItem in colItems
  strConnection = objItem.NetConnectionID
Next

How can I add in a check to see if the array contains more the one item?
If more than one item in array (ex Local Area Connection, Location Area Connection 1)
Then set strConnection = to one of those NetConnectionID's
and set the other to disabled (filesys = WshShell.Run("netsh interface set interface " & chr(34) & strConnection & chr(34) & " Disabled",1,True)



For Each strLine in arrFileLines
MyString = strLine
Next
Dim objWMIService, ObjItem
Dim strComputer, colItems
 
strComputer = "."
Set WshShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where netConnectionStatus = 2",,48)
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\input.txt", ForReading)
 
Const ForReading = 1
 
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
 
For Each strLine in arrFileLines
MyString = strLine
Next
 
MyArray = Split(MyString, ":", -1, 1)
strInputIP = MyArray(0)
strInputMask = MyArray(1)
strInputGatewayIP = MyArray(2)
strInputDns1 = MyArray(3)
strInputDns2 = MyArray(4)
wscript.echo strInputIP
wscript.echo strInputMask 
wscript.echo strInputGatewayIP 
wscript.echo strInputDns1 
wscript.echo strInputDns2
 
For Each objItem in colItems
  strConnection = objItem.NetConnectionID 
Next
strEnable = "netsh interface set interface " & chr(34) & strConnection & chr(34) & " Enabled"
strDisable = "netsh interface set interface " & chr(34) & strConnection & chr(34) & " Disabled"
 
strIP = Array(strInputIP)
strMask = Array(strInputMask)
strGatewayIP = Array(strInputGatewayIP)
strDnsIP = Array(strInputDns1,strInputDns2)
 
set colNA = objWMIService.ExecQuery("select * " & _
                            " from Win32_NetworkAdapter " & _
                            " where NetConnectionID = '" & strConnection & "'" ) 
for each objNA in colNA
   set colNetCards = objWMIService.ExecQuery _
      ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _
        objNA.DeviceID & "'} " & _
      " WHERE resultClass = win32_NetworkAdapterConfiguration ")
   for each objNetCard in colNetCards
      intRC = objNetCard.EnableStatic(strIP,strMask)
      intRC2 = objNetCard.SetGateways(strGatewayIP)
      intRC3 = objNetCard.SetDNSServerSearchOrder(strDnsIP)
      if intRC = 0 and intRC2 = 0 and intRC3 = 0 then
         WScript.Echo "IP address configured for " & strConnection
      elseif intRC = 1 or intRC2 = 1 or intRC3 = 1 then
         WScript.Echo "You must reboot for the changes to take effect for " & _
                      strConnection
      else
         WScript.Echo "There was an error configuring IP for " & _
                      strconnection & ": " & intRC & " and " & intRC2 & " and " & intRC3
      end if
   next
next
 
filesys = WshShell.Run(strDisable,1,True)
filesys = WshShell.Run(strEnable,1,True)

Open in new window

Avatar of WinPE

ASKER

Thanks, led me in the right direction.