切换网络连接的脚本

本文介绍了一个用于Windows系统的WSF脚本,该脚本能够通过命令行方式启用或禁用指定的网络适配器。适用于高级用户使用,已测试于Windows XP系统。

 <?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: Joey Zhang
Last Modify: Jan 1st, 2009
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 = "启用(&A)"
    strDisableVerb = "禁用(&B)"
    '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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值