{"id":472,"date":"2010-06-18T09:22:30","date_gmt":"2010-06-18T08:22:30","guid":{"rendered":"http:\/\/www.amikkelsen.com\/?p=472"},"modified":"2010-06-22T20:58:18","modified_gmt":"2010-06-22T19:58:18","slug":"powershell-check-if-a-service-is-running","status":"publish","type":"post","link":"http:\/\/www.amikkelsen.com\/?p=472","title":{"rendered":"Powershell &#8211; Check if a service is running"},"content":{"rendered":"<p>If you ever have experienced that after your vCenter server reboots &#8211; due to Windows updates, the &#8220;VMware VirtualCenter Server&#8221; service is not starting?<\/p>\n<p>I have seen it a few times and every time it&#8217;s during a weekend \ud83d\ude41<\/p>\n<p>So to make sure your vCenter service (or other) is always running you could use this powershell script to check if a service is running, and if not start it.<br \/>\nTo make sure you know if the service wasn&#8217;t started after a reboot or other cause, I have added a mail function to the script \ud83d\ude42<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nfunction FuncCheckService{\r\n    param($ServiceName)\r\n    $arrService = Get-Service -Name $ServiceName\r\n    if ($arrService.Status -ne &quot;Running&quot;){\r\n        Start-Service $ServiceName\r\n        FuncMail -To &quot;to-email@domain.com&quot; -From &quot;from-mail@domain.com&quot;  -Subject &quot;Servername : ($ServiceName) service started.&quot; -Body &quot;Service $ServiceName started&quot; -smtpServer &quot;relay.mailserver.com&quot;\r\n    }\r\n}\r\n\r\nfunction FuncMail {\r\n    #param($strTo, $strFrom, $strSubject, $strBody, $smtpServer)\r\n    param($To, $From, $Subject, $Body, $smtpServer)\r\n    $msg = new-object Net.Mail.MailMessage\r\n    $smtp = new-object Net.Mail.SmtpClient($smtpServer)\r\n    $msg.From = $From\r\n    $msg.To.Add($To)\r\n    $msg.Subject = $Subject\r\n    $msg.IsBodyHtml = 1\r\n    $msg.Body = $Body\r\n    $smtp.Send($msg)\r\n}\r\n\r\nFuncCheckService -ServiceName &quot;VMware VirtualCenter Server&quot;\r\n<\/pre>\n<p>Create a PS1 file and schedule it to run every 15 or 30 minutes.<\/p>\n<p>It works great and is simple&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you ever have experienced that after your vCenter server reboots &#8211; due to Windows updates, the &#8220;VMware VirtualCenter Server&#8221; service is not starting? I have seen it a few times and every time it&#8217;s during a weekend \ud83d\ude41 So to make sure your vCenter service (or other) is always running you could use this\u2026 <span class=\"read-more\"><a href=\"http:\/\/www.amikkelsen.com\/?p=472\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[50,330,59,240,132,52,37],"class_list":["post-472","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-mail","tag-powershell","tag-server","tag-servicce","tag-vcenter","tag-virtualcenter","tag-vmware"],"_links":{"self":[{"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=472"}],"version-history":[{"count":3,"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts\/472\/revisions"}],"predecessor-version":[{"id":670,"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=\/wp\/v2\/posts\/472\/revisions\/670"}],"wp:attachment":[{"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amikkelsen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}