Jump to content

Enable/Disable Wireless Card


Recommended Posts

Hi,

I am trying to enable and disable my wireless card using the Windows Management Instrumentation classes. I used the AutoIt ScriptOMatic tool to generate parts of the attached code and I modified it so it displays the status of my Broadcom 802.11a/b/g WLAN card. The program searches for the keyword "802.11" and then displays the appropriate information.

How can I now with this information enable and disable the adapter?

Thanks

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$wireless=""
$Output=""

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemDriver", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
        $Output = $objItem.Caption & @CRLF
        $result = StringInStr($Output, "802.11", 0,1)
    If $result <> 0 Then
        $wireless = $objItem.Caption & @CRLF
        $started = "Started: " & $objItem.Started & @CRLF
        $state = "State: " & $objItem.State & @CRLF
        $status = "Status: " & $objItem.Status & @CRLF
        Else
        $result = 0
    EndIf
        $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration" )
Endif
MsgBox (0, "Test", $wireless & $started & $state & $status)
Link to comment
Share on other sites

Thanks for your suggestion, but that does not help me. I have seen numerous posts like that one, but they are all based on the "Local Area Network" caption. I need to find a way in which I can enable/disable the adapter if I search by the adapter name (the one listed by the ipconfig command). This way the Windows name for the connection could be anything and it would not matter because my script would be bound to a particular adapter.

More suggestions please.

Link to comment
Share on other sites

...I need to find a way in which I can enable/disable the adapter if I search by the adapter name (the one listed by the ipconfig command)...

Use your WMI script to find the adapter by a string within its Description --- like that returned by the "ipconfig /all" command:

Ethernet adapter Wireless Network Connection:

Connection-specific DNS Suffix . :

Description . . . . . . . . . . . :

....

Then use AutoIt's STDIO functions with the "ipconfig /all" command to resolve the value needed to feed the $sConnectionName variable in the script that I referenced...

...or maybe I still don't understand your request...

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Thanks for your reply, again!

Will that allow me to enable/disable the wireless card? If it will, then how do I use the AutoIt's STDIO functions?

The code that I attached finds the name of the adapter, but now I just need to either enable/disable that particular adapter.

Anymore suggestions?

Link to comment
Share on other sites

...The code that I attached finds the name of the adapter, ...

... just not a name that I can make use of :-(

I think that I'm now at the same brick wall that you were at in your OP.

This is the "STDIO code" that I had in mind, but the string value in your $wireless variable is not in the "ipconfig /all" output (at least not on my system). I had hoped it would be a part of the Description:

Dim $CMDoutput, $STD_output, $Descrip = "Wireless LAN Adapter"

$PID = Run(@ComSpec & " /c ipconfig /all", "", @SW_HIDE, 2)

While Not @error
    $CMDoutput &= StdoutRead($PID)
WEnd

$CMDoutputArray = StringSplit($CMDoutput, @CRLF, 1)

For $i = 1 To $CMDoutputArray[0]
    ConsoleWrite($CMDoutputArray[$i] & @LF)
    If StringInStr($CMDoutputArray[$i], $Descrip, 1) Then
        $sConnectionName = StringTrimLeft($CMDoutputArray[$i - 3], 17)
        $sConnectionName = StringTrimRight($sConnectionName, 2)
        MsgBox(0, "", $sConnectionName)
        ExitLoop
    EndIf
Next

Perhaps others in the forum know the solution to your post.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Or you can query the registry for the Nic names and use the script that herewasplato give you in post #2.

#include <GUIConstants.au3>

Dim $i, $base, $key, $name

; == GUI generated with Koda ==
$Form1 = GUICreate("Nic", 176, 89, -1, -1, -1, $WS_EX_TOOLWINDOW)
GUICtrlCreateLabel("Pick Network Connection", 8, 8, 157, 17, $SS_CENTER)
$Combo1 = GUICtrlCreateCombo("", 8, 24, 161, 21)
$Button1 = GUICtrlCreateButton("Enable", 8, 56, 75, 25)
$Button2 = GUICtrlCreateButton("Disable", 96, 56, 75, 25)

;Get Network Connection Names
$base = "HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
While 1
    $i += 1
    $key = RegEnumKey($base, $i)
    If @error <> 0 Then ExitLoop
    $name = RegRead($base & "\" & $key & "\Connection", "Name")
    $type = RegRead($base & "\" & $key & "\Connection", "PnpInstanceID")
    If StringLeft($key, 1) = "{" Then
        If StringLeft($type, 4) = "PCI\" Or StringLeft($type, 8) = "ROOT\NET" Then
            GUICtrlSetData($Combo1, $name & "|")
        EndIf
    EndIf
WEnd
GUISetState(@SW_SHOW)

While 1
    $msg = GuiGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    Case $Button1
        If GUICtrlRead($Combo1) <> '' Then NicToggle(1, GUICtrlRead($Combo1))
    Case $Button2
        If GUICtrlRead($Combo1) <> '' Then NicToggle(0, GUICtrlRead($Combo1))
    Case Else
        ;;;;;;;
    EndSwitch
WEnd
Exit

Func NicToggle($Toggle, $sConnectionName = "Local Area Connection", $sNetworkFolder = "Network Connections")    
    $ssfCONTROLS = 3
    $sEnableVerb = "En&able"
    $sDisableVerb = "Disa&ble"
    $shellApp = ObjCreate("shell.application")
    $oControlPanel = $shellApp.Namespace ($ssfCONTROLS)
    $oNetConnections = "nothing"
    
    For $folderitem In $oControlPanel.items
        If $folderitem.name = $sNetworkFolder Then
            $oNetConnections = $folderitem.getfolder
            ExitLoop
        EndIf
    Next
    If $oNetConnections = "nothing" Then
        MsgBox(48, "Error", "Couldn't find " & $sNetworkFolder & " folder")
        Exit
    EndIf
    
    $oLanConnection = "nothing"
    For $folderitem In $oNetConnections.items
        If StringLower($folderitem.name) = StringLower($sConnectionName) Then
            $oLanConnection = $folderitem
            ExitLoop
        EndIf
    Next
    If $oLanConnection = "nothing" Then
        MsgBox(48, "Error", "Couldn't find '" & $sConnectionName & "' item")
        Exit
    EndIf
    
    $bEnabled = True
    $oEnableVerb = "nothing"
    $oDisableVerb = "nothing"
    $s = "Verbs: " & @CRLF
    For $verb In $oLanConnection.verbs
        $s = $s & @CRLF & $verb.name
        
    ;enables
        If $verb.name = $sEnableVerb And $Toggle = 1 Then
            $oEnableVerb = $verb
            $oEnableVerb.DoIt
            ExitLoop
            
        ;disables
        ElseIf $verb.name = $sDisableVerb And $Toggle = 0 Then
            $oDisableVerb = $verb
            $oDisableVerb.DoIt
            ExitLoop
        Else
            MsgBox(48, "Error", "Tried to disable when already disabled" & @CRLF & "or enable when already enabled")
            Exit
        EndIf
    Next
    Sleep(1000)
EndFunc  ;==>NicToggle

This script will enable or disable the selected nic. Hopefully this will give you an idea or get you start it.

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

If all you want is Wireless card only. From the above code delete the lines

FROM

;Get Network Connection Names

TO

wend

and add the following lines:

; Generated by AutoIt Scriptomatic
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM WiFi_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
       GUICtrlSetData($Combo1, $objItem.NetConnectionID & "|")      
   Next
Endif

This WMI script will give you a list of wireless card only.

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

@Danny35d,

Thank you for your suggestions, but I think I might have to give up because it still does not work. The problem is that I am trying to run all these scripts in WinPE and this Windows version is stripped of a whole bunch of things.

I ran the program you provided in Windows XP and it worked like a charm. When I ran the same program in WinPE, I do not get the adapters listed in that drop down list.

Right now I am using a program called PENETCFG.exe to configure the network cards IP, but that program does not allow me to enable/disable the adapters and it is not very attractive because I have a small AutoIt script that goes through the settings and configures the IPs that I need depending on the task that the user chooses to run. I also wanted to get rid of this PENETCFG program if I could, but I could not find a way to access the NICs and change the IPs.

I will try and get in touch with the guy that created this PENETCFG program and see how he was able to do it. His website is http://www.geocities.com/pierremounir/ for anyone that is interested in using his tool to configure the IP for the NIC in WinPE.

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...