Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Disable/Enable Local Area Connection

$
0
0
I would like to Disable/Enable a Local Area Connection.

I found this script that isn't working in Win_7  SP1 64bit.

I'm getting an error on line 108 saying Variable used without being declared.

Is there an #include that I am supposed to have?  This was originally created in 2008.  Maybe AutoIt changed?

[ autoit ]         
AutoItSetOption("TrayIconDebug", 1) ;0-off Local $Res ; Activation of all network cards. ; _NetCC (" All ") ; Activation of all network cards the name of which contains "place ". ; _NetCC ("place ", 1, 0) ; Deactivation of connection 1394. _NetCC (" local ", 0) ; Deactivation of a nonexistent connection and return of error. ; $Res _NetCC ("Connection XYZ", 0) MsgBox (0, "Information ", "$Res " & $Res & " @Error " & @error & " @Extented " & @extended) #cs =================================================================================================================== Network Connection Control (NetCC.au3) By Tlem Aviable Functions : _NetCC() = Change state or get state of network connections _NetCCGetList() = Get list of network connections. Some parts of this code is inspired from other codes. So tanks to : SvenP, Danny35d, smashly. - http://www.autoitscript.com/forum/index.php?showtopic=12645&view=findpost&p=87000 - http://www.autoitscript.com/forum/index.php?showtopic=72165&view=findpost&p=528862 - http://www.autoitscript.com/forum/index.php?showtopic=74074&view=findpost&p=538835 #ce =================================================================================================================== #include-once #RequireAdmin Global Const $NETWORK_FOLDER = 0x31 Global $strEnableVerb, $strDisableVerb, $strRepairVerb, $strStateVerb, $strFolderName ;==================================================================================================================== ; Name           : _NetCC ; Description    : Activate or deactivate a Network Interface Controler (NIC) ; Syntax         : _NetCC($oLanConnection, [$iFlag, , [$iFullName]]) ; Parameter(s)   : $oLanConnection - The name of the Lan connection. ; If equal to 'All' then change state of all NIC. ;                    $iFlag - 0 = Disable connection. ; 1 = Enable connection (default) ; 2 = Toggle connection ; 3 = Repair connection ; 4 = Check State of a connection. ;                    $iFullName - Look for string $oLanConnection in NIC name (Default 1 for full name). ; Requirement(s) : Win32_NT OS ; Return value(s) : On Success - Return 1 ;                    On Failure - Return 0 and @error 1~5. ; @error 1 = Not Win32_NT OS ; @error 2 = Network folder not find. ; @error 3 = Lan connection not find. ; @error 4 = Shell Application object creation error. ; @error 5 = Verb or function not available. ; Author         : Tlem <tlem at tuxolem dot net> ; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092 ; Note(s)        : ; ; To Do : Get individual errors for changing state of multiple NIC (Perhaps with array). ; =================================================================================================================== Func _NetCC($oLanConnection, $iFlag = 1, $iFullName = 1) ; Control OS type. If @OSTYPE <> "WIN32_NT" Then Return SetError(1, 0, 0) EndIf ; W2K/XP/W2K3 If StringInStr("WIN_2000,WIN_XP,WIN_2003,WIN_7", @OSVersion) Then ; Change all NIC state. If StringLower($oLanConnection) = "all" Then $Res = _NetCCAll($iFlag, 0) ; Change NIC state by partial name. ElseIf $iFullName = 0 Then $Res = _NetCCPartial($oLanConnection, $iFlag, 0) ; Change NIC state for $oLanConnection. Else $Res = _NetCCWin32($oLanConnection, $iFlag, $iFullName) EndIf Else ; Vista/W2K8 ; Option repair not exits in Vista/2008 If $iFlag = 3 Then Return SetError(5, 0, 0) ; Change all NIC state. If StringLower($oLanConnection) = "all" Then $Res = _NetCCAll($iFlag, 1) ; Change NIC state by partial name. ElseIf $iFullName = 0 Then $Res = _NetCCPartial($oLanConnection, $iFlag, 1) ; Change NIC state for $oLanConnection. Else $Res = _NetCCWin32New($oLanConnection, $iFlag, $iFullName) EndIf EndIf Return SetError(@error, @extended, $Res) EndFunc ;==>_NetCC ; Only for W2K/XP/W2K3 Func _NetCCWin32($oLanConnection, $iFlag = 1, $iFullName = 1) Local $objShell, $objFolder $objShell = ObjCreate("Shell.Application") If Not IsObj($objShell) Then Return SetError(2, 0, 0) $objFolder = $objShell.NameSpace($NETWORK_FOLDER) If Not IsObj($objFolder) Then Return SetError(3, 0, 0) ; Find the collection of the network connection name. For $LanItem In $objFolder.Items If StringLower($LanItem.Name) = StringLower($oLanConnection) Then $oLanConnection = $LanItem ExitLoop EndIf Next ; If no network connection name then return error. If Not IsObj($oLanConnection) Then Return SetError(3, 0, 0) $oEnableVerb = "" $oDisableVerb = "" ; Find the state of the network connection. For $Verb In $oLanConnection.Verbs If $Verb.Name = $strEnableVerb Then $oEnableVerb = $Verb If Not IsObj($oEnableVerb) Then Return SetError(5, 0, 0) $State = 0 EndIf If $Verb.Name = $strDisableVerb Then $oDisableVerb = $Verb If Not IsObj($oDisableVerb) Then Return SetError(5, 0, 0) $State = 1 EndIf If $Verb.Name = $strRepairVerb Then $oRepairVerb = $Verb If Not IsObj($oRepairVerb) Then Return SetError(5, 0, 0) EndIf Next ; Return State If $iFlag = 4 Then Return $State ; Repair Net Connection ElseIf $iFlag = 3 And $State = 1 Then $oRepairVerb.DoIt ; Toggle Net Connection ElseIf $iFlag = 2 Then If $State = 1 Then $oDisableVerb.DoIt Else $oEnableVerb.DoIt EndIf ; Enable Net Connection ElseIf $iFlag = 1 And $State = 0 Then $oEnableVerb.DoIt ; Disable Net Connection ElseIf $iFlag = 0 And $State = 1 Then $oDisableVerb.DoIt ; Else there is an error Else Return SetError(5, 0, 0) EndIf ; Loop to wait change state (for changing state of more than one connection). $begin = TimerInit() While 1 $dif = Int(TimerDiff($begin) / 1000) If $dif > 10 Then ExitLoop ; Control the state of the NIC to exit before the end of waiting time. If $iFlag = 3 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop If $iFlag = 2 And $State = 1 And _NetCCWin32($oLanConnection, 4) = 0 Then ExitLoop If $iFlag = 2 And $State = 0 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop If $iFlag = 1 And _NetCCWin32($oLanConnection, 4) = 1 Then ExitLoop If $iFlag = 0 And _NetCCWin32($oLanConnection, 4) = 0 Then ExitLoop Sleep(100) WEnd ; Set the return value of the function. $Res = _NetCCWin32($oLanConnection, 4) If $iFlag = 4 And $Res = 1 Then Sleep(3000) Return 1 ElseIf $iFlag = 1 And $Res = 0 Then Return 0 ElseIf $iFlag = 0 And $Res = 1 Then Return 0 Else Return 1 EndIf EndFunc ;==>_NetCCWin32 ; Only for Vista/W2K8 Func _NetCCWin32New($oLanConnection, $iFlag = 1, $iFullName = 1) Local $objWMIService, $colItems, $iModState = 0 $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30) If Not IsObj($colItems) Then Return SetError(4, 0, 0) ; Change NIC state of $oLanConnection ; Check name of the connection For $objItem In $colItems If $objItem.NetConnectionID = $oLanConnection Then ; Check State. If $iFlag = 4 Then If $objItem.NetEnabled = True Then Return 1 Else Return 0 EndIf Else ; Change the State of Net Connection. ; Disable Net Connection If $iFlag = 0 And $objItem.NetEnabled = True Then $objItem.Disable ; Enable Net Connection ElseIf $iFlag = 1 And $objItem.NetEnabled = False Then $objItem.Enable ; Toggle Net Connection ElseIf $iFlag = 2 Then If $objItem.NetEnabled = True Then $objItem.Disable $State = 1 Else $objItem.Enable $State = 0 EndIf EndIf ; One change be done. $iModState = 1 EndIf ExitLoop EndIf Next ; If no connection then error. If $iModState = 0 Then Return SetError(3, 0, 0) ; Loop to wait state change. If $iFlag = 4 Then While $iFlag <> _NetCCWin32New($oLanConnection, 4) Sleep(250) WEnd Else Sleep(1000) If $iFlag = 2 Then If $State = 0 And _NetCCWin32New($oLanConnection, 4) = 1 Then Return 1 ElseIf $State = 1 And _NetCCWin32New($oLanConnection, 4) = 0 Then Return 1 Else Return 0 EndIf ElseIf $iFlag = 1 And _NetCCWin32New($oLanConnection, 4) = 1 Then Return 1 ElseIf $iFlag = 0 And _NetCCWin32New($oLanConnection, 4) = 0 Then Return 1 Else Return 0 EndIf EndIf EndFunc ;==>_NetCCWin32New ;==================================================================================================================== ; Name           : _NetCCGetList() ; Description    : Get list of network connections. ; Syntax         : _NetCCGetList() ; Parameter(s)   : NA. ; Requirement(s) : Win32_NT OS ; Return value(s) : Array of network connections. ;                    On Failure - Return 0 and @error 3. ; @error 3 = Lan connection not find. ; Author         : Tlem <tlem at tuxolem dot net> ; http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=1092 ; Note(s)        : ; ; =================================================================================================================== Func _NetCCGetList() ; W2K/XP/W2K3 If StringInStr("WIN_2000,WIN_XP,WIN_2003", @OSVersion) Then Local $objShell, $objFolder, $sTmp = '' _NetCCLang() $objShell = ObjCreate("Shell.Application") If Not IsObj($objShell) Then Return SetError(4, 0, 0) $objFolder = $objShell.NameSpace($NETWORK_FOLDER) If Not IsObj($objFolder) Then Return SetError(3, 0, 0) For $objItem In $objFolder.Items For $oVerb In $objItem.Verbs If StringInStr($oVerb.Name, $strStateVerb) Then $sTmp &= $objItem.Name & "|" Next Next Else ; Vista/W2K8 Local $objWMIService, $colItems, $sTmp = '' $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30) If Not IsObj($colItems) Then Return SetError(4, 0, 0) For $objItem In $colItems If $objItem.NetConnectionID Then $sTmp &= $objItem.NetConnectionID & "|" EndIf Next EndIf If $sTmp = '' Then Return SetError(3, 0, 0) $aNetList = StringSplit(StringTrimRight($sTmp, 1), "|") Return $aNetList EndFunc ;==>_NetCCGetList Func _NetCCAll($iFlag, $Func = 0) $iModState = 0 $aNetCon = _NetCCGetList() For $i = 1 To $aNetCon[0] If $Func = 1 Then _NetCCWin32New($aNetCon[$i], $iFlag) Else _NetCCWin32($aNetCon[$i], $iFlag) EndIf $iModState = 1 Next ; If no connection find. If $iModState = 0 Then Return SetError(3, 0, 0) Else Return 1 EndIf EndFunc ;==>_NetCCAll Func _NetCCPartial($oLanConnection, $iFlag, $Func) $iModState = 0 $aNetCon = _NetCCGetList() For $i = 1 To $aNetCon[0] If StringInStr(StringLower($aNetCon[$i]), StringLower($oLanConnection)) Then If $Func = 1 Then _NetCCWin32New($aNetCon[$i], $iFlag) Else _NetCCWin32($aNetCon[$i], $iFlag) EndIf $iModState = 1 EndIf Next ; If no connection find. If $iModState = 0 Then Return SetError(3, 0, 0) Else Return 1 EndIf EndFunc ;==>_NetCCPartial Func _NetCCLang() ; Langage selection. Select ; English (United States) Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", @OSLang) $strEnableVerb = "En&able" $strDisableVerb = "Disa&ble" $strRepairVerb = "Re&pair" If @OSVersion = "WIN_2000" Then $strStateVerb = "St&ate" ; Windows 2000 (Not sur if correct) Else $strStateVerb = "Stat&us"; Windows XP EndIf ; Français (France) Case StringInStr("040c,080c,0c0c,100c,140c,180c", @OSLang) $strEnableVerb = "&Activer" $strDisableVerb = "&Désactiver" $strRepairVerb = "&Réparer" If @OSVersion = "WIN_2000" Then $strStateVerb = "É&tat" ; Windows 2000 Else $strStateVerb = "Sta&tut"; Windows XP EndIf ; Add here the correct Verbs for your Operating System Language EndSelect EndFunc ;==>_NetCCLang

Thanks for looking at it,

Docfxit

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>