用户登录
用户注册

分享至

用vbs实现本地连接禁用/启用脚本代码

  • 作者: 想念你的腿想念你的嘴
  • 来源: 51数据库
  • 2021-07-28
转载至 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 
s = "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 
软件
前端设计
程序设计
Java相关