Jump to content

WMI Query Help


Recommended Posts

Im working on a program that will automatically disable wireless while on a wire connection. I have found a VB script that works and have been able to recreate it in Autoit, with one issue. When I attempt to query with a Like statement for 'Local Area Connection%' autoit will not recognize the wildcard. I need the wildcard to work as some of our machines have multiple local area connections, docking stations, usb adapters so there could be local area connection #1 #2 #3 ect.

VB script that works

Set colLAN = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID like 'Local Area Connection%' and PhysicalAdapter='True'" )

$strWirelessName = GetWirelessName()
$objWMIService = ObjGet("winmgmts:\root\cimv2")
$colLAN = $objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID LIKE 'Local Area Connection%' and PhysicalAdapter='True'")
$colWiFi = $objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID =" & "'" & $strWirelessName & "'" & " and PhysicalAdapter='True'")

If I remove the % in the Autoit script it will work, but only if the LAN connection is made directly to the NIC. The VB script with the % works for any LAN connection I use, dock NIC, usb adapters. Can anyone think of a way to have the Autoit query view the % as the wildcard syntax or different method? complicated stinginstr?

Thank!

Link to comment
Share on other sites

iviikel,

You may get more response if you post code that runs.  

"C:UsersADMIN010Desktopwmi - like.au3"(1,36) : error: GetWirelessName(): undefined function.

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

You can have a look at my >Network UDF.

So you can use _DisableNetAdapter() with either the adapter name or the network connection name.

A simple code, which allow you to disable the wireless adapter even if you don't know it's name :

#RequireAdmin
#Include "Network.au3"

Local $aAdapters = _GetNetworkAdapterList()
If NOT IsArray($aAdapters) Then Exit

For $i = 0 To UBound($aAdapters) - 1
    If _IsWirelessAdapter( $aAdapters[$i][0] ) Then
        ConsoleWrite("Wireless found : " & $aAdapters[$i][0] & " / " & $aAdapters[$i][1] & @CRLF)
        _DisableNetAdapter([$i][0])
    EndIf
Next
Link to comment
Share on other sites

Try this:

$colLAN = $objWMIService.ExecQuery('Select * From Win32_NetworkAdapter Where NetConnectionID LIKE "Local Area Connection%" AND NetConnectionStatus="2"')

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Here is the full code that will work for only the internal NIC card, Some of the content is leftover garbage from the VB conversion but it works as intended. and as I said before the VB script works on any LAN device I connect to a computer and the only difference is the % after 'Local Area Connection'. If I add that to my AutoIT script it will no longer work, As if Autoit wont recognize the % as a wildcard as VB does

Dim $objDim, $objWMIService
Dim $colLAN, $colWiFi
Dim $objWiFi, $objLAN
Dim $strWirelessName
Dim $State = ""
Dim $wireStatus = ""
Dim $wifiStatus = ""
Dim $speed
;Dim $oVBS
$oVBS = ObjCreate("ScriptControl")
$oVBS.language = "VBScript"
Global Const $Nothing = $oVBS.eval("Nothing")
$oVBS = $Nothing
While 1
$colLAN = ""
$strWirelessName = GetWirelessName()
$objWMIService = ObjGet("winmgmts:\root\cimv2")
$colLAN = $objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID LIKE 'Local Area Connection' and PhysicalAdapter='True'")
$colWiFi = $objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID =" & "'" & $strWirelessName & "'" & " and PhysicalAdapter='True'")
For $objWiFi In $colWiFi
  If $objWiFi.Netconnectionstatus = 2 Then
   $wifiStatus = True
   ;MsgBox(0,"","WiFi Connected")
  Else
   $wifiStatus = False
   ;MsgBox(0,"","WiFi DIS-Connected")
  EndIf
Next
For $objLAN in $colLAN
  If $objLAN.Netconnectionstatus=2 Then
   $wireStatus = True
  Else
   $wireStatus = False
  EndIf
