切换网络连接的脚本

来源:互联网 发布:健身软件有哪些 编辑:程序博客网 时间:2024/06/02 07:30

<?xml version="1.0" encoding="gb2312" ?>
<?job error="true" debug="false"?>

<package id="SetNICStatus">

  <job id="Main">
    <runtime>
        <description>
 FileName: SetNICStatus.wsf
 This Script switch Network Adapter's status. For Advanced users only.

 Version: 1.0
 Created:
icuc88@hotmail.com
 Last Modify: Feb 14th, 2004
 All right reserved.

 **Note: Default settings for Windows XP. And I Just only tested on it.
 **You must change the variable strFolderName before running on other OS.
        </description>
        <named
 name="CN"
 helpstring="Connection Name"
 type="string"
 required="true"
        />
        <named
             name="Status"
             helpstring="0:Disable Network Adapter; 1:Enable Network Adapter"
 type="string"
             required="true"
        />
        <example>
        Example:
          To disable "Local Area Connection"
          SetNICStatus.wsf /CN:"Local Area Connection" /Status:0

          To enable "Local Area Connection"
          SetNICStatus.wsf /CN:"Local Area Connection" /Status:1
        </example>
    </runtime>

    <script language="VBScript">
    <![CDATA[
   
      Option Explicit
      On Error Resume Next

      If WScript.Arguments.Count < 2 Then
         WScript.Arguments.ShowUsage
         WScript.Quit
      Else
         ' Get system language
         Dim strComputer
         strComputer = "." ' Local Computer

         Dim objWMIService
         Dim colOperatingSystem

         Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!//"_
  & strComputer & "/root/cimv2")
         Set colOperatingSystem = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

         Dim objOperatingSystem
         Dim OSLanguage
         ' Dim OSType
         For Each objOperatingSystem in colOperatingSystem
            OSLanguage = objOperatingSystem.OSLanguage
            ' OSType = objOperatingSystem.OSType
         Next

         Dim strEnableVerb
         Dim strDisableVerb

         ' The folder's name of the networks settings in Control Panel
         ' You should change it for difference operating system
         Dim strFolderName

         Select Case OSLanguage
 Case 2052 ' Chinese(PRC)
   strEnableVerb = "启用"
   strDisableVerb = "停用"
   ' strFolderName = "网络和拨号连接"  ' Windows 2000
               strFolderName = "网络连接" ' Windows XP
             Case 1033 ' English (United States)
   strEnableVerb = "En&able"
   strDisableVerb = "Disa&ble"
               ' strFolderName = "Network and Dial-up Connections"  ' Windows 2000
   strFolderName = "Network Connections" ' Windows XP
         End Select

         'Virtual folder containing icons for the Control Panel applications. (value = 3)
         Const ssfCONTROLS = 3 

         Dim shellApp
         Dim oControlPanel
         Set shellApp = CreateObject("Shell.Application")
         Set oControlPanel = shellApp.Namespace(ssfCONTROLS)

         Dim oNetConnections
         Dim FolderItem
         Set oNetConnections = nothing
         For Each FolderItem in oControlPanel.Items
           If FolderItem.Name  = strFolderName then
             Set oNetConnections = FolderItem.GetFolder: Exit For
           End If
         Next
      
         If oNetConnections Is nothing Then
 MsgBox "Could't find " & strFolderName & " folder"
 WScript.Quit
         End If
        
         Dim oLanConnection
         Set oLanConnection = nothing
         For Each FolderItem In oNetConnections.Items
 If LCase(FolderItem.Name) = LCase(WScript.Arguments.Named.Item("CN")) Then
   Set oLanConnection = FolderItem: Exit For
 End If
         Next

         If oLanConnection Is nothing Then
 MsgBox "Couldn't find " & WScript.Arguments.Named.Item("CN") & " Item"
 WScript.Quit
         End If

         Dim bEnabled
         Select Case WScript.Arguments.Named.Item("Status")
            Case "0" bEnabled = false
            Case "1" bEnabled = true
         End Select
  
         Dim oEnableVerb
         Dim oDisableVerb
         Dim Verb
         For Each Verb In oLanConnection.Verbs
 If Verb.Name = strEnableVerb Then
   Set oEnableVerb = Verb
 End If
 If Verb.Name = strDisableVerb Then
   Set oDisableVerb = Verb
 End If
         Next

         If bEnabled Then
 oEnableVerb.DoIt
         Else
 oDisableVerb.DoIt
         End If

         WScript.Sleep 1000
      End If

    ]]>
    </script>
  </job>
</package>

把上面的之间的内容粘贴复制到记事本,然后另存为SetNICStatus.wsf,双击后可以看见帮助。

脚本是在Windows XP上面测试的,如果在Windows 2000上面,需要做一定的修改,
修改里面的strFolderName这个变量的名字,如果要用到NT或者Windows 9x上面去,要求更高,需要安装WMI。