Link to home
Start Free TrialLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

Powershell script for 2008R2 server to check services

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")
"@
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimmylew52

ASKER

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 -
My apoligies, I mage an error in your script. It works flawlessly.

Thank you for your assistance.