Jump to content
Phoenix35

Disable/Enable specific network adapters

Recommended Posts

Hi guys,

 

I'm looking for some help about syntax. I’m trying to create a script to disable some network adapters, depending on their names.

I want something like :

          if name of network adapters start with "xxxxx"

 

Share this post


Link to post
Share on other sites

if name of network adapters start with "xxxxx"

then disable network adapter

 

 

I find 'netsh.exe int set interface "name" disable', but you have to specify full name of adapter.

How can I do to disable all adapters which name start with specific characters?

 

thanks

Share this post


Link to post
Share on other sites

Easiest method is to use PowerShell example:

#RequireAdmin
_DiableNetAdapter("VMWare*", True) ;~ Disable all Network Adapters named like VMware*

Func _DiableNetAdapter($_sNetAdapter, $_bDisable = False)
    Switch $_bDisable
        Case True
            ;~ Disable Network Adapter
            RunWait('PowerShell -Command "& {Disable-NetAdapter -Name ' & $_sNetAdapter & ' -Confirm:$False}"', "", @SW_HIDE)
        Case False
            ;~ Enable Network Adapter
            RunWait('PowerShell -Command "& {Enable-NetAdapter -Name ' & $_sNetAdapter & ' -Confirm:$False}"', "", @SW_HIDE)
    EndSwitch
EndFunc

 

Share this post


Link to post
Share on other sites

I would use WMI to access all network adapters.  The where clause could be enough for your needs.  Otherwise, put the logic inside a loop.  To disable one (or more) adapter, look at the class methods.  All information are available here :

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter

 

Share this post


Link to post
Share on other sites
1 hour ago, Subz said:

Easiest method is to use PowerShell example:

#RequireAdmin
_DiableNetAdapter("VMWare*", True) ;~ Disable all Network Adapters named like VMware*

Func _DiableNetAdapter($_sNetAdapter, $_bDisable = False)
    Switch $_bDisable
        Case True
            ;~ Disable Network Adapter
            RunWait('PowerShell -Command "& {Disable-NetAdapter -Name ' & $_sNetAdapter & ' -Confirm:$False}"', "", @SW_HIDE)
        Case False
            ;~ Enable Network Adapter
            RunWait('PowerShell -Command "& {Enable-NetAdapter -Name ' & $_sNetAdapter & ' -Confirm:$False}"', "", @SW_HIDE)
    EndSwitch
EndFunc

 

That looks easy! :) I was using DevCon to do this kind of stuff.

I have my main PC on two networks on a 1gb and 10gb connection, I usually keep the 1gb connection disabled.

When I move I already have a 40gb switch lol just need to pick out a NIC.

Share this post


Link to post
Share on other sites

Hi guys, thanks for reply. I updated my script but I have a issue about runas capacity : I need this script to be usable by non-admin users but to launch admins commands inside.

Code

Quote

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <AutoItConstants.au3>

GUICreate("Virtualization",250,90)
$mn_software = GUICtrlCreateMenu("&Software : ")
$ssMn_VirtualBox = GUICtrlCreateMenuItem("&VirtualBox", $mn_software)
$ssMn_VMware = GUICtrlCreateMenuItem("&VMware", $mn_software)
$ssMn_Quitter = GUICtrlCreateMenuItem("&Quitter", $mn_software)
GUISetState(@SW_SHOW)
While 1
   $choice = GUIGetMsg()
   Select
      Case $choice = $ssMn_VirtualBox
         _DisableNetAdapter("VMware*", True)
         _DisableNetAdapter("VirtualBox*", False)
      Case $choice = $ssMn_VMware
         _DisableNetAdapter("VMware*", False)
         _DisableNetAdapter("VirtualBox*", True)
      Case $choice = $ssMn_Quitter
         Exit
   EndSelect
WEnd

Func _DisableNetAdapter($_sNetAdapter, $_bDisable)
   Local $sUserName = "*******"
   Local $sPassword = "******"

   Switch $_bDisable
      Case True
         Local $iPid = RunAs($sUserName, "localhost" , $sPassword, 0 , 'powershell -command "&{Disable-NetAdapter -Name '& $_sNetAdapter &' -Confirm:$false}"' , "")
      Case False
         Local $iPid = RunAs($sUserName, "localhost" , $sPassword, 0 , 'powerShell -command "&{Enable-NetAdapter -Name '& $_sNetAdapter &' -Confirm:$false}"' , "")
      EndSwitch
   Exit
EndFunc

Also, if you know a better way than my dropdown list... It's not really aesthetic in my case.

Thanks

Edited by Phoenix35

Share this post


Link to post
Share on other sites
Posted (edited)

Hi guys,

I finally found something doing the trick for the gui menu, but I'm always stuck on the runas command.

I tried another syntax with comspec and runas, but syntax seems wrong. Can you help me? Here is the code for final function :

 

Func _DisableNetAdapter($_sNetAdapter, $_bDisable)
   Local $sUserName = "*****"
   Local $sPassword = "*****"
   Switch $_bDisable
      Case True
         Local $iPid = ShellExecute(@ComSpec, "", "", "RunAs($sUserName, '"'localhost'"', $sPassword, 0, 'powershell -NoExit -command '"'&{Disable-NetAdapter -Name '& $_sNetAdapter &' -Confirm:$false}'"'', '"''"')")
      Case False
         Local $iPid = ShellExecute(@ComSpec, "", "", "RunAs($sUserName, '"'localhost'"', $sPassword, 0, 'powershell -NoExit -command '"'&{Enable-NetAdapter -Name '& $_sNetAdapter &' -Confirm:$false}'"'', '"''"')")
      EndSwitch
   Exit
EndFunc

 

Edited by Phoenix35

Share this post


Link to post
Share on other sites
Posted (edited)

Maybe you can use this :

 

 

#RequireAdmin

#Include <Array.au3>
#Include "network.au3"

$infos = _GetNetworkAdapterInfos("Local Area Network")   ; <=== Get all adapters
_ArrayDisplay($infos)

; _DisableNetAdapter("Name of you adapter")

 

Edited by jguinch

Share this post


Link to post
Share on other sites

Thanks but you don't follow the requirements I said before : I can't use #requireadmin as it will be used by non-admin users.

Thanks anyway.

Share this post


Link to post
Share on other sites

Hi, Thank you for your script. The thing is when i copy it , i have an error message saying :

"Error: ca,'t open include file "network.au3" "

 

Do you know what to do in this case ?

 

Thank you

Share this post


Link to post
Share on other sites

the good syntax should be :

Func _DisableNetAdapter($_sNetAdapter, $_bDisable)
   Local $sUserName = "*****"
   Local $sPassword = "*****"
   Switch $_bDisable
      Case True
         RunAsWait($sUserName, @ComputerName, $sPassword, 0, 'PowerShell -Command "& {Disable-NetAdapter -Name ' & $_sNetAdapter & ' -Confirm:$False}"', @SystemDir, @SW_HIDE)
      Case False
         RunAsWait($sUserName, @ComputerName, $sPassword, 0, 'PowerShell -Command "& {Enable-NetAdapter -Name ' & $_sNetAdapter & ' -Confirm:$False}"', @SystemDir, @SW_HIDE)
      EndSwitch
   Exit
EndFunc

and I think  $sUserName must be the builtin administrator account, otherwise the UAC will prohibit the modification

 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...