萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> VBS監視網絡連接與斷開的代碼

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

監視網絡斷開:

復制代碼 代碼如下:


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

copyright © 萬盛學電腦網 all rights reserved