欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-06 12:58:00
监视网络连接: 复制代码 代码如下:strcomputer = "." set objwmiservice = getobject("winmgmts:\\" & st...

监视网络连接:

复制代码 代码如下:

strcomputer = "."

set objwmiservice = getobject("winmgmts:\\" & strcomputer & "\root\wmi")
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 & "\root\wmi")
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