{"id":70193,"date":"2005-03-21T08:38:00","date_gmt":"2005-03-21T08:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/21\/how-can-i-be-notified-any-time-a-network-cable-gets-unplugged\/"},"modified":"2005-03-21T08:38:00","modified_gmt":"2005-03-21T08:38:00","slug":"how-can-i-be-notified-any-time-a-network-cable-gets-unplugged","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-be-notified-any-time-a-network-cable-gets-unplugged\/","title":{"rendered":"How Can I Be Notified Any Time a Network Cable Gets Unplugged?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Is there a way I can be notified any time a network cable gets unplugged on a computer?<BR><BR>&#8212; DD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DD. A lot of people, when growing up, had a grandmother or an aunt or someone somewhere who had a house with a big attic, an attic overflowing with stuff. Admittedly, most of the time you had no idea what you were looking at when you wandered through there, but every now and then you came across something really cool and really useful. Every visit to the attic was like a treasure hunt, and you never knew what you might find there.<\/P>\n<P>The WMI equivalent of Grandma\u2019s attic is the <B>root\\wmi<\/B> namespace. There\u2019s quite a lot of stuff in there &#8211; 374 classes on our Windows XP test machine &#8211; but a lot of the classes either are not implemented or don\u2019t return any data. Every now and then, however, you stumble upon a class that does something really useful, something like, say, notifying you any time a network connection is lost. Just like a treasure hunt.<\/P>\n<P>As it turns out, the <B>MSNdis_StatusMediaDisconnect<\/B> class &#8211; a WMI event class &#8211; can monitor a computer and notify you any time a network connection has been lost (whether that\u2019s because of a network cable being pulled or some other reason). As we often do, let\u2019s show you the script and then explain how it works:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\wmi&#8221;)\nSet colMonitoredEvents = objWMIService.ExecNotificationQuery _\n    (&#8220;Select * from MSNdis_StatusMediaDisconnect&#8221;) <\/p>\n<p>Do While True \n    Set strLatestEvent = colMonitoredEvents.NextEvent \n    Wscript.Echo &#8220;A network connection has been lost:&#8221;\n    WScript.Echo strLatestEvent.InstanceName, Now\n    Wscript.Echo \nLoop\n<\/PRE>\n<P>If you have at least <I>some<\/I> experience with WMI event scripts, this one should look very familiar. (If you <I>don\u2019t<\/I> have much experience with WMI event scripts, you might want to check out this <A href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032268754&amp;culture=en-US\" target=\"_blank\"><B>Scripting Week 2 webcast<\/B><\/A>.) We begin by connecting to the WMI service; note that we connect to <B>root\\wmi<\/B> rather than root\\cimv2. We then use <B>ExecNotificationQuery<\/B> to request notification any time a new instance of the <B>MSNdis_StatusMediaDisconnect<\/B> class is created. As you might have guessed, a new instance of this class is created any time a network connection is lost.<\/P>\n<P>We then set up an infinite loop; by design this script will continually monitor the computer for new instances of the MSNdis_StatusMediaDisconnect class. (In fact, the only way to get it to <I>stop<\/I> monitoring is to terminate the script or the script process.) The script then pauses on this line of code, waiting for the next network disconnection event:<\/P><PRE class=\"codeSample\">Set strLatestEvent = colMonitoredEvents.NextEvent\n<\/PRE>\n<P>So what happens if we pull the cable and we disconnect the computer from the network? In that case a network disconnection event will occur, a new instance of the MSNdis_StatusMediaDisconnect class will be created, and the script will issue a notification that looks something like this:<\/P><PRE class=\"codeSample\">A network connection has been lost:\nMicrosoft Wireless Notebook Adapter MN-720 3\/172005 8:28:34 AM\n<\/PRE>\n<P>Cool, huh? The script will then loop around and wait for the next network disconnection to occur. Should there be another such disconnection then another notification will be issued.<BR><\/P>\n<TABLE id=\"EFE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Of course, you aren\u2019t limited to echoing messages to the screen. You could have your script do pretty much anything in the event of a network disconnection; we just wanted to keep things simple.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Incidentally, there\u2019s a similar class &#8211; <B>MSNdis_StatusMediaConnect<\/B> &#8211; that can notify you any time a network connection has been <I>made<\/I>. Here\u2019s a sample script that does just that:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\wmi&#8221;)\nSet colMonitoredEvents = objWMIService.ExecNotificationQuery _\n    (&#8220;Select * from MSNdis_StatusMediaConnect&#8221;) <\/p>\n<p>Do While True \n    Set strLatestEvent = colMonitoredEvents.NextEvent \n    Wscript.Echo &#8220;A network connection has been made:&#8221;\n    WScript.Echo strLatestEvent.InstanceName, Now\n    Wscript.Echo \nLoop\n<\/PRE>\n<P>Unfortunately we don\u2019t know of an easy way to combine the two scripts into one (that is, a single script that could notify you of both network connections and network disconnections). There is at least one way this can be done, but there are some security concerns with that approach so we can\u2019t recommend it. For the time being, then, you\u2019ll need to run two separate scripts if you\u2019d like to monitor for both kinds of events. But we\u2019ll keep rummaging through the WMI attic; who knows what else we might find in there.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there a way I can be notified any time a network cable gets unplugged on a computer?&#8212; DD Hey, DD. A lot of people, when growing up, had a grandmother or an aunt or someone somewhere who had a house with a big attic, an attic overflowing with stuff. Admittedly, most [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[42,3,4,5],"class_list":["post-70193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there a way I can be notified any time a network cable gets unplugged on a computer?&#8212; DD Hey, DD. A lot of people, when growing up, had a grandmother or an aunt or someone somewhere who had a house with a big attic, an attic overflowing with stuff. Admittedly, most [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}