Asked by:
Get service status email from more than one computer

Question
-
Morning all,
I am trying to use this script to check service status email from more than one computer.
I have created two text files for computer and service name, would like to receive like computer name, service name, status email.
$computers = Get-Content "C:\Users\xyz\Desktop\Checkservices\Computers.txt" foreach ($line in $computers) { Invoke-Command -ComputerName $line -ScriptBlock{ $services = Get-Content "C:\Users\xyz\Desktop\Checkservices\services.txt" ##$serviceName = "Tomcat123" foreach ($serviceName in $services) { Invoke-Command -ServiceName $serviceName -ScriptBlock{ $service = Get-Service | Where-Object { $_.Name -eq $serviceName } write-host "Service Status is" $service.status if ($service.status -eq "Running") { write-host "Sending waring email" $smtpServer = "pass smtp server" $msg = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient($smtpServer) $dateString = get-date $msg.From = "pass email id" $msg.To.Add("pass email id") $msg.subject = "Service " + $service + " is " + $service.status $msg.body = ([string]$dateString) + " Service " + $service + " is " + $service.status $smtp.send($msg) } } } } }
Added URL
- Edited by SQL_VJ Thursday, February 13, 2020 4:10 PM
Thursday, February 13, 2020 4:07 PM
All replies
-
We do not customize code you have copied from teh Internet. Please ask the author of the code to help you change it.
Please carefully review the following links to set your expectation for posting in technical forums.
- This Forum is for Scripting Questions Rather than script requests
- How to ask questions in a technical forum
- How to post code in Technet Forums
\_(ツ)_/
Thursday, February 13, 2020 4:18 PM -
This is how to write this in PowerShell.
$services = Get-Content C:\Users\xyz\Desktop\Checkservices\services.txt Get-Content C:\Users\xyz\Desktop\Checkservices\Computers.txt | ForEach-Object{ Get-Service -ServiceName $services -ComputerName $_ | ForEach-Object{ write-host 'Service Status is' $_.status if ($_.status -eq 'Running'){ write-host 'Sending waring email' $msg = "$(get-date) Service $_ is $($_.status)" $mailprops = @{ From = 'pass email id' To = 'to email address' Subject = $msg Body = $msg SmtpServer = 'pass smtp server' } Send-MailMessage @mailprops } } }
\_(ツ)_/
Thursday, February 13, 2020 4:33 PM -
Thanks Jrv. Yes, I have copied from Git. New to power shell, would like to learn, not sure where do I start it.
Getting this error: Get-Service : Cannot find any service with service name ''.
Is there a way to get like 'computer name, service name, status by email'
Computer Service Status Computer1 WinRM Running Thursday, February 13, 2020 5:14 PM -
Just add the name to the message. Why is that a problem?
\_(ツ)_/
Thursday, February 13, 2020 5:16 PM -
Not sure, I am getting the following error. Still receiving an email - 02/13/2020 17:31:44 Service System.ServiceProcess.ServiceController is Running.
Just to add - the script is calling from local server to remote server. Could be permission issue.
======
Get-Service : Cannot find any service with service name 'Tomcat1'.
At C:\Users\xyz\Desktop\Checkservices\Get_service_New.ps1:4 char:9
+ Get-Service -ServiceName $services -ComputerName $_ |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Tomcat1:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Get-Service : Cannot find any service with service name 'Tomcat2'.
At C:\Users\xyz\Desktop\Checkservices\Get_service_New.ps1:4 char:9
+ Get-Service -ServiceName $services -ComputerName $_ |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Tomcat2:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand- Edited by SQL_VJ Thursday, February 13, 2020 5:42 PM
Thursday, February 13, 2020 5:36 PM -
Get-Service -ServiceName $services -ComputerName $_ -ErrorAction 0|
\_(ツ)_/
Thursday, February 13, 2020 5:42 PM -
Thanks Jrv.
I just run it on remote server itself, got the following, but the email is not looking right.
email: 02/13/2020 17:48:01 Service System.ServiceProcess.ServiceController is Running
It should be Service TomCat1 is running, but the the way i need it Service TomCat1 running on computer1.
Service Status is Running
Sending email
Also, this line should not check the status, regardless of the status it should check and return the results.
if ($_.status -eq 'Running'){
Thursday, February 13, 2020 5:52 PM -
As I noted above, we will not redesign or rewrite a script that you have copied from the Internet. I posted code that does what the original script appears to do. If it doesn't do what you want then you will have to learn PowerShell and write the script the way you need it to be.
Learning PowerShell and Windows technology is your responsibility. This forum is not a place to ask for others to teach you and we certainly cannot understand what you want.
If you are not capable of learning the technology then you will have to hire a consultant to write your script.
Please carefully review the following links to set your expectation for posting in technical forums.
- This Forum is for Scripting Questions Rather than script requests
- How to ask questions in a technical forum
- How to post code in Technet Forums
\_(ツ)_/
Thursday, February 13, 2020 6:05 PM -
This is how someone trained in computer engineering and with experience using and automating Windows management tasks would do this:
$services = Get-Content C:\Users\xyz\Desktop\Checkservices\services.txt $body = Get-Content C:\Users\xyz\Desktop\Checkservices\Computers.txt | ForEach-Object{Get-Service -ServiceName $services -ComputerName $_} | Where-Object{$_.Status -ne 'Running'} | Select-Object MachineName,ServiceName,Status | ConvertTo-Html | Out-String $mailprops = @{ From = 'pass email id' To = 'to email address' Subject = "$(get-date) Service Status" Body = $body BodyAsHtml = $true SmtpServer = 'pass smtp server' } Send-MailMessage @mailprops
\_(ツ)_/
Thursday, February 13, 2020 6:12 PM -
Thanks for your help so far. Agree with you, I am beginner into PS world, will start with some book or videos.
Thank you.
Friday, February 14, 2020 10:39 AM -
Just added error handling, since the service not available in other servers.
I am going to try next test case to check some of service in a group of computer and output that into csv. Like, the service is available, startup, owner of service, status of it etc. Hoping to learn more :-)
try { $services = Get-Content C:\Users\xyz\Desktop\Checkservices\services.txt $body = Get-Content C:\Users\xyz\Desktop\Checkservices\Computers.txt | ForEach-Object{Get-Service -ServiceName $services -ComputerName $_ -ErrorAction SilentlyContinue} | #Where-Object{$_.Status -ne 'Running'} | Select-Object MachineName,ServiceName,Status | ConvertTo-Html | Out-String } catch { $_ | Out-File C:\Users\xyz\Desktop\Checkservices\errors.txt -Append } $mailprops = @{ From = 'pass email id' To = 'to email address' Subject = "$(get-date) Service Status" Body = $body BodyAsHtml = $true SmtpServer = 'pass smtp server' } Send-MailMessage @mailprops
Friday, February 14, 2020 3:28 PM -
That won't work. Use 'Stop' to make error handling work correctly.
Start by reading the help for each new thing you need to use.
\_(ツ)_/
Friday, February 14, 2020 3:34 PM -
I have tested with and without error handling, it is working for both cases and i am getting an email as well.
Yes, i am starting get-help -Detailed
Friday, February 14, 2020 5:32 PM -
I just checked the get-help but i could not find how many columns / property 'Select-Object' or 'Get-Service' has it. It does not give 'MachineName,ServiceName,Status'. How do i get all the columns for a particular object or cmdlet.
Select-Object MachineName,ServiceName,Status
Monday, February 17, 2020 12:27 PM -
Got it - Get-Service | Get-memberMonday, February 17, 2020 1:23 PM
-
I just checked the get-help but i could not find how many columns / property 'Select-Object' or 'Get-Service' has it. It does not give 'MachineName,ServiceName,Status'. How do i get all the columns for a particular object or cmdlet.
Select-Object MachineName,ServiceName,Status
help get-member -online\_(ツ)_/
Monday, February 17, 2020 3:29 PM