Phoenix35 Posted February 28, 2020 Posted February 28, 2020 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"
Phoenix35 Posted February 28, 2020 Author Posted February 28, 2020 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
Subz Posted February 28, 2020 Posted February 28, 2020 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
Nine Posted February 28, 2020 Posted February 28, 2020 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
ViciousXUSMC Posted February 28, 2020 Posted February 28, 2020 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.
Phoenix35 Posted February 29, 2020 Author Posted February 29, 2020 (edited) 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 February 29, 2020 by Phoenix35
Phoenix35 Posted March 2, 2020 Author Posted March 2, 2020 (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 March 2, 2020 by Phoenix35
jguinch Posted March 2, 2020 Posted March 2, 2020 (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 March 2, 2020 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Phoenix35 Posted March 2, 2020 Author Posted March 2, 2020 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.
Yossep_237 Posted March 3, 2020 Posted March 3, 2020 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
Developers Jos Posted March 3, 2020 Developers Posted March 3, 2020 Just download the linked include file and store it with your script. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
jguinch Posted March 3, 2020 Posted March 3, 2020 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 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now