Sign in to follow this  
Followers 0
HockeyFan

Help with WMI script

11 posts in this topic

Hello all,

I was wondering if someone would be so kind as to help me with the following script that I copied from Autoit Scriptomatic. What I would like to do is list only the objects where the WMI "NetConnectionID" is not equal to "null".

; Generated by AutoIt Scriptomatic</P> <P>$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"</P> <P>$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)</P> <P>If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
      $strNetworkAddresses = $objItem.NetworkAddresses(0)
      $Output = $Output & "ProductName: " & $objItem.ProductName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapter" )
Endif</P> <P>

Thanks! :D

Share this post


Link to post
Share on other sites



$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
      $strNetworkAddresses = $objItem.NetworkAddresses(0)
      $Output = $Output & "ProductName: " & $objItem.ProductName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapter" )
Endif

Share this post


Link to post
Share on other sites

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
      $strNetworkAddresses = $objItem.NetworkAddresses(0)
      $Output = $Output & "ProductName: " & $objItem.ProductName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapter" )
Endif

Thank you! :D

Share this post


Link to post
Share on other sites

Another thought...

Is it possible to put the "NetConnectionID" list output into a GUI ComboBox? In other words, the query results for "NetConnectionID" put into a dropdown list that a user could select?

:D

Share this post


Link to post
Share on other sites

Did you mean something like this?

#include <GuiConstants.au3>

$hWnd = GuiCreate("")
$hCombo = GuiCtrlCreateCombo("",5,5,390)
$hEdit = GuiCtrlCreateEdit("",5,30,390,360,$ES_READONLY)
$sEdit = ""

; Generated by AutoIt Scriptomatic
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
    For $objItem In $colItems
        GUICtrlSetData($hCombo,$objItem.NetConnectionID, $objItem.NetConnectionID)
    Next
EndIf

_UpdateDisplay()
GuiSetState()

While 1
    $msg = GuiGetMsg()
    
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hCombo
            _UpdateDisplay()
    EndSwitch
WEnd

Func _UpdateDisplay()

    GUICtrlSetData($hEdit,"")
    $sEdit = ""
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) then
        For $objItem In $colItems
            If $objItem.NetConnectionID = GuiCtrlRead($hCombo) Then
                $sEdit &= "Description: " & $objItem.Description & @CRLF
                $sEdit &=  "MACAddress: " & $objItem.MACAddress & @CRLF
                $sEdit &=  "Name: " & $objItem.Name & @CRLF
                $sEdit &=  "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
                $sEdit &=  "Network Addresses: " & $objItem.NetworkAddresses(0) & @CRLF
                $sEdit &=  "ProductName: " & $objItem.ProductName & @CRLF
                $sEdit &=  "SystemName: " & $objItem.SystemName & @CRLF
                GUICtrlSetData($hEdit,$sEdit)
            EndIf
        Next
    EndIf

EndFunc

Share this post


Link to post
Share on other sites

covaks,

This is great, thanks...but, after thinking about it, what I'm after is a query based on the "NetConnectionID" but displaying the "$Object.Name" in the ComboBox. I no longer want the Edit box that displays the listed output but instead want the NetConnectionID assciated with the $Object.Name to be set to a constant (like $NetConnection = $objItem.NetConnectionID) so that I can plug that into the netsh.exe command that will be run.

#include <GuiConstants.au3
$hWnd = GuiCreate("")
$hCombo = GuiCtrlCreateCombo("",5,5,390)
$sEdit = ""
; Generated by AutoIt Scriptomatic
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
    For $objItem In $colItems
       ;GUICtrlSetData($hCombo,$objItem.NetConnectionID, $objItem.NetConnectionID)
        GUICtrlSetData($hCombo,$objItem.Name, $objItem.Name)
    Next
EndIf
GuiSetState()
While 1
    $msg = GuiGetMsg()
    
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hCombo
           ;
    EndSwitch
WEND

Share this post


Link to post
Share on other sites

