Solved

Powershell script for 2008R2 server to check services

Posted on 2014-10-29
3
209 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
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.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

828 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