埃德蒙顿华人社区-Edmonton China

 找回密码
 注册
查看: 2809|回复: 0

Windows 7下,脚本启用或者关闭网卡的方法

[复制链接]
鲜花(3) 鸡蛋(1)
发表于 2011-3-4 21:58 | 显示全部楼层 |阅读模式
老杨团队,追求完美;客户至上,服务到位!
方法一:NetSH大法~
& P, ?6 f# U  n, P采用下列Netsh脚本
* `2 f1 F4 B$ S1 ]interface
# f* [; s  Y- H4 wset interface name="Local Area Connection" admin=ENABLED/DISABLED
4 V# Y% |# F% E3 o2 N6 \3 n' G8 L. W7 B
方法二:Powershell+WMI大法
1 j  p2 C' \  U" p. W, z$adapter = get-wmiobject Win32_NetworkAdapter -filter "NetConnectionID='Local Area Connection'"# X5 Z2 D: o; U+ J9 K
$adapter.Disable()
9 L. B' C  b0 d  V  Q5 g3 k/ @& Q' K9 a! e; ?
同理可以VBS+WMI大法~
& k/ e/ q- k& f/ h0 ?2 r. f这个就不说了~没啥意义了~
  X3 u  A) {( h  l% h6 K# u  f; p' Z
其中有一个非常重要的问题就是权限控制问题!
0 E* H2 q, E' nWindows 7的UAC很严格,不能像WinXP里那样随意修改系统设置而不通知用户。
" G5 }3 T) D) E4 P& j如果不赋予脚本执行环境以Administrative权限的话,脚本会执行失败,WMI返回码为05,Netsh报错:“An interface with this name is not registered with the router.”* U( T; L( K" c# N# U( `9 ~; J
因此在启动Netsh或者Powershell时需要指定 Administrative权限,可通过右键菜单Run as Administrator实现。( i. P; ?9 @7 }* Q' n; g0 x
然后会出现UAC,Allow即可~
" H6 T; j% o# n( ^$ X由于UAC权限具有继承性,即一个具有Administrative权限的Process启动的Process默认是具有Administrative权限的(除非显式的禁止继承)~& T( v, g" |+ _, z& Z& ~, a3 ^0 Z
因此可以通过启动一个有Administrative的Cmd或者Powershell的Shell来让脚本具有Administrative权限~
& c2 M1 z$ Z& r5 Q) c( U# `  o: s- _5 {# s( z1 N* H9 B+ \5 `
该方法仅在Win7下测试通过~WinXP下不通过~+ {$ ?" G0 U, S  w0 P! ?
似乎原因是由于WinXP的WMI Class不支持Enable和Disable方法造成的~具体解决方案未知。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系我们|小黑屋|手机版|Archiver|埃德蒙顿中文网

GMT-7, 2026-3-31 06:21 , Processed in 0.283201 second(s), 10 queries , Gzip On, APC On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表