powershell script to start a windows service if stopped. Email notification is sent

Discussion in 'Scripting & Programming' started by mbrown, Apr 11, 2014.

  1. mbrown

    mbrown Bit Poster

    27
    0
    2
    Hi All

    I have a powershell script which starts a service if its stopped and sends an email to a mailbox. The problem i have at the moment is, that the email is sent even if the service is already started. I only want an email to be sent if the service was stopped and then subsequently started.

    Please see script below:

    Code:
    function FuncCheckService{
    param($ServiceName)
    $arrService = Get-Service -Name $wuauserv
    if ($arrService.Status -ne "Running"){
    Start-Service -name wuauserv
    FuncMail -To "email.domain.com" -From "[email protected]"  -Subject "stopped and started : ($wuauserv) service started." -Body "Service $wuauserv started" -smtpServer "mail.domain.com"
    }
    }
    
    function FuncMail {
    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 "wuauserv"
    
    
    If anyone has any ideas, that would be great. Thanks
     
  2. Shadowrunner

    Shadowrunner Nibble Poster Premium Member

    68
    7
    37
    Try this:

    Code:
    function FuncCheckService{
    param($ServiceName)
    $SendMail = False
    $arrService = Get-Service -Name $wuauserv
    if ($arrService.Status -ne "Running"){
    $SendMail = True
    Start-Service -name wuauserv
    }
    If ($SendMail -eq "True"){
    FuncMail -To "email.domain.com" -From "[email protected]"  -Subject "stopped and started : ($wuauserv) service started." -Body "Service $wuauserv started" -smtpServer "mail.domain.com"
    }
    }
    
    function FuncMail {
    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 "wuauserv"
     
    Last edited by a moderator: Apr 18, 2014
  3. SimonD
    Honorary Member

    SimonD Terabyte Poster

    3,668
    428
    199
    Certifications: CNA | CNE | CCNA | MCP | MCP+I | MCSE NT4 | MCSA 2003 | Security+ | MCSA:S 2003 | MCSE:S 2003 | MCTS:SCCM 2007 | MCTS:Win 7 | MCITP:EDA7 | MCITP:SA | MCITP:EA | MCTS:Hyper-V | VCP 4 | ITIL v3 Foundation | VCP 5 DCV | VCP 5 Cloud | VCP6 NV | VCP6 DCV | VCAP 5.5 DCA
    WIP: VCP6-CMA, VCAP-DCD and Linux + (and possibly VCIX-NV).
  4. mbrown

    mbrown Bit Poster

    27
    0
    2
    Hi Shadowrunner

    Thanks for the code. I receive an error when running the code you supplied. Please see screen capture attached.

    Please let me know.

    Thanks

    Matthew

    - - - Updated - - -

    i added "" to the true and false entries and was able to get it working. However i still receive the email, even if the service is already started.
     

    Attached Files:

  5. Gav

    Gav Kilobyte Poster

    447
    14
    27
    It looks like you're using the wrong variable name. You're taking '$ServiceName' as a parameter, but then looking for a service named '$wuauserv'. Also, take a look at Send-MailMessage as you're currently re-inventing the wheel! :-)
     
  6. mbrown

    mbrown Bit Poster

    27
    0
    2
    I found two vbs scripts (moving away from powershell), one that sends an email, and another that checks if a service has stopped and then prompts a message.

    I managed to merge the two together - i have no experience when it comes to producing code, however i can manipulate the coding to get what i want...sometimes!!. Here it is (not perfectly constructed of course!!)

    But it works!!

    Code:
    strComputer = "."
    dim objShell, objNet
    Set objNet = CreateObject("WScript.NetWork") 
    set objShell = CreateObject("Shell.Application")
    
    Set objWMIService = GetObject("winmgmts:")
    
    strServiceNameToCheck = "spooler"
    
    
    If objShell.IsServiceRunning(strServiceNameToCheck) = false then
    Set objMessage = CreateObject("CDO.Message")
    
    objMessage.Subject = "The print spooler service on ferret has stopped"
    objMessage.Sender = "[email protected]"
    objMessage.To = "[email protected]"
    objMessage.TextBody = "Print Spooler service stopped on server. Please investigate and restart the service."
           
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.server.org.uk"
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = ""
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ""
    
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
    
    'set the timeout to 60 seconds.
    'objMessage.Configuration.Fields.Item _
    '("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    
    objMessage.Configuration.Fields.Update
    objMessage.Send
    
        'Wscript.Echo "Message sent successfully."
    
    Else
        'Wscript.Echo "Enough Disk Space."
    End If 
    
    - - - Updated - - -

    I also managed to get it to send an email instead of a message
     

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.