I think this is what you want

#include <GuiConstants.au3>
$hWnd = GuiCreate("")
$hCombo = GuiCtrlCreateCombo("",5,5,390)
$sEdit = ""
; Generated by AutoIt Scriptomatic
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$cData = ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
    For $objItem In $colItems
      ;GUICtrlSetData($hCombo,$objItem.NetConnectionID, $objItem.NetConnectionID)
       ;GUICtrlSetData($hCombo,$objItem.Name, $objItem.Name)
       $cData &= $objItem.Name) & "|"
    Next
    $cDef = StringLeft($cData, StringInStr($cData,"|")-1)
    GUICtrlSetData($hCombo, StringTrimRight($cData, 1), $cDef)
EndIf
GuiSetState()
While 1
    $msg = GuiGetMsg()
    
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hCombo
          ;
    EndSwitch
WEND

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I think this is what you want

GEOSoft,

Thanks for your input. Here is basically what I'm trying to do...(this code is being added to an already existing script that I created):

#include <GuiConstants.au3>
$hWnd = GUICreate("Network Adapters", 477, 155, 193, 125)
$Label1 = GUICtrlCreateLabel("Select Network Adapter:", 8, 8, 216, 25)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$hCombo = GUICtrlCreateCombo("", 8, 32, 296, 25)
$hEdit = GUICtrlCreateLabel("", 8, 84, 300, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetData(-1, "")
$Label2 = GUICtrlCreateLabel("ConnectionID", 8, 64, 152, 17)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUISetState(@SW_SHOW)
$sEdit = ""</P> <P>; Generated by AutoIt Scriptomatic
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)</P> <P>If IsObj($colItems) Then
    For $objItem In $colItems
       ;GUICtrlSetData($hCombo,$objItem.NetConnectionID, $objItem.NetConnectionID)
  GUICtrlSetData($hCombo,$objItem.Name, $objItem.Name)
    Next
EndIf</P> <P>_UpdateDisplay()
GuiSetState()</P> <P>While 1
    $msg = GuiGetMsg()
    
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hCombo
   _UpdateDisplay()
            
    EndSwitch
WEnd</P> <P>Func _UpdateDisplay()
    GUICtrlSetData($hEdit,"")
    $sEdit = ""
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) then
        For $objItem In $colItems
            If $objItem.Name = GuiCtrlRead($hCombo) Then
  ;If $objItem.NetConnectionID = GuiCtrlRead($hCombo) Then
               ;$sEdit &=  "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
    $sEdit &=  $objItem.NetConnectionID & @CRLF
                GUICtrlSetData($hEdit,$sEdit)
   EndIf
        Next
    EndIf</P> <P>EndFunc

...but now I'm trying to figure out how to set the default option within the ComboBox to the active adapter, the Ethernet adapter. Any idea's? :D

Also, any way to speed the query up when the script is run? Just trying to figure out what I need to do with the script while the query is running so that user's don't click anything before the data is populated.

Thanks for you help!

Share this post


Link to post
Share on other sites

HELP!

Would someone be able to help me resolve my issue? I'm trying to use WMI to test to see what the active network adapter is when my script first starts so that the active adapter can be set as the default choice in a ComboBox. So far, the active network card is not being displayed as default in the combobox.

Here is what I have so far:

