Solved

Powershell script for 2008R2 server to check services

Posted on 2014-10-29
3
205 Views
Last Modified: 2014-10-30
I need a script to take server names from a text file and check them for a service that may not be running. Then email me a list of the server names that the service is not running on.

I had one working in 2003 server but it gives many errors I have not been able to solve in 2008r2.

Here is the mess I have been working with, I am very lost. Can someone get me starte on the right path?

$serviceName = "eT Service";

ForEach($Servername in Get-Content C:\MonitorFolder\eTServers.txt){
#$NameStat = @(Get-ChildItem $ServerName | ? {! $_.PsIsContainer})
$serviceStatus = (get-service "$serviceName" -computername $ServerName).Status;
#if ($serviceStatus -ne "Running") {
   # $ServerName | out-file C:\MonitorFolder\eTariffChecked.txt -append
      #$serviceStatus | out-file C:\MonitorFolder\eTariffChecked.txt -append
}#}

   $NameStat = @(Get-ChildItem $serviceStatus | ? {! $_.PsIsContainer})
    Send-MailMessage -From 'ServiceChecker@systrends.com' -To 'me@company.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Results of  Service Check' `
  -Body  @"
      Restart the  service on all the servers that show not running: `n`
             Please Check the service and restart if necessary.
     $(($NameStat | ConvertTo-HTML) -join "`n")
"@
0
Comment
Question by:jimmylew52
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40412057
What errors are you getting?

The following should work for you.
$serviceName = "eT Service";
$results = @()
ForEach($Servername in Get-Content C:\MonitorFolder\eTServers.txt){
$serviceStatus = (get-service "$serviceName" -computername $ServerName).Status;
if ($serviceStatus -ne "Running") {
    $results += "$Servername - $serviceStatus"
}
Send-MailMessage -From 'ServiceChecker@systrends.com' -To 'me@company.com' `
        -SmtpServer 'smtp.company.com' `
        -Subject 'Results of  Service Check' `
        -Body @"
    Restart the  service on all the servers that show not running:
            Please Check the service and restart if necessary.
$($results -join "`n")
"@

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 40413391
Thank You. Sorry to take so long to get back to you, had a network outage.

You script is much closer than mine but still not working. I get all of the server names in the list tested but no results for the service checked.

Server1 -
server2 -
server3 -
server4 -
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 40413404
My apoligies, I mage an error in your script. It works flawlessly.

Thank you for your assistance.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now