{"id":110,"date":"2006-12-03T20:45:12","date_gmt":"2006-12-03T18:45:12","guid":{"rendered":"http:\/\/contoso.se\/blog\/?p=110"},"modified":"2006-12-29T18:03:07","modified_gmt":"2006-12-29T16:03:07","slug":"starta-om-tjanst-om-process-saknas","status":"publish","type":"post","link":"http:\/\/contoso.se\/blog\/?p=110","title":{"rendered":"Restart a service if a process is missing"},"content":{"rendered":"<p>This is a script that will restart a service if a process is missing. I this example\u00c2\u00a0the DFS service will be\u00c2\u00a0restarted if not notepad.exe is running. Maybe not a very likely\u00c2\u00a0scenario :)\u00c2\u00a0<\/p>\n<p><code><br \/>\nstrProcess = \"notepad.exe\" 'Name of the process<br \/>\nstrServiceName = \"DFS\" 'Name of the service<\/code><code> <\/code><code>strComputer = \".\"<\/code><code>Set objWMIService = GetObject(\"winmgmts:\\\\\" & strComputer & \"\\root\\cimv2\")<br \/>\nSet objShell = CreateObject(\"Wscript.Shell\")<\/p>\n<p>Set colProcesses = objWMIService.ExecQuery _<br \/>\n(\"Select * from Win32_Process Where Name = '\" & strProcess & \"'\")<br \/>\nIf colProcesses.Count < 1 Then\n\nConst EVENT_WARNING = 2\nSet objShell = CreateObject(\"Wscript.Shell\")\nobjShell.LogEvent 2, strProcess &#038; \" is not running. The following process will be restarted: \" &#038; strServiceName\n\nSet objWMIService = GetObject(\"winmgmts:\" _\n&#038; \"{impersonationLevel=impersonate}!\\\\\" &#038; strComputer &#038; \"\\root\\cimv2\")\n\nSet colListOfServices = objWMIService.ExecQuery _\n(\"Select * from Win32_Service Where Name = '\" &#038; strServiceName &#038; \"'\")\nFor Each objService in colListOfServices\nIf objService.State = \"Running\" Then\nobjService.StopService\n\nstrNow = now\nDo Until DateDiff(\"s\", strNow, now) > 10<br \/>\nLoop<\/p>\n<p>End IF<br \/>\nobjService.StartService<br \/>\nNext<br \/>\nEnd If<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a script that will restart a service if a process is missing. I this example\u00c2\u00a0the DFS service will be\u00c2\u00a0restarted if not notepad.exe is running. Maybe not a very likely\u00c2\u00a0scenario :)\u00c2\u00a0 strProcess = &#8220;notepad.exe&#8221; &#8216;Name of the process strServiceName = &#8220;DFS&#8221; &#8216;Name of the service strComputer = &#8220;.&#8221;Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &#038; strComputer &#038; &hellip; <a href=\"http:\/\/contoso.se\/blog\/?p=110\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[2,13],"tags":[],"_links":{"self":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/110"}],"collection":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":0,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}