Announcement

Collapse
No announcement yet.

Disable network adapter via script

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

  • Disable network adapter via script

    Hi All,

    I'm looking for a script that disables all network adapters on a remote computer (the computer name is given as a parameter %1)

    I was trying out this script which for now is set for the local machine - but is doesn't disable all the enabled cards (probably because of this line " objItem.Disable "


    here is the script

    Code:
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'True'")
    
    For Each objItem in colItems 
        Wscript.Echo "Name: " & objItem.Name
        Wscript.Echo "Description: " & objItem.Description
        objItem.Disable
        Wscript.Echo
    Next

    Thanks!!

    Edit Dumber: Wrapped code tags around the script.
    Last edited by Dumber; 5th June 2008, 12:43.

  • #2
    Re: Disable network adapter via script

    On what Operating Systems do you run this script?

    The NetEnabled property is not available for Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0

    Also,
    Disable / Enable Method of the Win32_NetworkAdapter Class Requirements
    Client Requires: Windows Vista.
    Server Requires: Windows Server 2008.
    MOF Declared in: Cimwin32.mof.
    DLL Requires: Cimwin32.dll.
    Namespace Defined in: \root\cimv2.



    \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


    • #3
      Re: Disable network adapter via script

      Instead, you can use Devcon.exe to disable a certain device or devices.

      example
      Code:
      ' http://www.experts-exchange.com/OS/Miscellaneous/Q_21487369.html
      ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
      ' Disable Wireless Network Cards ( => MediaSubType=2 )
      ' note,
      ' You can not run this script as user Logon-script - unless 
      ' the current user is a member of the local group Administrators.
      ' Therefore it is better you run this script as computer Startup-script. 
      '
      ' you can store Devcon.exe on a networkshare, 
      ' in that case, replace the line with:
      '          oWShell.Run """\\server\share\devcon.exe"" disable ""@" & strValue & """" ,2,true
      ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
      
      Const HKEY_LOCAL_MACHINE = &H80000002
      
      Public oWShell, oReg
      
      strComputer = "."
      
      Set oWShell = Wscript.CreateObject("Wscript.Shell") 
      Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
        strComputer & "\root\default:StdRegProv")
      
      strKeyPath = _
        "SYSTEM\CurrentControlSet\Control\Network"
      
      EnumeratingSubkeys HKEY_LOCAL_MACHINE, strKeyPath
      
      Wscript.quit
      
      Sub EnumeratingSubkeys(HKEY_LOCAL_MACHINE, strKeyPath) 
          oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys 
      
          If IsArray(arrSubkeys) Then 
              For Each strSubkey In arrSubkeys 
                If Instr(subkey, "{4D36E972") = 0 Then
                  EnumeratingSubkeys HKEY_LOCAL_MACHINE,_
                    strKeyPath & "\" & strSubkey
                End If
                If subkey <> "Descriptions" Then
                  strValueName = "MediaSubType"
                  oReg.GetDWORDValue HKEY_LOCAL_MACHINE,_
                    strKeyPath & "\" & subkey & "\" & "Connection" ,_
                    strValueName,dwvalue
      
                  If dwvalue = 2 Then
                    strValueName = "PnpInstanceID"
                    oReg.GetStringValue HKEY_LOCAL_MACHINE,_
                      strKeyPath & "\" & subkey & "\" & "Connection" ,_
                      strValueName,strValue
                    oWShell.Run "devcon.exe disable ""@" & strValue & """" ,2,true
                  End If
                End If
              Next
          End If
      End Sub
      \Rems



      EDIT

      note!
      If you add the name of a remote computer for "strComputer", the script will EnumeratingSubkeys on the remote computer...
      BUT - devcon.exe however will be executed by this script on your local computer!!!!!!!!! and not on the remote computer.

      .
      Last edited by Rems; 5th June 2008, 23:14.

      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


      • #4
        Re: Disable network adapter via script

        Originally posted by Rems View Post
        On what Operating Systems do you run this script?

        The NetEnabled property is not available for Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0

        Also,
        Disable / Enable Method of the Win32_NetworkAdapter Class Requirements
        Client Requires: Windows Vista.
        Server Requires: Windows Server 2008.
        MOF Declared in: Cimwin32.mof.
        DLL Requires: Cimwin32.dll.
        Namespace Defined in: \root\cimv2.



        \Rems

        Well I am working for a solution based on windows 2008 servers and Vista clients, the script will be running on the vista client via the

        trying to get the server to inject the script to the client (vista) scheduler

        I've took a look at the DevCon solution but seems that it is not redistributable. It is provided for use as a debugging and development tool only.

        BTW - Stupid me the script is working I was just trying to run it on my a machine with GPO that I didn't had fully admin rights on it.... (stupid is as stupid does)
        tested it on another machine it works great...


        now I need to figure out how to make my script to use a parameter to run it on a remote machine...
        Last edited by XtaZee; 8th June 2008, 08:20.

        Comment


        • #5
          Re: Disable network adapter via script

          Script arguments (named and unnamed):
          - http://www.microsoft.com/technet/scr....mspx?mfr=true
          - http://cis.stvincent.edu/html/tutorials/wsh/args.html


          sample non-named argument:
          Code:
          If WScript.Arguments.Count <> 0 Then
             strComputer = WScript.Arguments.Item(0) 
             Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" _
                & strComputer & "\root\cimv2")
          Else
             wscript.echo "Missing Parameter!"
             wscript.Quit
          End If
          \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


          • #6
            Re: Disable network adapter via script

            Originally posted by Rems View Post
            Script arguments (named and unnamed):
            - http://www.microsoft.com/technet/scr....mspx?mfr=true
            - http://cis.stvincent.edu/html/tutorials/wsh/args.html


            sample non-named argument:
            Code:
            If WScript.Arguments.Count <> 0 Then
               strComputer = WScript.Arguments.Item(0) 
               Set objWMIService = GetObject("winmgmts:" _
                  & "{impersonationLevel=impersonate}!\\" _
                  & strComputer & "\root\cimv2")
            Else
               wscript.echo "Missing Parameter!"
               wscript.Quit
            End If
            \Rems
            Just a little update, here is the script in PowerShell using WMI - it disable all 802.3 nics and after 30 sec enable them again...

            Code:
            # Win32_NetworkAdapter. Disable / Enable Script"  
            # Created by Daniel Klepner 
            
            $filter = "AdapterType = 'Ethernet 802.3' and NetEnabled = 'True'"
            $class = "Win32_NetworkAdapter"
            $namespace = "root\cimv2"
            $strComputer = "."
            
            
            # Find all Active ethernet 802.3 network cards
            $colItems = get-wmiobject -class $class -namespace $namespace -computername $strComputer –filter $filter 
            
            
            
            # Disable  active network cards
            foreach ($objItem in $colItems) {
                  write-host $objItem.Description
            	   $objItem.Disable()
            }
            
            
            Start-sleep 30
            
            # Enable active network cards
            foreach ($objItem in $colItems) {
                  write-host $objItem.Description
            	   $objItem.Enable()
            }
            now I am working on running it from remote...

            Moderator edit:
            thread continues in PowerShell forum: http://forums.petri.com/showthread.php?t=46652
            -
            Last edited by Rems; 6th June 2010, 16:06.

            Comment

            Working...
            X