Next
If $wireStatus = False And $wifiStatus = False Then
  EnableWireless($strWirelessName)
ElseIf $wireStatus = True And $wifiStatus = True Then
  DisableWireless($strWirelessName)
EndIf
Sleep(5000)
WEnd
Func GetWirelessName()
Local $strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
Local $objReg
Local $arrSubKeys[25]
Local $SubKey
Local $strValueName
Local $dwValue
Local $strValue
Local $intRet
Const $HKLM = "HKEY_LOCAL_MACHINE\"
    For $i = 1  to UBound($arrSubKeys)
    Local $var = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}", $i)
    IF @error <> 0 Then ExitLoop
        $arrSubKeys[$i] = $var
        ReDim $arrSubKeys[$i+2]
    Next
$objReg = ObjGet("winmgmts:.rootdefault:StdRegProv")
;~ For $SubKey In $arrSubKeys
    For $i = 1 to UBound($arrSubKeys)
  $strValueName = "MediaSubType"
  $Dwvalue = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" & $arrSubKeys[$i] & "\Connection","MediaSubType")
  If $dwValue = 2 Then
   $strValueName = "Name"
   $strValue = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" & $arrSubKeys[$i] & "\Connection", $strValueName)
   ExitLoop
   EndIf
Next
Return($strValue)
EndFunc
Func EnableWireless($strNetConn)
Local $objShell, $colConnections, $objConn, $objNetwork, $objEnable, $objVerb
Local $strEnable = "En&able"
Const $NETWORK_CONNECTIONS = "0x31"
$objShell = ObjCreate("Shell.Application")
$colConnections = $objShell.Namespace(0x31)
For $objConn In $colConnections.Items
  If $objConn.Name = $strNetConn Then
   $objNetwork = $objConn
   ExitLoop
  EndIf
Next
$objEnable = $Nothing
;Enable NIC
For $i In $objConn.verbs
  If $i.name = $strEnable Then
   $objEnable = $i
   ExitLoop
  EndIf
Next
If $objEnable <> $Nothing Then $objEnable.DoIt
Sleep(5000)
EndFunc
Func DisableWireless($strNetConn)
Local $objShell, $colConnections, $objConn, $objNetwork, $objDisable, $objVerb
Local $strDisable = "Disa&ble"
Const $NETWORK_CONNECTIONS = 0x31
$objShell = ObjCreate("Shell.Application")
$colConnections = $objShell.Namespace($NETWORK_CONNECTIONS)
For $objConn in $colConnections.Items
  If $objConn.Name = $strNetConn Then
   $objNetwork = $objConn
   ExitLoop
  EndIf
Next
$objDisable = $Nothing
For $objVerb in $objNetwork.verbs
  if $objVerb.name = $strDisable Then
   $objDisable = $objVerb
   ExitLoop
  EndIf
Next
If $objDisable <> $Nothing Then $objDisable.DoIt
Sleep(3000)
EndFunc
Edited by iviikel
Link to comment
Share on other sites

  • Moderators

The line you are referencing works just fine for me. Please post the exact verbiage of the error you're seeing. I see errors in your script, but not on that line.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

After a little more error checking I found that the output of the wmi query was correct, The For loop looking for connection status was wrong as it was always looking at the last entry setting the status. After a little changing around its now working, here is the working example not yet cleaned up.

Dim $objDim, $objWMIService
Dim $colLAN, $colWiFi
Dim $objWiFi, $objLAN
Dim $strWirelessName
Dim $State = ""
Dim $wireStatus = ""
Dim $wifiStatus = ""
Dim $speed
;Dim $oVBS
$oVBS = ObjCreate("ScriptControl")
$oVBS.language = "VBScript"
Global Const $Nothing = $oVBS.eval("Nothing")
$oVBS = $Nothing
While 1
$wireStatus= False
$colLAN = ""
$strWirelessName = GetWirelessName()
$objWMIService = ObjGet("winmgmts:\root\cimv2")
$colLAN = $objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID LIKE 'Local Area Connection%' and PhysicalAdapter='True'")
For $testLAN in $colLAN
    $Name= $testLAN.NetConnectionID
    $status=$testLAN.NetConnectionStatus
