none
A complete noob strying to learn. Need to come up with a script for work.

    Question

  • I need to come up with a script that checks to see if three particular services are running on a server and emails an external address if they've stopped. It doesn't need to be real time just to check on a scheduled task. Any help would be apprecieted.
    Thursday, April 19, 2012 8:01 AM

Answers

All replies

  • Hello Matt,

    The scripting gallery is a great place to start, you can find it here:

    Technet Gallery 

    Here is a search query for scripts that can send email:

    http://gallery.technet.microsoft.com/site/search?query=send%20email&f%5B0%5D.Value=send%20email&f%5B0%5D.Type=SearchText&ac=8 

    Thursday, April 19, 2012 8:27 AM
  • Here is a very, very simple batch file solution:

    @echo off
    net start | find /i "Service 1" || goto :Fail
    net start | find /i "Service 2" || goto :Fail
    net start | find /i "Service 3" && goto :eof

    :Fail
    c:\Tools\blat.exe /.. /..

    Blat.exe is a command line mailer that you can download from a number of sites. You need to experiment with it in order to set a number of switches for sender, recipient, message, attachment, SMTP server etc.

    Thursday, April 19, 2012 8:29 AM
  • I need to come up with a script that checks to see if three particular services are running on a server and emails an external address if they've stopped. It doesn't need to be real time just to check on a scheduled task. Any help would be apprecieted.

    Each serveice has a set of events that you can define.  They can restart teh service or send a message.  Tisis how we get messages sent of pages sent when a service quits.  Just define the action for the service and it will notify you immediately on failures.

    These can be set by Group Policy.


    ¯\_(ツ)_/¯

    Thursday, April 19, 2012 9:29 AM
  • I need to come up with a script that checks to see if three particular services are running on a server and emails an external address if they've stopped. It doesn't need to be real time just to check on a scheduled task. Any help would be apprecieted.

    Each serveice has a set of events that you can define.  They can restart teh service or send a message.  Tisis how we get messages sent of pages sent when a service quits.  Just define the action for the service and it will notify you immediately on failures.

    These can be set by Group Policy.


    ¯\_(ツ)_/¯

    Hi jrv,

    Where do I define these events?

    Thursday, April 19, 2012 12:13 PM
  • Hi Guys, thanks a lot for your help so far.

    I tried cobbling this together:

    $ServiceResult=get-service -computername server -name *peg*
    send-mailmessage -from administrator@domain.local -to technical@quorum-business.co.uk  -subject  "Pegasus Services" -body "These are the current pegasus services $ServiceResult" -smtpserver exchange-svr  

    I get sent an email but all I get is:

    These are the current pegasus services System.ServiceProcess.ServiceController System.ServiceProcess.ServiceController System.ServiceProcess.ServiceController

    I'm confused.

    Thursday, April 19, 2012 2:06 PM
  • Where do I define these events?

    The 'recovery' tab of the service contol panel or as faolure options of SC:

    At a command prompt:

    sc failure * -?

    DESCRIPTION:
            Changes the actions upon failure
    USAGE:
            sc <server> failure [service name] <option1> <option2>...
    OPTIONS:
            reset= <Length of period of no failures (in seconds)
                    after which to reset the failure count to 0 (may be INFINITE)>
                    (Must be used in conjunction with actions= )
            reboot= <Message broadcast before rebooting on failure>
            command= <Command line to be run on failure>
            actions= <Failure actions and their delay time (in milliseconds),
                      separated by / (forward slash) -- e.g., run/5000/reboot/800
                      Valid actions are <run|restart|reboot>  >
                      (Must be used in conjunction with the reset= option)

    The command line can be a script or just a call to a simple mailer with parameters.


    ¯\_(ツ)_/¯

    Thursday, April 19, 2012 2:24 PM
  • Where do I define these events?

    The 'recovery' tab of the service contol panel or as faolure options of SC:

    At a command prompt:

    sc failure * -?

    DESCRIPTION:
            Changes the actions upon failure
    USAGE:
            sc <server> failure [service name] <option1> <option2>...
    OPTIONS:
            reset= <Length of period of no failures (in seconds)
                    after which to reset the failure count to 0 (may be INFINITE)>
                    (Must be used in conjunction with actions= )
            reboot= <Message broadcast before rebooting on failure>
            command= <Command line to be run on failure>
            actions= <Failure actions and their delay time (in milliseconds),
                      separated by / (forward slash) -- e.g., run/5000/reboot/800
                      Valid actions are <run|restart|reboot>  >
                      (Must be used in conjunction with the reset= option)

    The command line can be a script or just a call to a simple mailer with parameters.


    ¯\_(ツ)_/¯

    Hi,

    Sorry I was being an eejit. I was looking for the 'send a message' option in service manager. This would also mean donwloading a 3rd party mailer etc. It would be handy to know how to email the results of the check within the powershell umbrella, for future reference / learning too.

    Thursday, April 19, 2012 2:32 PM
  • No - you can call vbscript mailer or a PowerShell (preferred) mailer.

    If you are on WS2008 or lat3er you can just add an event to the eventlog which can directly mail a message whenver the event occurs.  ALl service insert an event record whan they are stopped.  WS2008 has an event log task that sends email.

    The problem with your script is that it has to be kept running all of the time in a loop.  Thisis very unrelaiable.  The Windows design is to crate events when important things happen.  The 'REcovery' options are those important events for pre-WS2008 systems.  For WS2008 (actually Vista) and later it is more useful to use Eventlog tasks as they can be more fine grained and usually require no scripting.

    The Recoverty method allows us to add arguments to the commandline which can be used in the script. On script can be parked on any number of services and the name of the service sent on the commandline.  The servr name, time and othe items can be derived in the called script.  On script can now do many things.


    ¯\_(ツ)_/¯

    Thursday, April 19, 2012 2:45 PM
  • Hi, I was just going to have it scheduled to run daily. The user only really needs to know on a daily basis. Unfortunately this is on a 2003 R2 box otherwise I guess my options would be legion. I noticed I can 'run a program' if the service stops; could I launch a PS script from there? The other complication is that the services shut down deliberately to run a routine over night so even if I did get this working the user would get a nightly email telling him what he already knows. I was just going to schedule this to run around 9am to check the service started again.
    Thursday, April 19, 2012 2:52 PM
  • Recovery events do not fire if teh srvice is shut down manually.  They only fire if teh service aborts with an error.

    You can run a script like this:

    PowerShell -file <path>scriptfile.ps1 -arguments "servicename"


    ¯\_(ツ)_/¯

    Thursday, April 19, 2012 3:05 PM
  • The guy I'm dealing with says he would feel 'reasurred' to see aspelling out the status of the particular services on a daily basis. I've come up with this

    $ServiceResult=get-service -computername server -name *peg*
    $serviceStatus= $ServiceResult | fl | out-string

    send-mailmessage -from administrator@design-go.com -to support@quorum-business.co.uk  -subject  "Pegasus Services" -body $ServiceStatus -smtpserver exchange-svr  

     It runs and works but there is a bit of info I dont need:

    Name                : PegasusMessageService

    DisplayName         : Pegasus Message Service

    Status              : Running

    DependentServices   : {}

    ServicesDependedOn  : {}

    CanPauseAndContinue : False

    CanShutdown         : False

    CanStop             : True

    ServiceType         : Win32OwnProcess

    All I need is the display name, status, and dependent services. How can I filter the others out?

    Thursday, April 19, 2012 3:55 PM
  • The guy I'm dealing with says he would feel 'reasurred' to see aspelling out the status of the particular services on a daily basis. I've come up with this

    $ServiceResult=get-service -computername server -name *peg*
    $serviceStatus= $ServiceResult | fl | out-string

    send-mailmessage -from administrator@design-go.com -to support@quorum-business.co.uk  -subject  "Pegasus Services" -body $ServiceStatus -smtpserver exchange-svr  

     It runs and works but there is a bit of info I dont need:

    Name                : PegasusMessageService

    DisplayName         : Pegasus Message Service

    Status              : Running

    DependentServices   : {}

    ServicesDependedOn  : {}

    CanPauseAndContinue : False

    CanShutdown         : False

    CanStop             : True

    ServiceType         : Win32OwnProcess

    All I need is the display name, status, and dependent services. How can I filter the others out?

    HELP FL -FULL


    ¯\_(ツ)_/¯

    Thursday, April 19, 2012 4:18 PM
  • All very good points, jv.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, April 19, 2012 4:35 PM
  • $ServiceResult=get-service -computername server -name *peg*
    $serviceStatus= $ServiceResult | select name, status, dependentservices | out-string 
    send-mailmessage -from administrator@design-go.com -to support@quorum-business.co.uk  -subject  "Pegasus Services" -body $ServiceStatus -smtpserver exchange-svr   


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, April 19, 2012 4:37 PM
  • To set something up what you are attempting have a look at the following:

    • Open the services console, type services.msc in the run box
    • Go to the Recovery tab
    • Set a program to start on the first failure
    • Make sure the program does two things: Start the service again and send an email.

    The program you start could send the email for you.

    Friday, April 20, 2012 8:28 AM