Jump to content

Disable WLAN script help


Recommended Posts

I found the below script which, when the network cable is unplugged will enable the WLAN and disable when plugged back in.  I am wondering if anyone knows how I can trigger this when the program starts.  I think it watches for an event, so I can't just run the Func

Thanks

#RequireAdmin

Dim $strComputer, $SINK1, $SINK2, $objWMIService
$strComputer = "127.0.0.1"
$SINK1 = ObjCreate("WbemScripting.SWbemSink")
$SINK2 = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK1, "SINK_")
ObjEvent($SINK2, "SINK_")
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\wmi")
If Not @error Then
    $objWMIService.ExecNotificationQueryAsync ($SINK1, "SELECT * FROM MSNdis_StatusMediaConnect")
    $objWMIService.ExecNotificationQueryAsync ($SINK2, "SELECT * FROM MSNdis_StatusMediaDisconnect")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)
While 1
    Sleep(10000)
WEnd
;******************************************************************************
Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    ;Trap asynchronous events.
    Switch $objLatestEvent.Path_.Class()
        Case "MSNdis_StatusMediaDisconnect"
            If $objLatestEvent.InstanceName = "Realtek PCIe GBE Family Controller" Then
               $CMD = 'netsh interface set interface "Wireless Network Connection" admin=ENABLE'
               RunWait(@ComSpec & " /c " & $CMD)
            EndIf
        Case "MSNdis_StatusMediaConnect"
            If $objLatestEvent.InstanceName = "Realtek PCIe GBE Family Controller" Then
               $CMD = 'netsh interface set interface "Wireless Network Connection" admin=DISABLE'
               RunWait(@ComSpec & " /c " & $CMD)
            EndIf
    EndSwitch
EndFunc   ;==>SINK_OnObjectReady

 

Link to comment
Share on other sites

I have no laptop to test my code, but it should work (I made two event handlers for more simplicity :

#RequireAdmin


Global $sEthernetAdapter =  "Realtek PCIe GBE Family Controller"
Global $sWirelesstAdapter =  "Wireless Network Connection"

_NetworkSetState($sWirelesstAdapter, 0) ; <--- disable the wireless adapter


Dim $strComputer, $SINK1, $SINK2, $objWMIService
$strComputer = "127.0.0.1"
$SINK1 = ObjCreate("WbemScripting.SWbemSink")
$SINK2 = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK1, "OnConnect_")
ObjEvent($SINK2, "OnDisconnect_")

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\wmi")
If Not @error Then
    $objWMIService.ExecNotificationQueryAsync ($SINK1, "SELECT * FROM MSNdis_StatusMediaConnect")
    $objWMIService.ExecNotificationQueryAsync ($SINK2, "SELECT * FROM MSNdis_StatusMediaDisconnect")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)
While 1
    Sleep(10000)
WEnd



Func OnConnect_OnObjectReady($objLatestEvent, $objAsyncContext)
    If $objLatestEvent.InstanceName = $sEthernetAdapter Then _NetworkSetState($sWirelesstAdapter, 0)  ; <--- disable the wireless adapter
EndFunc

Func OnDisconnect_OnObjectReady($objLatestEvent, $objAsyncContext)
    If $objLatestEvent.InstanceName = $sEthernetAdapter Then _NetworkSetState($sWirelesstAdapter, 1) ; <--- enable the wireless adapter
EndFunc

Func _NetworkSetState($sAdapter, $iState)
    Local $cmd = 'netsh interface set interface "' & $sAdapter & '" admin=' & ( $iState ? "ENABLE" : "DISABLE")
    RunWait(@ComSpec & " /c " & $CMD)
EndFunc

So you just have to call the _NetworkSetState function at the beginning of your code

You can also look at the Network configuration UDF to avoid the use of an external program

Edited by jguinch
Link to comment
Share on other sites

More than one way to skin a cat :)  I made one too a while back for a laptop that the built in bios wan/lan switching was not working.

I used the index values for the adapters in the script and saved them to the registry so I could use the same script for any computer and just set the index in the registry for a computer one time when I set it up.

 

IT Portion: Find my index values

#RequireAdmin

Run(@ComSpec & " /k wmic nic get name, index")

Script for Wan/Lan Switching

#RequireAdmin
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>

#pragma compile(Icon, E:\Users\it022565\Desktop\Desktop\System Tools\iconsext\icons\imageres_34.ico)

$sLanIndex = RegRead("HKLM\SOFTWARE\BoCC Automation\Network", "Lan Index")
$sWanIndex = RegRead("HKLM\SOFTWARE\BoCC Automation\Network", "Wan Index")

If $sLanIndex = "" Then
    $sLanIndex = InputBox("BoCC Automation", "Enter Lan Interface Index Number", "")
    $sWanIndex = InputBox("BoCC Automation", "Enter Wan Interface Index Number", "")
    RegWrite("HKLM\SOFTWARE\BoCC Automation\Network", "Lan Index", "REG_SZ", $sLanIndex)
    RegWrite("HKLM\SOFTWARE\BoCC Automation\Network", "Wan Index", "REG_SZ", $sWanIndex)
EndIf


AdlibRegister("CheckConnections", 5000)

;wmic nic get name, index, netconnectionstatus
;wmic nic where index=x get netconnectionstatus


