海浩网论坛

标题: 谁来解释一下wscript脚本 disable/enable NIC [打印本页]

作者: takelook    时间: 2008-6-15 15:35:40     标题: 谁来解释一下wscript脚本 disable/enable NIC

俺需要个命令行来disable/enable NIC,发现下面这个脚本能做,可是啥意思呀?怎么能做到disable后再自动enable?

Const ssfCONTROLS = 3

sConnectionName = "Local Area Connection"

sEnableVerb = "En&able"
sDisableVerb = "Disa&ble"

set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)

set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = "Network Connections" then
set oNetConnections = folderitem.getfolder: exit for
end if
next

if oNetConnections is nothing then
msgbox "Couldn't find 'Network Connections' folder"
wscript.quit
end if

set oLanConnection = nothing
for each folderitem in oNetConnections.items
if lcase(folderitem.name) = lcase(sConnectionName) then
set oLanConnection = folderitem: exit for
end if
next

if oLanConnection is nothing then
msgbox "Couldn't find '" & sConnectionName & "' item"
wscript.quit
end if

bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
s = s & vbcrlf & verb.name
if verb.name = sEnableVerb then
set oEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set oDisableVerb = verb
end if
next

'debugging displays left just in case...
'
'msgbox s ': wscript.quit
'msgbox "Enabled: " & bEnabled ': wscript.quit

'not sure why, but invokeverb always seemed to work
'for enable but not disable.
'
'saving a reference to the appropriate verb object
'and calling the DoIt method always seems to work.
'
if bEnabled then
' oLanConnection.invokeverb sDisableVerb
oDisableVerb.DoIt
wscript.sleep 1000
for each folderitem in oNetConnections.items
if lcase(folderitem.name) = lcase(sConnectionName) then
set oLanConnection = folderitem: exit for
end if
next

set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
s = s & vbcrlf & verb.name
'msgbox verb.name
if verb.name = sEnableVerb then
set oEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set oDisableVerb = verb
end if
next

oEnableVerb.DoIt
else
' oLanConnection.invokeverb sEnableVerb
oEnableVerb.DoIt
end if

'adjust the sleep duration below as needed...
'
'if you let the oLanConnection go out of scope
'and be destroyed too soon, the action of the verb
'may not take...
'
wscript.sleep 60
作者: 妮可宝贝    时间: 2008-6-15 15:38:40

本人严正声明:此帖在全世界只有一个沙发。沙发两边的扶手同属于一个沙发。任何企图想制造两个或两个以上沙发的,或是一个沙发一个板凳的行为都必将遭到包括中国人民在内的全世界人民的反对和强烈谴责!
作者: sock    时间: 2008-6-15 15:43:39

万恶的妮可...占据了全世界的沙发....美利坚人民将和这种霸权主义斗争到底...
作者: masoo    时间: 2008-6-15 16:08:04

原帖由 妮可宝贝 于 2008-6-15 15:38 发表
本人严正声明:此帖在全世界只有一个沙发。沙发两边的扶手同属于一个沙发。任何企图想制造两个或两个以上沙发的,或是一个沙发一个板凳的行为都必将遭到包括中国人民在内的全世界人民的反对和强烈谴责!

啥意思?和LZ的主题不相合呀
作者: 哈哈鸟    时间: 2008-6-15 16:40:24

尼可是说谁回答正确这沙发就给谁
作者: 不在状态    时间: 2008-6-15 22:30:23

没人品的没人顶
作者: fish    时间: 2008-6-15 22:33:17

disable后再自动enable  NIC
是先变换网卡设置么。。。。方便运行批处理吗,用netsh exec config.txt命令挺好
作者: takelook    时间: 2008-6-15 23:34:47

2K的系统上没有netsh吧?
不是改变设置,只不过时不时笔记本网卡不听使唤

原帖由 fish 于 2008-6-15 22:33 发表
disable后再自动enable  NIC
是先变换网卡设置么。。。。方便运行批处理吗,用netsh exec config.txt命令挺好

作者: guodl    时间: 2008-6-16 03:26:22

用Script好象不太容易理解,要不要试一试DevCon,即命令行实用工具可替代设备管理器。不过这种方法是完全禁用硬件设备,然后再启用这个设备。

官方网页:http://support.microsoft.com/kb/311272/zh-cn

我自己试了一下:
先运行devcon hwids * > DevList.txt

然后打开这个文件,找你要禁用的网卡。我电脑上是
PCI\VEN_11AB&DEV_4320&SUBSYS_811A1043&REV_13\4&2E98101C&0&28F0
    Name: Marvell Yukon Gigabit Ethernet 10/100/1000Base-T Adapter, Copper RJ-45
    Hardware ID's:
        PCI\VEN_11AB&DEV_4320&SUBSYS_811A1043&REV_13
        PCI\VEN_11AB&DEV_4320&SUBSYS_811A1043
        PCI\VEN_11AB&DEV_4320&CC_020000
        PCI\VEN_11AB&DEV_4320&CC_0200
    Compatible ID's:
        PCI\VEN_11AB&DEV_4320&REV_13
        PCI\VEN_11AB&DEV_4320
        PCI\VEN_11AB&CC_020000
        PCI\VEN_11AB&CC_0200
        PCI\VEN_11AB
        PCI\CC_020000
        PCI\CC_0200
之后看Hardware ID后的列表,
   随便选一个,如果要禁用,

devcon disable PCI\VEN_11AB&DEV_4320&SUBSYS_811A1043&REV_13

如果要启用
devcon enable PCI\VEN_11AB&DEV_4320&SUBSYS_811A1043&REV_13

官方的网站http://technet2.microsoft.com/wi ... f1033.mspx?mfr=true有很多实用例子。这个网站也有一些介绍:http://bloger.5-it.com/article.asp?id=138
作者: guodl    时间: 2008-6-16 03:33:32

看一看这个,好象很具体:

http://www.experts-exchange.com/OS/Miscellaneous/Q_20708207.html

how to disable network connection using script



since i accept a comment before i checked thoroughly i will ask again

i have a WIN2K server with 2 ethernet cards. each card is in a different subnet
one is connected to the internet and the other is connected to the intranet.
i need a script that will close one and open the other and vice versa so both
cards will never be connected at the same time. ( disable routing is not an option )
the script will run by the scheduler or manualy.

the solution was to use devcon.exe command which is a command line device manager
here is the answer i got :

##################################################################
##################################################################

DevCon Command Line Utility Alternative to Device Manager
http://support.microsoft.com/?kbid=311272

devcon.exe hwids * >> c:\hardware.txt
Search for the NIC you'd like to disable (in my case DEV_1229)

Example:
C:\test\i386>devcon disable *DEV_1229*
PCI\VEN_8086&DEV_1229&SUBSYS_00408086&REV_0C\3&61AAA01&0&48 : Disable
1 device(s) disabled.

C:\test\i386>devcon enable *DEV_1229*
PCI\VEN_8086&DEV_1229&SUBSYS_00408086&REV_0C\3&61AAA01&0&48 : Enabled
1 device(s) enabled.

Works the same as right clicking on the NIC and choosing disable/enable.




欢迎光临 海浩网论坛 (http://bbs.highot.net./) Powered by Discuz! X2