Solved

Powershell script for 2008R2 server to check services

Posted on 2014-10-29
3
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

732 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