;==================================================================================================================== ; Name : _NicControl ; Description : Activate or deactivate a Network Interface Controler (NIC) ; Syntax : _NicControl($oLanConnection, [$iFlag, , [$iFullName]]) ; Parameter(s) : $oLanConnection - The name of the Lan connection. ; If equal to 'All' then change state of all NIC. ; $iFlag - The state of the NIC (Default 1 for activate). ; $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 @Extented is set. ; @Extented = 1 : Not Win32_NT OS ; @Extented = 2 : Network folder not find. ; @Extented = 3 : Lan connection not find. ; Author : Tlem ; 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). ; =================================================================================================================== #RequireAdmin Global $strEnableVerb Global $strDisableVerb Global $strFolderName ; =================================================================================================================== ; =================================================================================================================== ; Exemple : ; $Res = _NICContol('all', 1, 0) ; MsgBox(0, "", "$Res=" & $Res & " @Error=" & @error & " @extented=" & @extended) ; =================================================================================================================== ; =================================================================================================================== Func _NICContol($oLanConnection, $iFlag = 1, $iFullName = 1) ; Control OS type. If @OSTYPE <> "WIN32_NT" Then SetError(1, 1) Return 0 EndIf ; 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" If @OSVersion = "WIN_2000" Then $strFolderName = "Network and Dial-up Connections" ; Windows 2000 Else $strFolderName = "Network Connections"; Windows XP EndIf ; Français (France) Case StringInStr("040c,080c,0c0c,100c,140c,180c", @OSLang) $strEnableVerb = "&Activer" $strDisableVerb = "&Désactiver" If @OSVersion = "WIN_2000" Then $strFolderName = "Connexions réseau et accès à distance" ; Windows 2000 Else $strFolderName = "Connexions réseau"; Windows XP EndIf ; Add here the correct Verbs for your Operating System Language EndSelect ; Vista/W2K8 If @OSVersion <> "WIN_2000" And @OSVersion <> "WIN_XP" And @OSVersion <> "WIN_2003" Then $Res = _NICContolWin32New($oLanConnection, $iFlag, $iFullName) Else ; W2K/XP/W2K3 $Res = _NICContolWin32($oLanConnection, $iFlag, $iFullName) EndIf SetError(@error, @extended) Return $Res EndFunc ;==>_NICContol Func _NICContolWin32($oLanConnection, $iFlag = 1, $iFullName = 1) ; Inspired from : ; Original author : SvenP ; http://www.autoitscript.com/forum/index.php?showtopic=12645&view=findpost&p=87000 $ShellApp = ObjCreate("Shell.Application") $oControlPanel = $ShellApp.Namespace(3) ; Find 'Network connections' control panel item $oNetConnections = "" For $FolderItem In $oControlPanel.Items If $FolderItem.Name = $strFolderName Then $oNetConnections = $FolderItem.GetFolder ExitLoop EndIf Next ; If no 'Network connections' folder then return error. If Not IsObj($oNetConnections) Then SetError(1, 2) Return 0 EndIf ; Change all NIC state (Do not return extented errors). If StringLower($oLanConnection) = "all" Then For $FolderItem In $oNetConnections.Items _NICContolWin32($FolderItem.name, $iFlag) Next Return 1 EndIf ; Change NIC state by partial name (Do not return extented errors). If $iFullName = 0 Then For $FolderItem In $oNetConnections.Items If StringInStr(StringLower($FolderItem.name), StringLower($oLanConnection)) Then _NICContolWin32($FolderItem, $iFlag) EndIf Next Return 1 EndIf ; Find the collection of the network connection name. For $FolderItem In $oNetConnections.Items If StringLower($FolderItem.Name) = StringLower($oLanConnection) Then $oLanConnection = $FolderItem ExitLoop EndIf Next ; If no network connection name then return error. If Not IsObj($oLanConnection) Then SetError(1, 3) Return 0 EndIf $oEnableVerb = "" $oDisableVerb = "" ; Find the state of the network connection. For $Verb In $oLanConnection.Verbs If $Verb.Name = $strEnableVerb Then $oEnableVerb = $Verb $State = 0 EndIf If $Verb.Name = $strDisableVerb Then $oDisableVerb = $Verb $State = 1 EndIf Next If $iFlag = 3 Then Return $State ; Enable NIC If $iFlag Then If IsObj($oEnableVerb) Then $oEnableVerb.DoIt EndIf ; Disable NIC If Not $iFlag Then If IsObj($oDisableVerb) Then $oDisableVerb.DoIt EndIf $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 = 1 And _NICContolWin32($oLanConnection, 3) = 1 Then ExitLoop If $iFlag = 0 And _NICContolWin32($oLanConnection, 3) = 0 Then ExitLoop Sleep(100) WEnd ; Set the return value of the function. $Res = _NICContolWin32($oLanConnection, 3) If $iFlag = 1 And $Res = 0 Then Return 0 ElseIf $iFlag = 0 And $Res = 1 Then Return 0 Else Return 1 EndIf EndFunc ;==>_NICContolWin32 Func _NICContolWin32New($oLanConnection, $iFlag = 1, $iFullName = 1) ; Inspired from : ; Original author : Danny35d ; http://www.autoitscript.com/forum/index.php?showtopic=72165&view=findpost&p=528862 $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x30) If IsObj($colItems) Then $iModState = 0 ; Activate All NIC (Return just 1 if function end correctly). If StringLower($oLanConnection) = "all" Then For $objItem In $colItems If $objItem.NetConnectionID <> '' Then _NICContolWin32New($objItem.NetConnectionID, $iFlag) EndIf Next Return 1 EndIf ; Change NIC state by partial name (Return just 1 if function end correctly or if no connection was find). If $iFullName = 0 Then For $objItem In $colItems If StringInStr(StringLower($objItem.NetConnectionID), StringLower($oLanConnection)) Then $iModState = 1 $Res = _NICContolWin32New($objItem.NetConnectionID, $iFlag) EndIf Next ; If no connection find. If $iModState = 0 Then SetError(1,3) Return 0 Else Return 1 EndIf EndIf ; Change NIC state of $oLanConnection For $objItem In $colItems If $objItem.NetConnectionID = $oLanConnection Then ; Check State. If $iFlag = 3 Then If $objItem.NetEnabled = True Then Return 1 Else Return 0 EndIf Else ; Change State. If $iFlag = 0 And $objItem.NetEnabled = True Then $objItem.Disable ElseIf $iFlag = 1 And $objItem.NetEnabled = False Then $objItem.Enable EndIf $iModState = 1 EndIf ExitLoop EndIf Next ; If no connection then error. If $iModState = 0 Then SetError(1, 3) Return 0 EndIf Else SetError(1, 2) Return 0 EndIf If $iFlag = 3 Then While $iFlag <> _NICContolWin32New($oLanConnection, 3) Sleep(250) WEnd Else Sleep(1000) If $iFlag = 1 And _NICContolWin32New($oLanConnection, 3) = 1 Then Return 1 ElseIf $iFlag = 0 And _NICContolWin32New($oLanConnection, 3) = 0 Then Return 1 Else Return 0 EndIf EndIf EndFunc ;==>_NICContolWin32New