Solved

vbscript help - creating a custom wmi query.

Posted on 2008-10-02
6
1,218 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

786 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