?
Solved

Powershell script for 2008R2 server to check services

Posted on 2014-10-29
3
Medium Priority
?
214 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 2000 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Suggested Courses

777 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