;~  MsgBox(1,"test",$Name & "      " & $status & "      " & $wireStatus)
    Next
$colWiFi = $objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID =" & "'" & $strWirelessName & "'" & " and PhysicalAdapter='True'")
For $objWiFi In $colWiFi
  If $objWiFi.Netconnectionstatus = 2 Then
   $wifiStatus = True
   ;MsgBox(0,"","WiFi Connected")
  Else
   $wifiStatus = False
   ;MsgBox(0,"","WiFi DIS-Connected")
  EndIf
Next
For $objLAN in $colLAN
  If $objLAN.Netconnectionstatus=2 Then
   $wireStatus = True
  EndIf
Next
If $wireStatus <> True THEN $wireStatus = False
If $wireStatus = False And $wifiStatus = False Then
  EnableWireless($strWirelessName)
ElseIf $wireStatus = True And $wifiStatus = True Then
  DisableWireless($strWirelessName)
EndIf
Sleep(3000)
WEnd
Func GetWirelessName()
Local $strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
Local $objReg
Local $arrSubKeys[25]
Local $SubKey
Local $strValueName
Local $dwValue
Local $strValue
Local $intRet
Const $HKLM = "HKEY_LOCAL_MACHINE\"
    For $i = 1  to UBound($arrSubKeys)
    Local $var = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}", $i)
    IF @error <> 0 Then ExitLoop
        $arrSubKeys[$i] = $var
        ReDim $arrSubKeys[$i+2]
    Next
$objReg = ObjGet("winmgmts:.rootdefault:StdRegProv")
;~ For $SubKey In $arrSubKeys
    For $i = 1 to UBound($arrSubKeys)
  $strValueName = "MediaSubType"
  $Dwvalue = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" & $arrSubKeys[$i] & "\Connection","MediaSubType")
  If $dwValue = 2 Then
   $strValueName = "Name"
   $strValue = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" & $arrSubKeys[$i] & "\Connection", $strValueName)
   ExitLoop
   EndIf
Next
Return($strValue)
EndFunc
Func EnableWireless($strNetConn)
Local $objShell, $colConnections, $objConn, $objNetwork, $objEnable, $objVerb
Local $strEnable = "En&able"
Const $NETWORK_CONNECTIONS = "0x31"
$objShell = ObjCreate("Shell.Application")
$colConnections = $objShell.Namespace(0x31)
For $objConn In $colConnections.Items
  If $objConn.Name = $strNetConn Then
   $objNetwork = $objConn
   ExitLoop
  EndIf
Next
$objEnable = $Nothing
;Enable NIC
For $i In $objConn.verbs
  If $i.name = $strEnable Then
   $objEnable = $i
   ExitLoop
  EndIf
Next
If $objEnable <> $Nothing Then $objEnable.DoIt
Sleep(5000)
EndFunc
Func DisableWireless($strNetConn)
Local $objShell, $colConnections, $objConn, $objNetwork, $objDisable, $objVerb
Local $strDisable = "Disa&ble"
Const $NETWORK_CONNECTIONS = 0x31
$objShell = ObjCreate("Shell.Application")
$colConnections = $objShell.Namespace($NETWORK_CONNECTIONS)
For $objConn in $colConnections.Items
  If $objConn.Name = $strNetConn Then
   $objNetwork = $objConn
   ExitLoop
  EndIf
Next
$objDisable = $Nothing
For $objVerb in $objNetwork.verbs
  if $objVerb.name = $strDisable Then
   $objDisable = $objVerb
   ExitLoop
  EndIf
Next
If $objDisable <> $Nothing Then $objDisable.DoIt
Sleep(3000)
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...