Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to send an email message with service status info.
Hey, Scripting Guy! I have a number of services on a remote server that I would like to monitor. I would like to see the status of these services and receive these status updates via an email message. I have looked around on the Internet and not found a satisfactory script—everything looks too complicated. Can you help me?
Microsoft Scripting Guy, Ed Wilson, is here. Well, it is sort of cold and rainy down here in Charlotte, North Carolina. But tomorrow is supposed to be a beautiful sunny day. So, at least there is hope. If the weather is good, the Scripting Wife and I are planning on heading out to the Columbia Zoo in Columbia, South Carolina, tomorrow. They have a very nice zoo there, and we always enjoy spending a few hours there. Combine that with lunch out at a nice restaurant, and we end up having a great day.
DB, monitoring the status of your services and sending you an email message does not have to be a zoo, it can be a really simple solution—in fact, it is a single line of code.
To check the status of services on a remote computer, I can use the Get-Service cmdlet (assuming I have rights, and the appropriate ports in the Windows Firewall are open). In fact, it can be really easy. Suppose I want to check on the status of services associated with my SQL Server. In this case, it is really easy because the services begin with MSSQL. Therefore, I can use this bit of information, and do something like the following (gsv is an alias for the Get-Service cmdlet):
gsv -cn sql1 -Name mssql*
One thing to keep in mind is that the Get-Service cmdlet returns an object, or in this example, a series of objects. The ultimate goal is to write the information to an email message—I cannot write an object to an email message. Therefore, I will need to convert the output to a string. Luckily, Windows PowerShell contains the Out-String cmdlet. The code to retrieve SQL service information from a remote server named SQL1 and to return that information as a string is shown here, along with the associated output.
14:57 C:\> gsv -cn sql1 -Name mssql* | out-string
Status Name DisplayName
------ ---- -----------
Running MSSQL$SHAREPOINT SQL Server (SHAREPOINT)
Running MSSQLFDLauncher SQL Full-text Filter Daemon Launche...
Running MSSQLSERVER SQL Server (MSSQLSERVER)
Stopped MSSQLServerADHe... SQL Active Directory Helper Service
Running MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
Windows PowerShell 2.0 introduced a very cool cmdlet—the Send-MailMessage cmdlet. The Send-MailMessage cmdlet permits me to, well, send an email message. One of the great things to do is to create default values for this cmdlet—such as the From field and the SMTPServer field. In Windows PowerShell 3.0 this is easy to do, but for my one-liner today, I will not create default values. The key fields I need to populate are From, To, Subject, Message, and SMTPServer. For the message, I use the command I populated earlier to get the remote service information to a string. The resultant command appears here—keep in mind, it is a single-line command that spans two lines on the blog.
Send-MailMessage -To email@example.com -From "firstname.lastname@example.org" -SmtpServer ex1.contoso.net -Subject "Service Status SQL1" -body ((gsv -cn sql1 -Name mssql*) | out-string)
The email message is shown here.
DB, that is all there is to using Windows PowerShell to retrieve the status of services on your remote server. You can easily plug this sort of one-line command into the Task Scheduler to send you a status message every hour, or as often as you feel you need the status update. I have written quite a bit about using Windows PowerShell and the Task Scheduler. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
This is a great script. One thing I found was that if you are on a domain, the Exchange administrator may need to add your machine as a Relay. Often 'Open Relay' is disabled on mail servers to prevent spammers from hijacking them for their own nefarious purposes.
Hello ED and DB!
My recent blog post I wrote was just about that:
Sorry if I'm trolling!
I like it. Very efficient and very easy to remember so it can be easily typed in any time we need it. It will also fit easily in a task as a one liner or 'command' using the PowerShell New-ScheduledTask.
PS: I am enjoying the new IE10/W8 on screen spell checker. It makes posting much easier.
Hi Guys, great post on the send mail functionality. Ive found that especially exchange 2010 is very fit for powershell usage. I have found a script that gives a good hands on example of the send mail functions, hope this helpes someone like it helped me! Cheers!
Thanks it helped me in sending, some info from server.
I need a power shell script to get the email alerts when site went to stopped satate on IIS 7.0...please help me...thanks