VBS监视网络连接与断开的实例代码

发布时间:2020-06-08编辑:脚本学堂
用vbs代码监视网络的连接与断开,必要时加到定时任务中,实时监视网络的状态,会有点用的。

vbs监视网络连接的代码:
 

复制代码 代码示例:

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & strComputer & "rootwmi")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from MSNdis_StatusMediaConnect")

Do While True
    Set strLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo "A network connection has been made:"
    WScript.Echo strLatestEvent.InstanceName, Now
    Wscript.Echo
Loop

vbs监视网络断开的代码:
 

复制代码 代码示例:

strComputer = "."

Set objWMIService = GetObject("winmgmts:" & strComputer & "rootwmi")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery("Select * from MSNdis_StatusMediaDisconnect")

Do While True
    Set strLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo "A network connection has been lost:"
    WScript.Echo strLatestEvent.InstanceName, Now
Loop

这个年代了,vbs还是很好用的,回想当年搞asp时,经常用到这些,用vbs监视网络的连接与断开状态,个人感觉还是不错的,经济实用啊。