埃德蒙顿华人社区-Edmonton China

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

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

[复制链接]
鲜花(3) 鸡蛋(1)
发表于 2011-3-4 21:58 | 显示全部楼层 |阅读模式
老杨团队,追求完美;客户至上,服务到位!
方法一:NetSH大法~) l- F: X, R) s
采用下列Netsh脚本
8 u2 t5 k# ]  @) r9 Linterface
8 c) S; R6 {  N4 x) R* Cset interface name="Local Area Connection" admin=ENABLED/DISABLED7 ^* M. U' _; V5 W5 w. i1 c8 \
% _1 X, A- b6 ^! M3 g2 X
方法二:Powershell+WMI大法
. }7 Z. w" H" ?; D6 H$ y$adapter = get-wmiobject Win32_NetworkAdapter -filter "NetConnectionID='Local Area Connection'"$ k% L' `, d. h# n& ]
$adapter.Disable()6 O8 c7 R$ e' l
. a% \8 {) P) A& L4 |
同理可以VBS+WMI大法~+ c4 N$ L5 z$ J
这个就不说了~没啥意义了~
/ e0 H4 g8 S6 l5 D+ L
6 e& j; s" E, w  x) y其中有一个非常重要的问题就是权限控制问题!
; f. h/ S7 i4 K8 w9 mWindows 7的UAC很严格,不能像WinXP里那样随意修改系统设置而不通知用户。- `) I% {& [" G, S3 r: z, D
如果不赋予脚本执行环境以Administrative权限的话,脚本会执行失败,WMI返回码为05,Netsh报错:“An interface with this name is not registered with the router.”/ U5 l  S- H1 o0 _
因此在启动Netsh或者Powershell时需要指定 Administrative权限,可通过右键菜单Run as Administrator实现。7 q' O/ |- G5 d: O4 Z8 V
然后会出现UAC,Allow即可~
; \6 {8 O& w. G2 I由于UAC权限具有继承性,即一个具有Administrative权限的Process启动的Process默认是具有Administrative权限的(除非显式的禁止继承)~
1 b" s2 t& G( Y, m0 P- l8 h" ~0 w$ t5 N因此可以通过启动一个有Administrative的Cmd或者Powershell的Shell来让脚本具有Administrative权限~
* M1 n8 ~; F5 Y( `, L6 ]
2 g% P, n7 W9 g, t0 \- J7 I该方法仅在Win7下测试通过~WinXP下不通过~
+ l  Z5 R! w  P( C似乎原因是由于WinXP的WMI Class不支持Enable和Disable方法造成的~具体解决方案未知。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT-7, 2026-4-24 16:49 , Processed in 0.107033 second(s), 9 queries , Gzip On, APC On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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