Solved

vbscript help - creating a custom wmi query.

Posted on 2008-10-02
6
1,212 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

Join & Write a Comment

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
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.

746 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

13 Experts available now in Live!

Get 1:1 Help Now