本地连接禁用/启用脚本

转载至 http://community.csdn.net/Expert/topic/4881/4881882.xml

Const  ssfCONTROLS  =   3

sConnectionName 
=   " 本地连接 "    ' 可改成需要控制的连接名称,如"无线网络连接"等

sEnableVerb 
=   " 启用(&A) "
sDisableVerb 
=   " 禁用(&B) "     ' XP系统中应为 "停用(&B)"

set  shellApp  =   createobject ( " shell.application " )
set  oControlPanel  =  shellApp.Namespace(ssfCONTROLS)

set  oNetConnections  =   nothing
for   each  folderitem in oControlPanel.items
 
if  folderitem.name   =   " 网络连接 "   then
   
set  oNetConnections  =  folderitem.getfolder:  exit   for
 
end   if
next

if  oNetConnections  is   nothing   then
 
msgbox   " 未找到网络连接文件夹 "
 wscript.quit
end   if

set  oLanConnection  =   nothing
for   each  folderitem in oNetConnections.items
 
if   lcase (folderitem.name)   =   lcase (sConnectionName)  then
   
set  oLanConnection  =  folderitem:  exit   for
 
end   if
next

if  oLanConnection  is   nothing   then
 
msgbox   " 未找到 ' "   &  sConnectionName  &   " ' item "
 wscript.quit
end   if

bEnabled 
=   true
set  oEnableVerb  =   nothing
set  oDisableVerb  =   nothing
=   " Verbs:  "   &  vbcrlf
for   each  verb in oLanConnection.verbs
 s 
=  s  &  vbcrlf  &  verb.name
 
if  verb.name  =  sEnableVerb  then  
   
set  oEnableVerb  =  verb  
   bEnabled 
=   false
 
end   if
 
if  verb.name  =  sDisableVerb  then  
   
set  oDisableVerb  =  verb  
 
end   if
next

' debugging displays left just in case...
'
'
msgbox s ': wscript.quit
'
msgbox "Enabled: " & bEnabled ': wscript.quit

' not sure why, but invokeverb always seemed to work 
'
for enable but not disable.  
'
'
saving a reference to the appropriate verb object 
'
and calling the DoIt method always seems to work.
'
if  bEnabled  then
'   oLanConnection.invokeverb sDisableVerb
 oDisableVerb.DoIt
else
'   oLanConnection.invokeverb sEnableVerb
 oEnableVerb.DoIt
end   if

' adjust the sleep duration below as needed...
'
'
if you let the oLanConnection go out of scope
'
and be destroyed too soon, the action of the verb
'
may not take...
'
wscript.sleep  400
### 回答1: 可以使用以下批处理脚本禁用/启用以太网:@echo offrem 以太网启用/禁用if "%1"=="-enable" ( netsh interface set interface "以太网" enabled ) else if "%1"=="-disable" ( netsh interface set interface "以太网" disabled )echo 以太网已%1 ### 回答2: 批处理脚本是一种用于自动化任务的脚本语言,可以在Windows操作系统中执行。以下是一个用于一键禁用/启用以太网的批处理脚本示例: @echo off setlocal enabledelayedexpansion REM 获取当前以太网状态 for /f "tokens=2 delims=:" %%a in ('netsh interface show interface^|findstr /c:"以太网"') do ( set status=%%a ) REM 判断当前状态并进行相应操作 if "%status%"==" 已连接 " ( REM 禁用以太网 echo 禁用以太网... netsh interface set interface "以太网" admin=disable REM 延迟2秒等待操作生效 ping localhost -n 2 >nul REM 检查禁用状态 for /f "tokens=2 delims=:" %%a in ('netsh interface show interface^|findstr /c:"以太网"') do ( set new_status=%%a ) if "%new_status%"==" 已禁用 " ( echo 以太网已禁用。 ) else ( echo 无法禁用以太网。 ) ) else ( REM 启用以太网 echo 启用以太网... netsh interface set interface "以太网" admin=enable REM 延迟2秒等待操作生效 ping localhost -n 2 >nul REM 检查启用状态 for /f "tokens=2 delims=:" %%a in ('netsh interface show interface^|findstr /c:"以太网"') do ( set new_status=%%a ) if "%new_status%"==" 已连接 " ( echo 以太网已启用。 ) else ( echo 无法启用以太网。 ) ) endlocal exit 这段脚本首先通过netsh命令获取当前以太网的状态,然后根据状态进行相应的禁用启用操作。操作完成后,脚本会再次检查以太网的状态,以确保操作成功。脚本中的延迟操作是为了等待禁用/启用操作生效。最后,脚本会输出相应的操作结果。 请注意,在运行此脚本前,请确保以管理员权限运行命令提示符窗口。此外,脚本中的"以太网"是根据具体设备名称而定,可能需要根据实际情况进行调整。 ### 回答3: 以下是一个简单的批处理脚本,用于一键禁用/启用以太网: @echo off echo 请选择要执行的操作: echo [1] 禁用以太网 echo [2] 启用以太网 set /p option=请选择: if %option%==1 ( netsh interface set interface "以太网" admin=disable echo 以太网已禁用。 ) else if %option%==2 ( netsh interface set interface "以太网" admin=enable echo 以太网已启用。 ) else ( echo 无效的选项,请重新运行脚本。 ) 上述脚本通过使用netsh命令来控制以太网的启用/禁用状态。用户可以选择1来禁用以太网,选择2来启用以太网。脚本会根据用户的选择执行相应的命令,并显示操作结果。 请注意,脚本中的"以太网"可能需要根据您的系统进行适当的更改。您可以通过在cmd中运行"netsh interface show interface"命令来查看以太网接口的名称,并将其作为脚本中"netsh interface set interface"命令中的参数进行替换。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值