2009年5月27日 星期三

依照有線網路連線狀態停/啟用無線

windows xp/2003預設的netsh雖然在help有說明有disable,不過實際上disable的參數不管怎樣設定都會失敗~~
要解決這個問題,必須透過微軟的devcon來控制裝置~~
On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
Col_WirelessObj=Split(objShell.Exec("%ComSpec% /c devcon.exe find * |findstr /I wireless|findstr /I DEV").StdOut.ReadAll,VbCrLf)
If UBound(Col_WirelessObj) = 0 Then
Wscript.Echo "Wireless device is not found"
Wscript.Quit
Else
ReDim DEVID_Wireless(0)
ReDim Prod_Wireless(0)

For index=0 to (UBound(Col_WirelessObj)-1)
If Instr(Col_WirelessObj(index),"PCI") Then
Buf=Split(Col_WirelessObj(index),"&")
If UBound(Buf) = 6 Then
DEVID_Wireless(UBound(DEVID_Wireless)) = Buf(1)
Prod_Wireless(UBound(Prod_Wireless)) = Trim(Split(Buf(6),":")(1))
ReDim Preserve DEVID_Wireless(UBound(DEVID_Wireless)+1)
ReDim Preserve Prod_Wireless(UBound(Prod_Wireless)+1)
End If
End If
Next
End If

Set objWMIService = GetObject("winmgmts:{(Security)}\\.\root\cimv2")

Set colEvent = objWMIService.ExecNotificationQuery("Select * from __InstanceCreationEvent within 60 Where TargetInstance ISA 'Win32_NTLogEvent' AND (TargetInstance.Logfile='System' AND (TargetInstance.EventCode=4 OR TargetInstance.EventCode=9))")
'EventCode 4 for LinkDown; 9 For LinkUp
Do
Set CurrentEvent = colEvent.NextEvent
If CurrentEvent.TargetInstance.EventCode = 4 Then
For Index=0 to (UBound(DevID_Wireless)-1)
Wscript.Echo "Enable " & Prod_Wireless(index)
objShell.Exec("%ComSpec% /c devcon.exe enable *" & DEVID_Wireless(Index) & "*")
Next
Else
For Index=0 to (UBound(DevID_Wireless)-1)
Wscript.Echo "Disable " & Prod_Wireless(index)
objShell.Exec("%ComSpec% /c devcon.exe disable *" & DEVID_Wireless(Index) & "*")
Next
End If
Loop

沒有留言: