Jump to content

If 2 NIC connected then disable 1 network adaptor


Recommended Posts

Dear Professionals, I need an AutoIt Script to see if there are two active network connections ( Local Area Network and Wireless Network Connection ) then it should disable wireless network by default.

I have searched the whole internet but I couldn't find such a script. Then I stumbled upon AutoIt. I searched AutoIt Forums and found the script to disable the network adapter.

I just need a simple condition to check the active network connections (local area network and wireless network connection) then I can run the below script to disable the wireless network connection by default. If the user has only 1 active network adaptor connection then it should do nothing.

; Disable and Enable a Network card using 'Shell.Application'
;
; See also: http://blog.mvpcn.net/icuc88/articles/244.aspx
;
; To do: Rewrite this into a UDF. Parameters: $oLanConnection and $bEnabled


$oLanConnection = "Local Area Connection"; Change this to the name of the adapter to be disabled !
$bEnable = False             ; Change this to 'false' to DISABLE the network adapter

if @OSType<>"WIN32_NT" then
    Msgbox(0,"","This script requires Windows 2000 or higher.")
    exit
endif


if @OSVersion="WIN_2000" then
    $strFolderName = "Network and Dial-up Connections"
else
    $strFolderName = "Network Connections"; Windows XP
endif

Select 
    Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", @OSLang) ; English (United States)
       $strEnableVerb  = "En&able"
       $strDisableVerb = "Disa&ble"

; Add here the correct Verbs for your Operating System Language
EndSelect


;Virtual folder containing icons for the Control Panel applications. (value = 3)
Const $ssfCONTROLS = 3 

$ShellApp = ObjCreate("Shell.Application")
$oControlPanel = $shellApp.Namespace($ssfCONTROLS)


; Find 'Network connections' control panel item
$oNetConnections=""

For $FolderItem in $oControlPanel.Items
    If $FolderItem.Name = $strFolderName then
        $oNetConnections = $FolderItem.GetFolder
        Exitloop
    Endif
Next

      
If not IsObj($oNetConnections) Then
    MsgBox(0,"Error","Couldn't find " & $strFolderName & " folder." )
    Exit
EndIf
        

For $FolderItem In $oNetConnections.Items
    If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then
        $oLanConnection = $FolderItem
        Exitloop
    EndIf
Next

If not IsObj($oLanConnection) Then
     MsgBox(0,"Error","Couldn't find " & $oLanConnection & " Item." )
     Exit
EndIf

$oEnableVerb=""
$oDisableVerb=""

For $Verb In $oLanConnection.Verbs
 If $Verb.Name = $strEnableVerb Then
   $oEnableVerb = $Verb
 EndIf
 If $Verb.Name = $strDisableVerb Then
   $oDisableVerb = $Verb
 EndIf
Next

If $bEnable then
 If IsObj($oEnableVerb) Then $oEnableVerb.DoIt  ; Enable network card
Endif

If not $bEnable then
 If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
EndIf

Msgbox(0,"","Your local area network has been disabled")

Sleep(1000)

I will really appreciate. I am eagerly looking forward for your prompt replies and co-operation.

Link to comment
Share on other sites

I don't know whether this will help you:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Net_Description, $colItems, $colItem, $ping, $x
    $ping = Ping($srv)
    If $ping Then
        $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count
        If $colItems > 1 Then
            $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30)
            If IsObj($colItems) Then
                For $objItem In $colItems
                    $Net_Description &= $objItem.Description & @CRLF
                Next
                SetError(0)
                MsgBox(0, "Network Adapter Information", "More than 1 enabled network adapters found: " & @CRLF & @CRLF & $Net_Description)
                Return $Net_Description
            Else
                SetError(1)
                Return "Error!"
            EndIf
        Else
            MsgBox(0, "Information", "Only one network adapter found")
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thank you so much UEZ this is very close to what I need. I was wondering if you could please kindly help me and modify the code to check if local area connection and wireless network connection are active/enabled then I can use the above script to disable the wireless network connection. Since you're a professional it will take you just few minutes and I will really appreciate it.

Looking forward for your help.

Link to comment
Share on other sites

