{"id":4451,"date":"2012-12-19T00:01:00","date_gmt":"2012-12-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/12\/19\/send-an-email-message-with-service-status-via-powershell\/"},"modified":"2012-12-19T00:01:00","modified_gmt":"2012-12-19T00:01:00","slug":"send-an-email-message-with-service-status-via-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/send-an-email-message-with-service-status-via-powershell\/","title":{"rendered":"Send an Email Message with Service Status Via PowerShell"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to send an email message with service status info.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;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&mdash;everything looks too complicated. Can you help me?<\/p>\n<p>&mdash;DB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello DB,<\/p>\n<p>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.<\/p>\n<p>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&mdash;in fact, it is a single line of code.<\/p>\n<h2>Checking the status of services on a remote server<\/h2>\n<p>To check the status of services on a remote computer, I can use the <strong>Get-Service<\/strong> 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 (<strong>gsv<\/strong> is an alias for the <strong>Get-Service<\/strong> cmdlet):<\/p>\n<p style=\"padding-left: 30px\">gsv -cn sql1 -Name mssql*<\/p>\n<p>One thing to keep in mind is that the <strong>Get-Service<\/strong> cmdlet returns an object, or in this example, a series of objects. The ultimate goal is to write the information to an email message&mdash;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 <strong>Out-String<\/strong> 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.<\/p>\n<p style=\"padding-left: 30px\">14:57 C:\\&gt; gsv -cn sql1 -Name mssql* | out-string<\/p>\n<p style=\"padding-left: 30px\">Status&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DisplayName<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp; MSSQL$SHAREPOINT&nbsp;&nbsp; SQL Server (SHAREPOINT)<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp; MSSQLFDLauncher&nbsp;&nbsp;&nbsp; SQL Full-text Filter Daemon Launche&#8230;<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp; MSSQLSERVER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SQL Server (MSSQLSERVER)<\/p>\n<p style=\"padding-left: 30px\">Stopped&nbsp; MSSQLServerADHe&#8230; SQL Active Directory Helper Service<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp; MSSQLServerOLAP&#8230; SQL Server Analysis Services (MSSQL&#8230;<\/p>\n<h2>Sending an email message<\/h2>\n<p>Windows PowerShell&nbsp;2.0 introduced a very cool cmdlet&mdash;the <strong>Send-MailMessage<\/strong> cmdlet. The <strong>Send-MailMessage<\/strong> cmdlet permits me to, well, send an email message. One of the great things to do is to create default values for this cmdlet&mdash;such as the From field and the SMTPServer field. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/12\/03\/use-powershell-default-parameter-values-to-simplify-scripts.aspx\" target=\"_blank\">In Windows PowerShell 3.0 this is easy to do<\/a>, 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&mdash;keep in mind, it is a single-line command that spans two lines on the blog.<\/p>\n<p style=\"padding-left: 30px\">Send-MailMessage -To edwilson@contoso.net -From &#8220;ed@iammred.net&#8221; -SmtpServer ex1.contoso.net -Subject &#8220;Service Status SQL1&#8221; -body ((gsv -cn sql1 -Name mssql*) | out-string)<\/p>\n<p>The email message is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7776.hsg-12-19-12-01.png\"><img decoding=\"async\" title=\"Image of email message\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7776.hsg-12-19-12-01.png\" alt=\"Image of email message\" \/><\/a><\/p>\n<p>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. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scheduled+tasks\/windows+powershell\/\" target=\"_blank\">I have written quite a bit about using Windows PowerShell and the Task Scheduler<\/a>. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to send an email message with service status info. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[357,31,3,182,39,45],"class_list":["post-4451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-messaging-communication","tag-operating-system","tag-scripting-guy","tag-sending-email","tag-services","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to send an email message with service status info. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4451"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4451\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}