埃德蒙顿华人社区-Edmonton China

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

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

[复制链接]
鲜花(3) 鸡蛋(1)
发表于 2011-3-4 21:58 | 显示全部楼层 |阅读模式
老杨团队,追求完美;客户至上,服务到位!
方法一:NetSH大法~
/ t) s0 ^. [% a7 h7 [6 z采用下列Netsh脚本- ~9 K. B3 @: d2 D& n8 l
interface
$ n' b4 R  _/ Z. U) i% }$ tset interface name="Local Area Connection" admin=ENABLED/DISABLED& I9 r$ U0 v% {) y5 B( j

! ?+ o# f2 g% f) d/ [' A+ c: ~方法二:Powershell+WMI大法& e+ r8 H/ U( {3 H, k7 j; {
$adapter = get-wmiobject Win32_NetworkAdapter -filter "NetConnectionID='Local Area Connection'"+ t0 r! X) a- I. |) p; n  A4 \
$adapter.Disable()5 O- I9 O+ n4 R; E' e  j  h
$ ~  |$ X3 t5 t
同理可以VBS+WMI大法~
9 Y4 _  U6 G' ]0 r2 U* _$ K这个就不说了~没啥意义了~3 F. B* Z/ S7 y2 v2 [$ I) G

: m. o: i2 {7 F9 i其中有一个非常重要的问题就是权限控制问题!) J6 h% k6 i* r4 V" D
Windows 7的UAC很严格,不能像WinXP里那样随意修改系统设置而不通知用户。
3 g" z  X& c; W8 V# I) W如果不赋予脚本执行环境以Administrative权限的话,脚本会执行失败,WMI返回码为05,Netsh报错:“An interface with this name is not registered with the router.”  m+ R* B7 t- J- L
因此在启动Netsh或者Powershell时需要指定 Administrative权限,可通过右键菜单Run as Administrator实现。
8 c& s* Z6 L3 |然后会出现UAC,Allow即可~
8 P4 b) T3 K1 g8 l9 V, b" j由于UAC权限具有继承性,即一个具有Administrative权限的Process启动的Process默认是具有Administrative权限的(除非显式的禁止继承)~
2 X& ~* o1 H# _因此可以通过启动一个有Administrative的Cmd或者Powershell的Shell来让脚本具有Administrative权限~6 W7 G( D0 r/ `, c+ i0 M

( L0 s' q5 d+ L% b! y6 W; j* W2 f该方法仅在Win7下测试通过~WinXP下不通过~/ m) Z. K, H5 k3 n5 |4 t" \; w
似乎原因是由于WinXP的WMI Class不支持Enable和Disable方法造成的~具体解决方案未知。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT-7, 2025-9-15 02:21 , Processed in 0.174802 second(s), 9 queries , Gzip On, APC On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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