2013-06-12 32 views
1

我已經在我手上了以下資源:添加某種事件偵聽器用於網絡連接

VBScript中,WMI查詢,註冊表鍵/值的.bat批處理文件

使用這些資源的任意組合我需要創建某種事件監聽器,當計算機重新建立與網絡的連接時將觸發.bat文件。

我在實驗室工作,不斷有機器更換網絡,我們正在使用BGinfo.exe來顯示計算機在背景圖像上的網絡。所以我想要做的是設置一個偵聽器,檢查網絡是否失去連接並重新連接,然後重新運行批處理文件以更新背景。

我該怎麼做類似eventListener.on('connection', goIntoSomeCallbackFunction)?如果這是不可能的使用VBScript,那麼是否有其他選擇?

+0

可能重複:http://stackoverflow.com/questions/2101041/capturing-network-status-change-event – Papasmile

回答

0

這裏是我的解決方案:

dim shell, ip 
set shell=createobject("wscript.shell") 


function main() 

Dim status, result 

result = False 


'Here we have to loop every few seconds to look at connectivity 
Do 

result = statusChanged() 

If result Then 
    shell.Run "D:\tools\Batches\updateBG.bat" 
End If 

WScript.Sleep(1000) 

Loop While True 

End function 

function statusChanged() 

Dim found, status 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 

status = False 
found = False 

For Each result in colItems 
    If Not isNull(result.IPAddress) Then 
     If ip <> result.IPAddress(0) Then 
      ip = result.IPAddress(0) 
      status = True 
     End If 
    End If 
Next 

statusChanged = status 

End function 

main()