{"id":1922,"date":"2016-09-01T18:11:33","date_gmt":"2016-09-01T18:11:33","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=1922"},"modified":"2016-09-01T18:11:44","modified_gmt":"2016-09-01T18:11:44","slug":"how-can-i-be-notified-any-time-a-service-goes-down","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=1922","title":{"rendered":"How Can I Be Notified Any Time a Service Goes Down?"},"content":{"rendered":"<p>How can I be notified any time a service goes down?<\/p>\n<p>There are a couple different ways you can do this, but perhaps the simplest approach is to create a script that monitors WMI events. We don\u2019t have time to do a detailed explanation of WMI events in this column, but the basic idea is that you can ask WMI to notify you when certain things happen. For example, you might request notification when hard disk space drops below a specified amount, or when CPU consistently spikes above a pre-determined level. Likewise, you can request notification any time a service changes state; that is, any time a service that was running is stopped, or any time a service that was stopped is re-started. If a service goes down, you can ask WMI to tell you about it.<\/p>\n<p>&nbsp;<\/p>\n<p>The following script checks for changes in service status; every 30 seconds it looks to see if any services have changed state. The script is set up in an endless loop, which means that, once started, it will continue to run forever and ever, or at least until you reboot the machine or terminate the script process.<\/p>\n<pre class=\"codeSample\">strComputer = \u201c.\u201d\r\nSet objWMIService = GetObject(\u201cwinmgmts:\u201d _\r\n    &amp; \u201c{impersonationLevel=impersonate}!\\\\\u201d &amp; strComputer &amp; \u201c\\root\\cimv2\u201d)\r\nSet colServices = objWMIService. _\r\n    ExecNotificationQuery(\u201cSelect * from __InstanceModificationEvent \u201d _\r\n        &amp; \u201cwithin 30 where TargetInstance isa \u2018Win32_Service'\u201d)\r\ni = 0\r\nDo While i = 0\r\n    Set objService = colServices.NextEvent\r\n    If objService.TargetInstance.State &lt;&gt; _\r\n        objService.PreviousInstance.State Then\r\n        Wscript.Echo objService.TargetInstance.Name _\r\n            &amp;  \u201d is \u201d &amp; objService.TargetInstance.State _\r\n                &amp; \u201c. The service previously was \u201d &amp; objService.PreviousInstance.State &amp; \u201c.\u201d\r\n    End If\r\nLoop\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>As you can see, pretty easy. Any time a service is modified in any way, an instance of the __InstanceModificationEvent class is created. The script compares the current state of the modified service (TargetInstance.State) with the previous state of the service (PreviousInstance.State). What if the two are the same; that is, what if the service is currently running, and was previously running? In that case, we just ignore the event; that means that some other property of the service was modified. (For example, you might have changed the service password, or maybe switched the service from Manual start to Autostart.) If the two states differ \u2013 that is, if the service used to be running but now is stopped \u2013 a message box pops up telling us the name of the service, its current state, and its previous state.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How can I be notified any time a service goes down? There are a couple different ways you can do this, but perhaps the simplest approach is to create a script that monitors WMI events. We don\u2019t have time to do a detailed explanation of WMI events in this column, but the basic idea is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1922","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/1922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1922"}],"version-history":[{"count":2,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/1922\/revisions"}],"predecessor-version":[{"id":1924,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/1922\/revisions\/1924"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}