Hi Readers,
Sharing a very useful script that can be used with windows servers for daily check list, after maintenance and other activities.
This script will remotely check the service status of sevices that you want to verify if they are running or stopped and report back in
HTML table format as below :- (stopped services are displayed in Red)
example for defining variables–
############################Define Server & Services Variable ###############
$EVServerList = Get-Content “.\EvServer.txt”
$EVServicesList = Get-Content “.\EvServices.txt”
$EVJserverList = Get-Content “.\EvJserver.txt”
$EVJservicesList = Get-Content “.\EvJservices.txt”
$EVDAserverList = Get-Content “.\EvDAserver.txt”
$EVDAservicesList = Get-Content “.\EvDAservices.txt”
#############################Define other variables##########################
$report = “.\report.htm”
$smtphost = “smtp server”
$from = “ServiceStatus@labtest.com”
$to = “vikas.sukhija@labtest.com”
####################################################################
############################################Call Function#########################################
servicestatus $EVServerList $EVServicesList
servicestatus $EVJserverList $EVJservicesList
servicestatus $EVDAserverList $EVDAservicesList
##############################################################################################
download & extract the zip file for script & example..
https://github.com/vikasSukhija/Downloads
you can use this for n number of server/services to be monitored remotely, this can also be scheduled.
Just define server/services as text file as shown in attached zip and then define variables inside attached shell script.
In the end, double click the bat file after that to get the magic in your email.
############################################################################# # Author: Mahesh Sharma # Reviewer: Vikas SUkhija # Date: 06/10/2013 # Modified:06/23/2013 - made it to run from any Location & for any server # Description: Services Monitor ############################################################################# ############################Define Server & Services Variable ############### $EVServerList = Get-Content ".\EvServer.txt" $EVServicesList = Get-Content ".\EvServices.txt" $EVJserverList = Get-Content ".\EvJserver.txt" $EVJservicesList = Get-Content ".\EvJservices.txt" $EVDAserverList = Get-Content ".\EvDAserver.txt" $EVDAservicesList = Get-Content ".\EvDAservices.txt" #############################Define other variables########################## $report = ".\report.htm" $smtphost = "smtp server" $from = "ServiceStatus@labtest.com" $to = "vikas.sukhija@labtest.com" ############################################################################## $checkrep = Test-Path ".\report.htm" If ($checkrep -like "True") { Remove-Item ".\report.htm" } New-Item ".\report.htm" -type file ################################ADD HTML Content############################# Add-Content $report "<html>" Add-Content $report "<head>" Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>" Add-Content $report '<title>Service Status Report</title>' add-content $report '<STYLE TYPE="text/css">' add-content $report "<!--" add-content $report "td {" add-content $report "font-family: Tahoma;" add-content $report "font-size: 11px;" add-content $report "border-top: 1px solid #999999;" add-content $report "border-right: 1px solid #999999;" add-content $report "border-bottom: 1px solid #999999;" add-content $report "border-left: 1px solid #999999;" add-content $report "padding-top: 0px;" add-content $report "padding-right: 0px;" add-content $report "padding-bottom: 0px;" add-content $report "padding-left: 0px;" add-content $report "}" add-content $report "body {" add-content $report "margin-left: 5px;" add-content $report "margin-top: 5px;" add-content $report "margin-right: 0px;" add-content $report "margin-bottom: 10px;" add-content $report "" add-content $report "table {" add-content $report "border: thin solid #000000;" add-content $report "}" add-content $report "-->" add-content $report "</style>" Add-Content $report "</head>" Add-Content $report "<body>" add-content $report "<table width='100%'>" add-content $report "<tr bgcolor='Lavender'>" add-content $report "<td colspan='7' height='25' align='center'>" add-content $report "<font face='tahoma' color='#003399' size='4'><strong>Service Status Report</strong></font>" add-content $report "</td>" add-content $report "</tr>" add-content $report "</table>" add-content $report "<table width='100%'>" Add-Content $report "<tr bgcolor='IndianRed'>" Add-Content $report "<td width='10%' align='center'><B>Server Name</B></td>" Add-Content $report "<td width='50%' align='center'><B>Service Name</B></td>" Add-Content $report "<td width='10%' align='center'><B>Status</B></td>" Add-Content $report "</tr>" ######################################################################################################## ################################## Get Services Status ################################################# Function servicestatus ($serverlist, $serviceslist) { foreach ($machineName in $serverlist) { foreach ($service in $serviceslist) { $serviceStatus = get-service -ComputerName $machineName -Name $service if ($serviceStatus.status -eq "Running") { Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green $svcName = $serviceStatus.name $svcState = $serviceStatus.status Add-Content $report "<tr>" Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $machineName</B></td>" Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$svcName</B></td>" Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>" Add-Content $report "</tr>" } else { Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Red $svcName = $serviceStatus.name $svcState = $serviceStatus.status Add-Content $report "<tr>" Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>" Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>" Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>" Add-Content $report "</tr>" } } } } ############################################Call Function############################################# servicestatus $EVServerList $EVServicesList servicestatus $EVJserverList $EVJservicesList servicestatus $EVDAserverList $EVDAservicesList ############################################Close HTMl Tables######################################### Add-content $report "</table>" Add-Content $report "</body>" Add-Content $report "</html>" ##################################################################################################### #############################################Send Email############################################## $subject = "Daily Service Monitor" $body = Get-Content ".\report.htm" $smtp= New-Object System.Net.Mail.SmtpClient $smtphost $msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body $msg.isBodyhtml = $true $smtp.send($msg) #####################################################################################################
Please rate the script if you found it to be useful.
http://gallery.technet.microsoft.com/scriptcenter/Monitor-Report-5d1998d4
Regards
Sukhija Vikas
Hi,
I want to thank you because I have been searching a lot about how to check services in multiples machines. I’m using the code to test my programs. Thank you!!
thanks for this. i used it as the base for a ‘service down alert’ emailer. my only question was the multiple server and services (3) lists and what their difference was. The variable naming and code comments didn’t make it clear. I ended up just ripping out two of them.
you can call multiple servers & different services by defining them as different variables and than calling the function in the end.
servicestatus $EVServerList $EVServicesList
servicestatus $EVJserverList $EVJservicesList
servicestatus $EVDAserverList $EVDAservicesList
Is it possible to send emails if there are stopped services only?
yes, but it needs modification in if condition..It was made to send email in html & turn the services as red that are stopped.
Can you please send me the modifications because i want it only to send me emails when the services are stopped. Thank you
I’m able to check for stopped services only but if there are no stopped services the script still sending emphty report email!!
It send all running services marked as green & the one not running marked as red.
You are saying that you need a condition that it should only send if there is something stopped ?
Yes exactly, i need it to send email if there are stopped process only. I hope you can help me on this.
Ali, I was away so was not able to reply you, but you just have to change the if logic & than you will have what you wanted from this script. but this script was designed for multiple servers & daily checklist so that it reports in green /red & action can be taken accordingly.
This is very good script and so useful
how can we have a display name different in the report. For exam: server1 should show server-function in the report. Is it possible?
Please let me know is it possible to have display name changed in the report.
script is picking server name from text file..I didn’t got you about what do you mean by server1 should show server-function..
Hi Vikas,
Great Script, Do you have a similar version using Get-WmiObject OR Get-CimInstance. this way we can also get StartMode and check all services on multiple servers.
Many Thanks in Advace,
Hi Vikas,
Great Script, do you have similar version using “Get-WmiObject” or “Get-CimInstance” so we can get more information about services on multiple machine,
Thanks.
I have one more that sets startup mode etc..but doesn’t use get-wmi..
http://msexchange.me/2015/11/28/bulk-start-stop-disable-of-windows-services/
I am getting the below error when I run the script.
Exception calling “Send” with “1” argument(s): “Failure sending mail.”
At C:\CheckServiceStatus.ps1:162 char:1
+ $smtp.send($msg)
I am getting the below error. Please help
Exception calling “Send” with “1” argument(s): “Failure sending mail.”
At C:\CheckServiceStatus.ps1:162 char:1
+ $smtp.send($msg)
I am getting this error now.
Get-Service : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Supply an argument that is not null or empty and t
hen try the command again.
what version of powershell are you using ??
Powershell 2.0 & 3.0 I tried in both of them
it should work on both versions, either the text file are not created & function call is not happening..
$EVServerList = Get-Content “.\EvServer.txt”
$EVServicesList = Get-Content “.\EvServices.txt”
$EVJserverList = Get-Content “.\EvJserver.txt”
$EVJservicesList = Get-Content “.\EvJservices.txt”
$EVDAserverList = Get-Content “.\EvDAserver.txt”
$EVDAservicesList = Get-Content “.\EvDAservices.txt”
——
servicestatus $EVServerList $EVServicesList
servicestatus $EVJserverList $EVJservicesList
servicestatus $EVDAserverList $EVDAservicesList
Thanks Vikas. I just realized typo in the Function call. Now its all working. Thanks for your prompt response. Highly appreciated
Hi Vikas, Scripts good and easy to amend to email out. How easy would this be to add iisapppool checks and MSMQ and drive space to so it reports a bigger return. I am just starting to learn powershell scripting
yes those can be added by customizing this script, use the HTML pattern inside the script & relevant info that can be updated..
Hi Vikas, I have had the script working for multiple servers now and it is helping me immensly, but would like a bit of help, how in the script, could I add say, Listing the harddrives, sizes and free space on the servers from the serverlist i.e. where and how would I add it.
first you need to decide if you want to create a separate section for it & than lot of code needs to be added for HTML/for finding the things that you are interested.
Hi Vikas, decided to make serveral scripts and have a few reports for each set. I am now trying to create a script to email if it sees one of the essential services stopped. and report that service has stopped. I do not want it restarted, as I want to investigate the stop.
In new Year, ones I get time , I will include drive pace as on this report..
Hi Vikas, added the below to you service script, to email external:
########### SMTP SERVER AND MAIL SETTINGS #############
#### Working email settings for external exchange. Add this below SMTP in vikas csript####
$smtpServer = “smtp.Something.net”
$smtpPort = “port number, can get from internet e.g 587 gmail” #It depends according to your smtp server
###$sslEnabled= $true #If your smtp server does not support secure connections,
#you have to delete this line
$username = “Email account of sending account”
$password = “” #Type your password here
$from = “Email address for exchange”
$to = “recipeint email address”
$subject = “service status report”
$body = “The Server services report is attached”
########Then at the bottom of the script add this#####
############### E-MAIL SETTINGS ###############
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
####$smtp.sslEnabled = $sslEnabled #If your smtp server does not support secure
#connections, you have to delete this line
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
$msg = new-object Net.Mail.MailMessage
$msg.From = $from
$msg.To.Add($to)
$msg.Subject = $subject
$msg.Body = $body
$attachFile = “F:\Status Reports\report.html”
$att = new-object Net.Mail.Attachment($attachFile)
$msg.Attachments.Add($att)
$smtp.Send($msg)
########################################################################
Hello Vikas,
Your Check Services Script is really great and trying to add another line that will check the status of the Start up type of the service (Automatic, Manual or Disabled. I’ve been trying to insert this command from your CheckServices script but i cant make it work.
(Get-WmiObject Win32_Service -filter “Name=’W32Time'”).StartMode
that can have this kind of output:
Your help will really appreciated.
Thanks,
check using get-service (example)
Get-Service Retaildemo | select -property name,starttype
Hello Vikas, Thank you..
I’m trying to insert it to your Check Services Script but i cant make it work. I’m not sure where i supposed to put it. Can you give me a tip on it? Really appreciated your time on this.
Hello Vikas,
Its already working on mine, i added conditions in the IF Else line and made it work. Thank you very much your scripts is great and easy to follow though im just new.
Thank you very much
Jonathan