And what about this version:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Net_Description, $colItems, $colItem, $ping, $msg
    $ping = Ping($srv)
    If $ping Then
        If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count > 1 Then
            $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30)
            If IsObj($colItems) Then
                For $objItem In $colItems
                    $Net_Description &= $objItem.Description & @CRLF
                Next
                SetError(0)
                $msg = "More than one enabled network adapter found: " & @CRLF & @CRLF & $Net_Description & @CRLF
                If StringInStr($Net_Description, "Wireless Network Connection") > 0 Then
                    MsgBox(0, "Network Adapter Information", $msg & "Wireless Network Connection can be disabled")
                Else
                    MsgBox(0, "Network Adapter Information", $msg)
                EndIf
                Return $Net_Description
            Else
                SetError(1)
                Return "Error!"
            EndIf
        Else
            MsgBox(0, "Information", "Only one network adapter found")
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc

Br,

UEZ

PS: I'm far away being a professional!

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

It works awesome!... :blink:

after i made a small change, replaced "Wireless Network Connection" with "Dell Wireless 1397 WLAN Mini-Card" because it's comparing against the description of the network card.

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Net_Description, $colItems, $colItem, $ping, $msg
    $ping = Ping($srv)
    If $ping Then
        If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count > 1 Then
            $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30)
            If IsObj($colItems) Then
                For $objItem In $colItems
                    $Net_Description &= $objItem.Description & @CRLF
                Next
                SetError(0)
                $msg = "More than one enabled network adapter found: " & @CRLF & @CRLF & $Net_Description & @CRLF
                If StringInStr($Net_Description, "Dell Wireless 1397 WLAN Mini-Card") > 0 Then
                    MsgBox(0, "Network Adapter Information", $msg & "Wireless Network Connection can be disabled")
                Else
                    MsgBox(0, "Network Adapter Information", $msg)
                EndIf
                Return $Net_Description
            Else
                SetError(1)
                Return "Error!"
            EndIf
        Else
            MsgBox(0, "Information", "Only one network adapter found")
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc

There is a problem I can't use the above script because every user has a different Wireless LAN Card. I need to compare against the NetconnectionID. for this I made changes in the below line.

For $objItem In $colItems

$Net_Description &= $objItem.NetConnectionID& @CRLF

Next

It didn't work and gave me an error message ;) Could you please kindly look into this once again?? Can we compare against the NetConnectionID and not the description?? forexample: "Wireless Network Connection" and not Dell Wireless 1397 WLAN Mini-Card. Thanks alot once again!

Please let me know if you have any question or need more explanation.

Edited by Mukhtar
Link to comment
Share on other sites

You can query using AdapterTypeID from Win32_NetworkAdapter class to differentiate the types of network adapters that are active.

Thank you omikron48 however unfortunately im not a programmer. If you could please kindly modify UEZ's code I will really appreciate.

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Net_Description, $colItems, $colItem, $ping, $msg
    $ping = Ping($srv)
    If $ping Then
        If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count > 1 Then
            $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30)
            If IsObj($colItems) Then
                For $objItem In $colItems
                    $Net_Description &= $objItem.Description & @CRLF
                Next
                SetError(0)
                $msg = "More than one enabled network adapter found: " & @CRLF & @CRLF & $Net_Description & @CRLF
                If StringInStr($Net_Description, "Dell Wireless 1397 WLAN Mini-Card") > 0 Then
                    MsgBox(0, "Network Adapter Information", $msg & "Wireless Network Connection can be disabled")
                Else
                    MsgBox(0, "Network Adapter Information", $msg)
                EndIf
                Return $Net_Description
            Else
                SetError(1)
                Return "Error!"
            EndIf
        Else
            MsgBox(0, "Information", "Only one network adapter found")
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc
Edited by Mukhtar
Link to comment
Share on other sites

