Jump to content

Disable a Network Device with WMI


Recommended Posts

Hello,

Here is a code that tell the user when a network adapter has changed its status.

Now, I want to find a way to disable a network card. I do not want to use tool like devcon.exe. It must be done by using WMI.

How can I do it?

Thanks!

$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$colEvents = $objWMIService.ExecNotificationQuery _
("Select * From __InstanceOperationEvent Within 5 Where " _
& "TargetInstance isa 'Win32_NetworkAdapter'")
While 1
    $objEvent = $colEvents.NextEvent
    if $objEvent.Path_.Class()="__InstanceModificationEvent" Then 
    ; on teste si le statut de la carte réseau a changé
        If $objEvent.TargetInstance.NetConnectionStatus <> $objEvent.PreviousInstance.NetConnectionStatus Then
            Select
                Case $objEvent.TargetInstance.NetConnectionStatus = 2
                    MsgBox(0, "Enable", "Interface was enabled : " & $objEvent.TargetInstance.Name)
                Case $objEvent.TargetInstance.NetConnectionStatus = 0
                    MsgBox(0, "Disable", "Interface was disabled : " & $objEvent.TargetInstance.Name)
                EndSelect
            EndIf
    EndIf
WEnd
Link to comment
Share on other sites

Hi,

disable or just cut the connection?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

@Tentaal

A question we hear frequently is "How do I enable and disable my network adapters through a script?" Well, we won't hold you in suspense. We'll tell you right now that you can't. At least not until you're running Windows Vista. In Windows Vista two new methods have been added to the Win32_NetworkAdapter class: Enable and Disable.

Sorry :)

Regards,

ptrex

Link to comment
Share on other sites

The NicToggle() UDF was made by Wus

#include <GUIConstants.au3>

Dim $i, $base, $key, $name

; == GUI generated with Koda ==
$Form1 = GUICreate("Nic", 176, 89, -1, -1, -1, $WS_EX_TOOLWINDOW)
GUICtrlCreateLabel("Pick Network Connection", 8, 8, 157, 17, $SS_CENTER)
$Combo1 = GUICtrlCreateCombo("", 8, 24, 161, 21)
$Button1 = GUICtrlCreateButton("Enable", 8, 56, 75, 25)
$Button2 = GUICtrlCreateButton("Disable", 96, 56, 75, 25)

;Get Network Connection Names
$base = "HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
While 1
    $i += 1
    $key = RegEnumKey($base, $i)
    If @error <> 0 Then ExitLoop
    $name = RegRead($base & "\" & $key & "\Connection", "Name")
    If StringLeft($key, 1) = "{" Then GUICtrlSetData($Combo1, $name & "|")
WEnd

GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    Case $Button1
        If GUICtrlRead($Combo1) <> '' Then NicToggle(1, GUICtrlRead($Combo1))
    Case $Button2
        If GUICtrlRead($Combo1) <> '' Then NicToggle(0, GUICtrlRead($Combo1))
    Case Else
        ;;;;;;;
    EndSwitch
WEnd
Exit

Func NicToggle($Toggle, $sConnectionName = "Local Area Connection", $sNetworkFolder = "Network Connections")    
    $ssfCONTROLS = 3
    $sEnableVerb = "En&able"
    $sDisableVerb = "Disa&ble"
    $shellApp = ObjCreate("shell.application")
    $oControlPanel = $shellApp.Namespace ($ssfCONTROLS)
    $oNetConnections = "nothing"
    
    For $folderitem In $oControlPanel.items
        If $folderitem.name = $sNetworkFolder Then
            $oNetConnections = $folderitem.getfolder
            ExitLoop
        EndIf
    Next
    If $oNetConnections = "nothing" Then
        MsgBox(48, "Error", "Couldn't find " & $sNetworkFolder & " folder")
        Exit
    EndIf
    
    $oLanConnection = "nothing"
    For $folderitem In $oNetConnections.items
        If StringLower($folderitem.name) = StringLower($sConnectionName) Then
            $oLanConnection = $folderitem
            ExitLoop
        EndIf
    Next
    If $oLanConnection = "nothing" Then
        MsgBox(48, "Error", "Couldn't find '" & $sConnectionName & "' item")
        Exit
    EndIf
    
    $bEnabled = True
    $oEnableVerb = "nothing"
    $oDisableVerb = "nothing"
    $s = "Verbs: " & @CRLF
    For $verb In $oLanConnection.verbs
        $s = $s & @CRLF & $verb.name
        
    ;enables
        If $verb.name = $sEnableVerb And $Toggle = 1 Then
            $oEnableVerb = $verb
            $oEnableVerb.DoIt
            ExitLoop
            
        ;disables
        ElseIf $verb.name = $sDisableVerb And $Toggle = 0 Then
            $oDisableVerb = $verb
            $oDisableVerb.DoIt
            ExitLoop
        Else
            MsgBox(48, "Error", "Tried to disable when already disabled" & @CRLF & "or enable when already enabled")
            Exit
        EndIf
    Next
    Sleep(1000)
EndFunc  ;==>NicToggle
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
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...