 鲜花( 3)  鸡蛋( 1)
|
方法一:NetSH大法~( D; H. _; |2 f8 r2 P5 n5 p
采用下列Netsh脚本- }& G! d- _) `3 V4 |+ W q
interface
7 d# Y7 [3 `! X, t6 e- Uset interface name="Local Area Connection" admin=ENABLED/DISABLED
8 w" x) Y; b! w2 e
# J1 r: |: P$ f V. J! @方法二:Powershell+WMI大法, {0 u8 c1 f' ~5 f, }; H
$adapter = get-wmiobject Win32_NetworkAdapter -filter "NetConnectionID='Local Area Connection'"
, Z! j1 p# {: H, B; g- D9 |. L$adapter.Disable()' E" ?9 v) E: k
9 P9 C9 c, Q; ~% F
同理可以VBS+WMI大法~
4 v5 R% b& a8 M" L$ j这个就不说了~没啥意义了~6 L/ P/ l5 c" l3 |; E7 B" |
2 k" M7 b) f! k" @7 [
其中有一个非常重要的问题就是权限控制问题!8 J& O) g2 ^! a7 B
Windows 7的UAC很严格,不能像WinXP里那样随意修改系统设置而不通知用户。
# ?( o! P" X$ d/ S9 o如果不赋予脚本执行环境以Administrative权限的话,脚本会执行失败,WMI返回码为05,Netsh报错:“An interface with this name is not registered with the router.”1 g3 d. S$ ^- Q9 V8 y: `* B
因此在启动Netsh或者Powershell时需要指定 Administrative权限,可通过右键菜单Run as Administrator实现。. q: Y3 ]+ ~- D* M' _) F0 l
然后会出现UAC,Allow即可~
% ?% r6 n- i3 i由于UAC权限具有继承性,即一个具有Administrative权限的Process启动的Process默认是具有Administrative权限的(除非显式的禁止继承)~
& N: Q0 `( q5 d: A因此可以通过启动一个有Administrative的Cmd或者Powershell的Shell来让脚本具有Administrative权限~
5 h$ A& y5 I1 U x8 q+ ?6 X0 K+ `8 f$ b
该方法仅在Win7下测试通过~WinXP下不通过~5 z: u& X$ T# y. S* e3 b1 O, R% n# w
似乎原因是由于WinXP的WMI Class不支持Enable和Disable方法造成的~具体解决方案未知。 |
|