#include <GuiConstants.au3>
local $DefaultComboBox
$hWnd = GUICreate("Network Adapters", 477, 155, 193, 125)
$Label1 = GUICtrlCreateLabel("Select Network Adapter:", 8, 8, 216, 25)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$hCombo = GUICtrlCreateCombo("", 8, 32, 296, 25)
$hEdit = GUICtrlCreateLabel("", 8, 84, 300, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetData(-1, "")
$Label2 = GUICtrlCreateLabel("ConnectionID", 8, 64, 152, 17)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUISetState(@SW_SHOW)
$sEdit = ""</P> <P>; Generated by AutoIt Scriptomatic
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
    For $objItem In $colItems
       ;GUICtrlSetData($hCombo,$objItem.NetConnectionID, $objItem.NetConnectionID)
 ;GUICtrlSetData($hCombo,$objItem.Name, $objItem.Name)
  GUICtrlSetData($hCombo,$objItem.Name, $DefaultComboBox)
 Next
EndIf
_UpdateDisplay()
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hCombo
   _UpdateDisplay()
EndSwitch
WEnd
Func _UpdateDisplay()
    GUICtrlSetData($hEdit,"")
    $sEdit = ""
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) then
        For $objItem In $colItems
            If $objItem.Name = GuiCtrlRead($hCombo) Then
  ;If $objItem.NetConnectionID = GuiCtrlRead($hCombo) Then
               ;$sEdit &=  "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
    $sEdit &=  $objItem.NetConnectionID & @CRLF
                GUICtrlSetData($hEdit,$sEdit)
   EndIf
        Next
    EndIf
EndFunc

Func OnAutoItStart()
   $wbemFlagReturnImmediately = 0x10
   $wbemFlagForwardOnly = 0x20
   $colItems = ""
   $strComputer = "localhost"
   $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
   $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where NetConnectionID != NULL", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
   If IsObj($colItems) then
        For $objItem In $colItems
            If $objItem.NetConnectionStatus = 2 Then
    MsgBox(0,"Active NIC:", $objItem.Name);This is for testing
                $DefaultComboBox = $objItem.Name
   EndIf
        Next
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Made changes to two places and seem to do what you wanted.

If IsObj($colItems) Then
    For $objItem In $colItems
        GUICtrlSetData($hCombo, $objItem.Name, OnAutoItStart())
    Next
EndIfoÝ÷ Ù«­¢+ÙÕ¹=¹Õѽ%ÑMÑÉÐ ¤($ÀÌØíݵ±IÑÕɹ%µµ¥Ñ±äôÁàÄÀ($ÀÌØíݵ±½ÉÝÉ=¹±äôÁàÈÀ($ÀÌØí½±%ѵÌôÅÕ½ÐìÅÕ½Ðì($ÀÌØíÍÑÉ
½µÁÕÑÈôÅÕ½Ðí±½±¡½ÍÐÅÕ½Ðì($ÀÌØí½©]5%MÉÙ¥ô=©Ð ÅÕ½ÐíÝ¥¹µµÑÌèÀäÈìÀäÈìÅÕ½ÐìµÀìÀÌØíÍÑÉ
½µÁÕÑȵÀìÅÕ½ÐìÀäÈíɽ½ÐÀäÈí
%5XÈÅÕ½Ðì¤($ÀÌØí½±%ѵÌôÀÌØí½©]5%MÉÙ¥¹áEÕÉä ÅÕ½ÐíM1
P¨I=4]¥¸ÌÉ}9ÑݽɭÁÑÈÝ¡É9Ñ
½¹¹Ñ¥½¹%ÌÌìô9U10ÅÕ½Ðì°ÅÕ½Ðí]E0ÅÕ½Ðì°ÀÌØíݵ±IÑÕɹ%µµ¥Ñ±ä¬ÀÌØíݵ±½ÉÝÉ=¹±ä¤(%%%Í=¨ ÀÌØí½±%ѵ̤Q¡¸($%½ÈÀÌØí½©%Ñ´%¸ÀÌØí½±%ѵÌ($$%%ÀÌØí½©%Ñ´¹9Ñ
½¹¹Ñ¥½¹MÑÑÕÌôÈQ¡¸($$$%IÑÕɸÀÌØí½©%Ñ´¹9µ($$%¹%($%9áÐ(%¹%(%IÑÕɸÅÕ½ÐìÅÕ½Ðì)¹Õ¹ìôôÐí=¹Õѽ%ÑMÑÉÐ

Share this post


Link to post
Share on other sites

Joon,

Yes in deed! This does exactly what I wanted. Thank you!!

:D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0