Here the modified version:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Description, $NetConnectionID, $colItems, $colItem, $ping, $msg
    $ping = Ping($srv)
    If $ping Then
    If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count > 1 Then
    $colItems = $objWMIService.ExecQuery("SELECT Description, NetConnectionID FROM Win32_NetworkAdapter WHERE NetConnectionStatus >= 0", "WQL", 0x30)
    If IsObj($colItems) Then
    For $objItem In $colItems
    $NetConnectionID &= $objItem.NetConnectionID & @CRLF
                    $Description &= $objItem.Description & @CRLF
    Next
    SetError(0)
    $msg = "More than one enabled network adapter found: " & @CRLF & @CRLF & $Description & @CRLF
    If StringInStr($NetConnectionID, "Dell Wireless 1397 WLAN Mini-Card") > 0 Then
    MsgBox(0, "Network Adapter Information", $msg & "Dell Wireless 1397 WLAN Mini-Card can be disabled")
    Else
    MsgBox(0, "Network Adapter Information", $msg)
    EndIf
    Return $NetConnectionID
    Else
    SetError(1)
    Return "Error!"
    EndIf
    Else
    MsgBox(0, "Information", "Only one network adapter found")
    EndIf
    Else
    SetError(1)
    Return "Host not reachable"
    EndIf
EndFunc

I hope it works for you :blink:

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • 2 weeks later...

Hi UEZ, this is exactly what ive been looking for... ;) i found this forum after alot of searching. i put together the codes and made small change in the condition. you can see it below:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Description, $NetConnectionID, $colItems, $colItem, $ping, $msg
    $ping = Ping($srv)
    If $ping Then
    If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count > 1 Then
    $colItems = $objWMIService.ExecQuery("SELECT Description, NetConnectionID FROM Win32_NetworkAdapter WHERE NetConnectionStatus >= 0", "WQL", 0x30)
    If IsObj($colItems) Then
    For $objItem In $colItems
    $NetConnectionID &= $objItem.NetConnectionID & @CRLF
                    $Description &= $objItem.Description & @CRLF
    Next
    SetError(0)
    $msg = "More than one enabled network adapter found: " & @CRLF & @CRLF & $NetConnectionID & @CRLF
    If (StringInStr($NetConnectionID, "Local Area Connection") > 0) AND (StringInStr($NetConnectionID, "Wireless Network Connection")> 0)  Then
    MsgBox(0, "Network Adapter Information", $msg & "Wireless Network Connection can be disabled")
; Disable and Enable a Network card using 'Shell.Application'
;
; See also: http://blog.mvpcn.net/icuc88/articles/244.aspx
;
; To do: Rewrite this into a UDF. Parameters: $oLanConnection and $bEnabled


$oLanConnection = "Wireless Network Connection"; Change this to the name of the adapter to be disabled !
$bEnable = False             ; Change this to 'false' to DISABLE the network adapter

if @OSType<>"WIN32_NT" then
    Msgbox(0,"","This script requires Windows 2000 or higher.")
    exit
endif


if @OSVersion="WIN_2000" then
    $strFolderName = "Network and Dial-up Connections"
else
    $strFolderName = "Network Connections"; Windows XP
endif

Select 
    Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", @OSLang) ; English (United States)
       $strEnableVerb  = "En&able"
       $strDisableVerb = "Disa&ble"

; Add here the correct Verbs for your Operating System Language
EndSelect


;Virtual folder containing icons for the Control Panel applications. (value = 3)
Const $ssfCONTROLS = 3 

$ShellApp = ObjCreate("Shell.Application")
$oControlPanel = $shellApp.Namespace($ssfCONTROLS)


; Find 'Network connections' control panel item
$oNetConnections=""

For $FolderItem in $oControlPanel.Items
    If $FolderItem.Name = $strFolderName then
        $oNetConnections = $FolderItem.GetFolder
        Exitloop
    Endif
Next

      
If not IsObj($oNetConnections) Then
    MsgBox(0,"Error","Couldn't find " & $strFolderName & " folder." )
    Exit
EndIf
        

For $FolderItem In $oNetConnections.Items
    If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then
        $oLanConnection = $FolderItem
        Exitloop
    EndIf
Next

If not IsObj($oLanConnection) Then
     MsgBox(0,"Error","Couldn't find " & $oLanConnection & " Item." )
     Exit
EndIf

$oEnableVerb=""
$oDisableVerb=""

For $Verb In $oLanConnection.Verbs
 If $Verb.Name = $strEnableVerb Then
   $oEnableVerb = $Verb
 EndIf
 If $Verb.Name = $strDisableVerb Then
   $oDisableVerb = $Verb
 EndIf
Next

If $bEnable then
 If IsObj($oEnableVerb) Then $oEnableVerb.DoIt  ; Enable network card
Endif

