方法一: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方法造成的~具体解决方案未知。