Solved

vbscript help - creating a custom wmi query.

Posted on 2008-10-02
6
1,231 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:WinPE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 22

Expert Comment

by:Paka
ID: 22625121
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

0
 
LVL 22

Expert Comment

by:Paka
ID: 22625215
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

0
 
LVL 22

Accepted Solution

by:
Paka earned 500 total points
ID: 22625312
Here's a more elegant version that uses WMI to filter the names.
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) and (name like 'Local Area Connection%')",,48)
 
For Each objItem in colItems
  WScript.Echo "Name: " & objItem.Name
Next
 
WScript.Quit

Open in new window

0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:WinPE
ID: 22625560
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

0
 

Author Comment

by:WinPE
ID: 22626530
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

0
 

Author Closing Comment

by:WinPE
ID: 31502398
Thanks, led me in the right direction.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question