Solved

vbscript help - creating a custom wmi query.

Posted on 2008-10-02
6
1,216 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sharepoint 2010 Audit Logs 11 84
Deploying Windows 10 in MDT 18 56
Windows IPv6 DHCP server 8 37
iPhone v Android phone 7 30
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now