Powershell – Check if a service is running

By | June 22, 2010

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 thoughts on “Powershell – Check if a service is running

  1. bugsfix

    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

  2. A. Mikkelsen Post author

    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

  3. steve

    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

  4. A. Mikkelsen Post author

    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

  5. steve

    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

  6. steve

    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”

  7. Matthew

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *