Schedule restart of service & email

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Richard Siddaway Richard Siddaway 3 years, 8 months ago.

  • Author
    Posts
  • #9877
    Profile photo of Jake Sully
    Jake Sully
    Participant

    I am trying to get this script to work on a windows 2008 R2 server to stop a service for example "spooler", then start the service.
    I want to get an email only if the task fails, but right now for testing I'm trying to get it to send an email when the task runs. So far I can't get the task to run at all, the service doesn't stop, I am not sure where I should be putting the variable for the service name, and then for the email, I thought there was an easier way to do this, but I tried and couldn't get it to work, so I copied the email portion from another script I found online, but the email doesn't work either.
    ## Reference to System.Net for email alerts
    [void][System.Reflection.Assembly]::LoadWithPartialName(“System.Net.Mail”)
    function restart-service
    {
    param ($srvName)
    ## stop service
    stop-Service $srvName
    $service = get-service $srvName
    if($service.status -ne “Stopped”)
    {
    email-admin($srvName + ” has failed to stop, please manually stop and restart.”)
    write($srvName + ” has failed to stop, please manually start.”)
    }
    ##start service
    start-Service $srvname
    $service = get-service $srvName
    if($service.status -ne “Running”)
    {
    email-admin($srvName + ” has failed to start, please manually start.”)
    write($srvName + ” has failed to start, please manually start.”)
    }
    else
    {
    write($srvName + ” has started.”)
    }
    }
    ##email if failed
    function email-admin($body)
    {
    $mail = New-Object System.Net.Mail.MailMessage
    $mail.From = “psScript@server”
    $mail.To.Add(“your@email.com”)
    $mail.Subject = “Service Restart”
    $mail.Body = $body
    $smtp = New-Object System.Net.Mail.SmtpClient(“your.smtp.com”)
    $smtp.Send($mail)
    }
    restart-service BITS
    [/ps]
    I put this into the task scheduler:
    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -nolog -command “&{c:\Scripts\restartService.ps1}”

  • #9878
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    I suspect that part of your problem is that your code isn't waiting for the service to stop before testing if it has stopped

    for testing purposes I'd break this down into a number of sections. I'd start with the code to stop and start the service. You can use something like this:


    $srvname = "BITS"

    if ( (Get-Service -Name $srvname).Status -eq 'Running'){
    Stop-Service -Name $srvname
    }
    else {
    ## may want to send email to this effect
    Write-Warning "$srvname service NOT running"

    ## use a break if you want to stop here otherwise comment out
    break
    }

    while($true){
    if ( (Get-Service -Name $srvname).Status -eq 'Stopped'){
    ## replace Write-Host by email function
    Write-Host "$srvname STOPPED"

    #this next line is just for testing - remove in production
    Get-Service -Name $srvname
    break
    }
    }
    Start-Service -Name $srvname
    while($true){
    if ( (Get-Service -Name $srvname).Status -eq 'Running'){
    ## replace Write-Host by email function
    Write-Host "$srvname Running"
    break
    }
    }

    Some services can't be stopped which I haven't handled here – check the CanStop property returned by get-service

    Once you have the service management section sorted you can test email part. You're using Windows 2008R2 which has PowerShell 2.0 which has a send-mailmessage cmdlet. Check that out rather than writing your own function.

    The final part of the problem is that you are running this as a scheduled task. Make sure that the task runs under a security context that can stop/start services & send email. The task needs to be set to run without the user logged on if you want it to be running unattended

    Hope this helps – please bring any further questions back to this thread

You must be logged in to reply to this topic.