While 1
    Sleep(10)
WEnd


Func CheckLan()
Local $iPID = Run(@comspec & ' /c wmic nic where index=' & $sLanIndex & ' get netconnectionstatus', "", @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
Local $sOutput = StdoutRead($iPID)
;MsgBox(0, "", $sOutput)
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
;_ArrayDisplay($aArray)
Return $aArray[2]
EndFunc


Func CheckWan()
Local $iPID = Run(@comspec & ' /c wmic nic where index=' & $sWanIndex & ' get netconnectionstatus', "", @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
Local $sOutput = StdoutRead($iPID)
;MsgBox(0, "", $sOutput)
Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
;_ArrayDisplay($aArray)
Return $aArray[2]
EndFunc

Func CheckConnections()
    If CheckLan() = 2 Then
        If CheckWan() = 2 Then
            DisableWan()
        EndIf
    Else
        If CheckWan() <> 2 Then
            EnableWan()
        EndIf
    EndIf
EndFunc

Func DisableWan()
    Run(@ComSpec & ' /c wmic path win32_networkadapter where index=' & $sWanIndex & ' call disable', "", @SW_HIDE)
EndFunc

Func EnableWan()
    Run(@ComSpec & ' /c wmic path win32_networkadapter where index=' & $sWanIndex & ' call enable', "", @SW_HIDE)
EndFunc

 

Link to comment
Share on other sites

@jguinch Looks like that script will just disable the wireless right at startup, what if the network cable is not plugged in?

@ViciousXUSMC looking at your code now, trying to figure out how it works.  I like what I have now, I just want it to check when the program runs.

Edited by evansullivan
Link to comment
Share on other sites

Try this at startup :

Global $sEthernetAdapter =  "Realtek PCIe GBE Family Controller"
Global $sWirelesstAdapter =  "Wireless Network Connection"

Global $sEthernetAdapter =  "Realtek PCIe GBE Family Controller"
Global $sWirelesstAdapter =  "Wireless Network Connection"

Global $oWMIService = ObjGet("winmgmts:root\CIMV2")
Global $oDisconnectLAN = $oWMIService.ExecQuery("select * from Win32_NetworkAdapter Where Name = '" & $sEthernetAdapter & "' And NetConnectionStatus = 0")
If $oDisconnectLAN.Count Then _NetworkSetState($sWirelesstAdapter, 1)

 

Link to comment
Share on other sites

I added that to the top, but it doesn't seem to do anything at startup.  If the network cable is unplugged it won't enable it, and if it's plugged in it won't disable it.

Thanks for all your help, by the way!

Edit: I did find the network.au3 to include, but I get an error on line 443, since my code doesn't have that many line, I am assuming it's from the network.au3 include.

Edited by evansullivan
Link to comment
Share on other sites

Sorry, I made some errors. Try this one please (replace $sEthernetAdapter and $sWirelessAdapter values) . It works well for me:

#RequireAdmin

Global $objWMIService = ObjGet("winmgmts:\\.\root\wmi")
Global $oCIMV2 = ObjGet("winmgmts:\\.\root\CIMV2")

Global $sEthernetAdapter =  "Broadcom NetXtreme 57xx Gigabit Controller"
Global $sWirelesstAdapter =  "Intel(R) WiFi Link 5100 AGN"

Global $oDisconnectLAN = $oCIMV2.ExecQuery("select * from Win32_NetworkAdapter Where Name = '" & $sEthernetAdapter & "' And NetConnectionStatus = 0")
If $oDisconnectLAN.Count Then
   _NetworkSetState($sWirelesstAdapter, 1)
Else
   _NetworkSetState($sWirelesstAdapter, 0)
EndIf

Dim $strComputer, $SINK1, $SINK2, $objWMIService
$strComputer = "127.0.0.1"
$SINK1 = ObjCreate("WbemScripting.SWbemSink")
$SINK2 = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK1, "OnConnect_")
ObjEvent($SINK2, "OnDisconnect_")


If Not @error Then
    $objWMIService.ExecNotificationQueryAsync ($SINK1, "SELECT * FROM MSNdis_StatusMediaConnect")
    $objWMIService.ExecNotificationQueryAsync ($SINK2, "SELECT * FROM MSNdis_StatusMediaDisconnect")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)
While 1
    Sleep(10000)
WEnd


Func OnConnect_OnObjectReady($objLatestEvent, $objAsyncContext)
    If $objLatestEvent.InstanceName = $sEthernetAdapter Then
       _NetworkSetState($sWirelesstAdapter, 0)  ; <--- disable the wireless adapter
    EndIf
EndFunc

Func OnDisconnect_OnObjectReady($objLatestEvent, $objAsyncContext)
    If $objLatestEvent.InstanceName = $sEthernetAdapter Then
       _NetworkSetState($sWirelesstAdapter, 1) ; <--- enable the wireless adapter
    EndIf
EndFunc

Func _NetworkSetState($sAdapter, $iState)
    Local $oAdapters = $oCIMV2.ExecQuery("select * from Win32_NetworkAdapter where Name = '" & $sAdapter & "'")
    If Not IsObj($oAdapters) Then Return SetError(1, 0, 0)

    For $oAdapter In $oAdapters
       Return ($iState ? $oAdapter.Enable : $oAdapter.Disable)
    Next
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...