Announcement

Collapse
No announcement yet.

disable a network adapter by netconnectionID?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • disable a network adapter by netconnectionID?

    I have a server running Windows Server 2008, that has 2 network adapters, one is connected to the internet and the other is connected to the internal network.

    I would like the ability to disable the NIC that is connected to the internal network.

    I have this script to tell me what the netconnectionID is:

    Code:
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_NetworkAdapter",,48) 
    For Each objItem in colItems 
        Wscript.Echo "NetConnectionID: " & objItem.NetConnectionID
    Next

    and I have found this script to kill the NIC:

    Code:
    ' Rich Kreider <rjk @techish.net> August 2011 
    ' Tested to work on Vista/7 - Untested on Windows XP (send reports!) 
    ' Check UAC 
    ' Modified by Eric W
    If WScript.Arguments.length = 0 Then 
    Set objShell = CreateObject("Shell.Application") 
    objShell.ShellExecute "wscript.exe", """" & _ 
    WScript.ScriptFullName & """" &_ 
    " RunAsAdministrator", , "runas", 1 
    Else 
    ' Set local computer 
    strComputer = "." 
    ' Throw all network adapters that are disabled into a collection 
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
    Set colItems = objWMIService.ExecQuery( _ 
    "SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'False'") 
    ' If there are no adapters disabled then lets disable some them! 
    if colItems.count = 0 then 
        MsgBox ("Disabling Internet") 
        ' Since we know there are ZERO disabled adapters, I believe we re-run the select and look 
        ' for all ENABLED adapters so we have something to disable. 
        Set colItems = objWMIService.ExecQuery( _ 
            "SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'") 
        ' Loop through all the enabled adapters and disable them (WMI:  Win32_NetworkAdapter.Disable() Method) 
        For Each objItem in colItems 
            objItem.Disable 
        Next 
    else 
        ' I guess there were some disabled adapters found, better 
        ' enable them now! 
        MsgBox ("Enabling Internet") 
        For Each objItem in colItems 
            objItem.Enable 
        Next 
    End If 
    End If
    but my problem is how do I get the kill script to kill just the NIC that has the specific NetConnecitonID?

    thanks

    Eric
    Last edited by pbhound; 23rd November 2012, 20:13.

  • #2
    Re: disable a network adapter by netconnectionID?

    "SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'")

    might work as "select * from win32_networkadaptor where NetConnectionID = 'Value'"
    Please do show your appreciation to those who assist you by leaving Rep Point https://www.petri.com/forums/core/im.../icon_beer.gif

    Comment


    • #3
      Re: disable a network adapter by netconnectionID?

      Originally posted by tehcamel View Post
      "SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'")

      might work as "select * from win32_networkadaptor where NetConnectionID = 'Value'"

      thanks I will try that when I get home this afternoon!!

      Comment


      • #4
        Re: disable a network adapter by netconnectionID?

        ok that does not work...

        i got it to work by using this:

        to Disable:

        Code:
        strComputer = "." 
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
        ' Obtain an instance of the the class 
        ' using a key property value.
        Set objShare = objWMIService.Get("Win32_NetworkAdapter.DeviceID='7'")
        
        ' no InParameters to define
        
        ' Execute the method and obtain the return status.
        ' The OutParameters object in objOutParams
        ' is created by the provider.
        Set objOutParams = objWMIService.ExecMethod("Win32_NetworkAdapter.DeviceID='7'", "Disable")
        
        ' List OutParams
        Wscript.Echo "Out Parameters: "
        Wscript.echo "ReturnValue: " & objOutParams.ReturnValue
        to enable:

        Code:
        strComputer = "." 
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
        ' Obtain an instance of the the class 
        ' using a key property value.
        Set objShare = objWMIService.Get("Win32_NetworkAdapter.DeviceID='7'")
        
        ' no InParameters to define
        
        ' Execute the method and obtain the return status.
        ' The OutParameters object in objOutParams
        ' is created by the provider.
        Set objOutParams = objWMIService.ExecMethod("Win32_NetworkAdapter.DeviceID='7'", "Enable")
        
        ' List OutParams
        Wscript.Echo "Out Parameters: "
        Wscript.echo "ReturnValue: " & objOutParams.ReturnValue
        but when i try to use the NetConnectionID it will give an error.

        Click image for larger version

Name:	NetConnectionID-Error.JPG
Views:	1
Size:	20.4 KB
ID:	466626

        Comment


        • #5
          Re: disable a network adapter by netconnectionID?

          Originally posted by tehcamel View Post
          Originally posted by pbhound View Post
          [...]
          "SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'")
          [...]
          might work as "select * from win32_networkadaptor where NetConnectionID = 'Value'"
          Should work except for the fact that there is a typo!

          "select * from Win32_NetworkAdapter where NetConnectionID = 'Value'"

          as in
          Code:
          ' Set local computer
          strComputer = "."
          
          Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
          
          Set colItems = objWMIService.ExecQuery( _ 
             "SELECT * FROM Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")
             
             ' toggle adapter status
             For Each objItem in colItems
          
               If objItem.NetEnabled = False Then
                 wscript.echo "enabling " & objItem.NetConnectionID
          
               ElseIf objItem.NetEnabled = True Then
                 wscript.echo "disabling " & objItem.NetConnectionID
          
               End If
             Next
          /Rems
          Last edited by Rems; 25th November 2012, 23:33.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: disable a network adapter by netconnectionID?

            Originally posted by Rems View Post
            Should work except for the fact that there is a typo!

            "select * from Win32_NetworkAdapter where NetConnectionID = 'Value'"

            as in
            Code:
            ' Set local computer
            strComputer = "."
            
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
            
            Set colItems = objWMIService.ExecQuery( _ 
               "SELECT * FROM Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")
               
               ' toggle adapter status
               For Each objItem in colItems
            
                 If objItem.NetEnabled = False Then
                   wscript.echo "enabling " & objItem.NetConnectionID
            
                 ElseIf objItem.NetEnabled = True Then
                   wscript.echo "disabling " & objItem.NetConnectionID
            
                 End If
               Next
            /Rems

            LMAO thank you so much I feel so dumb that i missed that. (not really; but i still cant believe i missed that.)

            Comment


            • #7
              Re: disable a network adapter by netconnectionID?

              ok tried that and now I receive the error" Invalid Class
              Attached Files

              Comment


              • #8
                Re: disable a network adapter by netconnectionID?

                You did not customized the sample script?, and the target computer is still the local server?

                There is a problem with WMI on the sever, mostlikely that some WMI classes are not correctly registered.

                Try this command;
                wmiadap /f

                /Rems

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment

                Working...
                X