If not $bEnable then
 If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
EndIf

Msgbox(0,"Wireless Disconnected, By default you'll be connected to LAN","You've been disconnected from Wireless Network Connection because you had more than 1 Network interface connected. If you want to use WLAN, please disconnect LAN cable and then connect WLan")

;Sleep(1000)

    Else
    MsgBox(0, "Network Adapter Information", $msg)

EndIf
    Return $NetConnectionID
    Else
    SetError(1)
    Return "Error!"
    EndIf
    Else
    MsgBox(0, "Information", "Only one network adapter found")
    EndIf
    Else
    SetError(1)
    Return "Host not reachable"
    EndIf
EndFunc

and unfortunately it has 1 problem. It does not check if 2 NIC connected which means plugged in, then it should disable the WLAN. it only checks if 2 NIC enabled.. there is difference between enabled and being connected to 2 networks (LAN & WLAN)?

Is it possible to check if it's connected to LAN and WLAN instead of checking for whether it's enabled? Can you help me guys pleasssseee?

Link to comment
Share on other sites

Hi UEZ, this is exactly what ive been looking for... ;) i found this forum after alot of searching. i put together the codes and made small change in the condition.

You are welcome.

and unfortunately it has 1 problem. It does not check if 2 NIC connected which means plugged in, then it should disable the WLAN. it only checks if 2 NIC enabled.. there is difference between enabled and being connected to 2 networks (LAN & WLAN)?

Is it possible to check if it's connected to LAN and WLAN instead of checking for whether it's enabled? Can you help me guys pleasssseee?

Try this code to get the Wireless Network Connection:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Description, $colItems, $colItem, $ping
    $ping = Ping($srv)
    If $ping Then

        $colItems = $objWMIService.ExecQuery("SELECT Description, NetConnectionID FROM Win32_NetworkAdapter WHERE NetConnectionID = 'Wireless Network Connection' And NetConnectionStatus > 0", "WQL", 0x30)
        If IsObj($colItems) Then
            For $objItem In $colItems
                MsgBox(0, "Wireless Network Adapter", $objItem.Description, 5)
            Next
            SetError(0)
        Else
            SetError(1)
            Return "Error!"
        EndIf

    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc ;==>GetWMI

This should list the network adapter "Wireless Network Connection" independently whether it has connection or not.

You have to modify the NetConnectionID name appropriate your NetConnectionID! In my case it is "Wireless Network Connection".

Currently I don't know how to disabled the WLAN adapter on OS Vista+ using WMI or Shell objects (I have never needed it!).

BR,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thank you UEZ for replying..

This should list the network adapter "Wireless Network Connection" independently whether it has connection or not.

I checked and It listed the network adapter whether it is enabled or not.

I want whether it is connected to the network or not.

I need a Condition to check if Local Area Connection and Wireless Area Connection both are connected to the network.

Thanks for your time in advance ;) please let me know if u have any confusion and/or need anything.

Link to comment
Share on other sites

  • 1 month later...

You are welcome.

Try this code to get the Wireless Network Connection:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $Description, $colItems, $colItem, $ping
    $ping = Ping($srv)
    If $ping Then

        $colItems = $objWMIService.ExecQuery("SELECT Description, NetConnectionID FROM Win32_NetworkAdapter WHERE NetConnectionID = 'Wireless Network Connection' And NetConnectionStatus > 0", "WQL", 0x30)
        If IsObj($colItems) Then
            For $objItem In $colItems
                MsgBox(0, "Wireless Network Adapter", $objItem.Description, 5)
            Next
            SetError(0)
        Else
            SetError(1)
            Return "Error!"
        EndIf

    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc ;==>GetWMI

This should list the network adapter "Wireless Network Connection" independently whether it has connection or not.

You have to modify the NetConnectionID name appropriate your NetConnectionID! In my case it is "Wireless Network Connection".

Currently I don't know how to disabled the WLAN adapter on OS Vista+ using WMI or Shell objects (I have never needed it!).

BR,

UEZ

UEZ not sure if your still looking at this but I did find out an easy way to do this in Vista+ check my modified code, uses a different WMI Class but its pretty simple.

#RequireAdmin
Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]
$AdaptDescription = "'Cisco Anyconnect VPN Virtual Miniport Adapter for Windows'"

$objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $ip & "\root\cimv2")

GetWMI($ip)

Func GetWMI($srv)
    Local $colItems
    If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapter WHERE NetEnabled = True AND Description = " & $AdaptDescription).Count > 0 Then
        $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapter WHERE NetEnabled = True AND Description = " & $AdaptDescription)
        If IsObj($colItems) Then
            For $objItem In $colItems
                    $objItem.Disable()
                Next
        EndIf           
    EndIf
EndFunc
Link to comment
Share on other sites

joe1981al,

thanks for the hint how to disable the network adapter using WMI! When I found the time I will test it!

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

joe1981al,

thanks for the hint how to disable the network adapter using WMI! When I found the time I will test it!

Br,

UEZ

I'm using on Win7 perfectly, I wrote this to be cross platform, I call the AdapterKill function with variables from an INI file so a compiled file can be distributed and then customized with ini for the variables. It seems that all the stuff I want to do with autoit is out on the fringes and I have to come up with ways to make it happen...

#RequireAdmin


Func AdapterKill($AdaptDescription, $ConnectionName, $bEnable = false)
    ;$AdaptDescription = "' '"  ;for Vista/Win7 Actual Adapter Name must be single quoted inside double-quotes
    ;$ConnectionName = " " ;for PreVista Connection Name in Net Connections
    
    if @OSType<>"WIN32_NT" then
        SetError(1)
    endif
    if @OSVersion="WIN_2000" then
        $strFolderName = "Network and Dial-up Connections"
        PreVistaDisable($ConnectionName,$strFolderName,$bEnable)
        Return 0
    elseif StringInStr(@OSVersion, "WIN_XP") then
        $strFolderName = "Network Connections"
        PreVistaDisable($ConnectionName,$strFolderName,$bEnable)
        Return 0
    else
        VistaDisable($AdaptDescription,$bEnable)
        Return 0
    endif
EndFunc


Func VistaDisable($Adapter, $bEnable = false)
    $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    If $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapter WHERE NetEnabled = True AND Description = " & $Adapter).Count > 0 Then
        $colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapter WHERE NetEnabled = True AND Description = " & $Adapter)
        If IsObj($colItems) Then
            For $objItem In $colItems
                If $bEnable = False Then
                    $objItem.Disable()
                Else
                    $objItem.Enable()
                EndIf
                
                Next
            Return 0
        EndIf           
    EndIf
EndFunc

Func PreVistaDisable($oLanConnection,$strFolderName, $bEnable = false)
    ;$bEnable = false             ; Change this to 'false' to DISABLE the network adapter

    Select 
        Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009, 2409,2809,2c09,3009,3409", @OSLang) ; English (United States)
           $strEnableVerb  = "En&able"
           $strDisableVerb = "Disa&ble"

    ; Add here the correct Verbs for your Operating System Language
    EndSelect


    ;Virtual folder containing icons for the Control Panel applications. (value = 3)
    Const $ssfCONTROLS = 3 

    $ShellApp = ObjCreate("Shell.Application")
    $oControlPanel = $shellApp.Namespace($ssfCONTROLS)


    ; Find 'Network connections' control panel item
    $oNetConnections=""

    For $FolderItem in $oControlPanel.Items
        If $FolderItem.Name = $strFolderName then
            $oNetConnections = $FolderItem.GetFolder
            Exitloop
        Endif
    Next

          
    If not IsObj($oNetConnections) Then
        SetError(1)
    EndIf
            

    For $FolderItem In $oNetConnections.Items
        If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then
            $oLanConnection = $FolderItem
            Exitloop
        EndIf
    Next

    If not IsObj($oLanConnection) Then
        SetError(1)
    EndIf

    $oEnableVerb=""
    $oDisableVerb=""

    For $Verb In $oLanConnection.Verbs
     If $Verb.Name = $strEnableVerb Then
       $oEnableVerb = $Verb
     EndIf
     If $Verb.Name = $strDisableVerb Then
       $oDisableVerb = $Verb
     EndIf
    Next

    If $bEnable then
     If IsObj($oEnableVerb) Then $oEnableVerb.DoIt  ; Enable network card
    Endif

    If not $bEnable then
     If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
    EndIf

    Sleep(1000)
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...