A. Mikkelsen VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Friday, June 18, 2010

Powershell – Check if a service is running

Filed under: PowerShell — Tags: , , , , , , — A. Mikkelsen @ 9:22 am

If you ever have experienced that after your vCenter server reboots – due to Windows updates, the “VMware VirtualCenter Server” service is not starting?

I have seen it a few times and every time it’s during a weekend 🙁

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.
To make sure you know if the service wasn’t started after a reboot or other cause, I have added a mail function to the script 🙂

function FuncCheckService{
    param($ServiceName)
    $arrService = Get-Service -Name $ServiceName
    if ($arrService.Status -ne "Running"){
        Start-Service $ServiceName
        FuncMail -To "to-email@domain.com" -From "from-mail@domain.com"  -Subject "Servername : ($ServiceName) service started." -Body "Service $ServiceName started" -smtpServer "relay.mailserver.com"
    }
}

function FuncMail {
    #param($strTo, $strFrom, $strSubject, $strBody, $smtpServer)
    param($To, $From, $Subject, $Body, $smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = $From
    $msg.To.Add($To)
    $msg.Subject = $Subject
    $msg.IsBodyHtml = 1
    $msg.Body = $Body
    $smtp.Send($msg)
}

FuncCheckService -ServiceName "VMware VirtualCenter Server"

Create a PS1 file and schedule it to run every 15 or 30 minutes.

It works great and is simple….

11 Comments »

  1. nice and easy, tweaked a bit, i wanted mine to email me when a certain service starts.

    Comment by B — Thursday, February 2, 2012 @ 1:30 am

  2. Glad to be able to help…..

    Comment by A. Mikkelsen — Thursday, February 2, 2012 @ 11:50 am

  3. I am getting the following error, any ideas?

    Exception calling “Send” with “4” argument(s): “The parameter ‘from’ cannot be an empty string.
    Parameter name: from”
    At Z:\EmpowerSchedulerServicStatus.ps1:15 char:15
    + $smtp.Send <<<< ($emailFrom, $emailTo, $subject, $body)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    Comment by bugsfix — Sunday, April 8, 2012 @ 2:35 am

  4. Hi,
    What are you calling the function with?
    The error says that you are missing the FROM variable (-From “from-mail@domain.com”).

    It should be like
    FuncMail -To “to-email@domain.com” -From “from-mail@domain.com” -Subject “Servername : ($ServiceName) service started.” -Body “Service $ServiceName started” -smtpServer “relay.mailserver.com”

    A. Mikkelsen

    Comment by A. Mikkelsen — Sunday, April 8, 2012 @ 19:44 pm

  5. how do you put the code in to do secure connection. i get the following error when i run this script

    Exception calling “Send” with “1” argument(s): “The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated”

    thanks

    Comment by steve — Tuesday, April 10, 2012 @ 10:46 am

  6. Hi Steve,

    Just found these blogs.
    http://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage
    http://gallery.technet.microsoft.com/scriptcenter/cfcbd6a4-ef7d-474c-938a-ff8cdb4fce03

    Try using this function instead, remember to change the username, password and smtp server port:

    function FuncMail {
    #param($strTo, $strFrom, $strSubject, $strBody, $smtpServer)
    param($To, $From, $Subject, $Body, $smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
    $smtp.Credentials = New-Object System.Net.NetworkCredential(“username”, “password”);
    $smtp.EnableSsl = $true
    $msg.From = $From
    $msg.To.Add($To)
    $msg.Subject = $Subject
    $msg.IsBodyHtml = 1
    $msg.Body = $Body
    $smtp.Send($msg)
    }

    BR
    A. Mikkelsen

    Comment by A. Mikkelsen — Tuesday, April 10, 2012 @ 19:58 pm

  7. Sorry to bother you, this still doesnt seem to work. when i use that code it returns the following
    Exception calling “Send” with “1” argument(s): “Failure sending mail.”
    At F:\services.ps1:22 char:12
    + $smtp.Send <<<< ($msg)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    if i remove the arguments it presents me with this but doesnt seem to have sent any email

    MemberType : Method
    OverloadDefinitions : {System.Void Send(string from, string recipients, string subject, string body), System.Void Send(
    System.Net.Mail.MailMessage message)}
    TypeNameOfValue : System.Management.Automation.PSMethod
    Value : System.Void Send(string from, string recipients, string subject, string body), System.Void Send(S
    ystem.Net.Mail.MailMessage message)
    Name : Send
    IsInstance : True

    Comment by steve — Wednesday, April 11, 2012 @ 9:51 am

  8. for your information a copy of the full script is below:

    function FuncCheckService{
    param($ServiceName)
    $arrService = Get-Service -Name $ServiceName
    if ($arrService.Status -ne “Running”){
    Start-Service $ServiceName
    FuncMail -To “myemail@mydomain.co.uk” -From “Server.Events@mydomain.co.uk” -Subject “server :

    ($ServiceName) service started.” -Body “Service $ServiceName started” -smtpServer “server.mydomain.local”
    }
    }

    function FuncMail {
    #param($strTo, $strFrom, $strSubject, $strBody, $smtpServer)
    param($To, $From, $Subject, $Body, $smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
    $smtp.Credentials = New-Object System.Net.NetworkCredential(“my-domain\administrator”, “password”);
    $smtp.EnableSsl = $true
    $msg.From = $From
    $msg.To.Add($To)
    $msg.Subject = $Subject
    $msg.IsBodyHtml = 1
    $msg.Body = $Body
    $smtp.Send($msg)
    }

    FuncCheckService -ServiceName “myservice”

    Comment by steve — Wednesday, April 11, 2012 @ 10:07 am

  9. Hmm,

    I’ll look at it ASAP

    A. Mikkelsen

    Comment by A. Mikkelsen — Wednesday, April 11, 2012 @ 18:48 pm

  10. It tells me “cannot find any service with service name:” even though I can start it in the command prompt using the net start command. Any ideas would be muchly appreciated.

    Comment by Matthew — Wednesday, April 18, 2012 @ 16:12 pm

  11. If your service name contains spaces, the put ” in front and behind of the service name.

    A. Mikkelsen

    Comment by A. Mikkelsen — Wednesday, April 18, 2012 @ 18:35 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress