【问题标题】:Adding some kind of event listener for network connectivity为网络连接添加某种事件监听器
【发布时间】:2013-06-12 18:23:13
【问题描述】:
我可以使用以下资源:
VBScript、WMI 查询、注册表项/值、.bat 批处理文件
使用这些资源的任意组合,我需要创建某种事件侦听器,当计算机重新建立与网络的连接时将触发 .bat 文件。
我在实验室工作,经常有机器更换网络,我们正在使用 BGinfo.exe 在背景图像中显示计算机所在的网络。所以我要做的是设置一个监听器,检查网络是否丢失连接并重新连接,然后重新运行批处理文件以更新背景。
我该如何做eventListener.on('connection', goIntoSomeCallbackFunction) 之类的事情?如果使用 VBscript 无法做到这一点,那么还有其他选择吗?
【问题讨论】:
标签:
batch-file
vbscript
wmi
【解决方案1】:
这是我的解决方案:
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()