{"id":3421,"date":"2009-10-30T12:32:59","date_gmt":"2009-10-30T12:32:59","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/10\/30\/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpacks-taskscheduler-module\/"},"modified":"2019-02-18T13:12:23","modified_gmt":"2019-02-18T20:12:23","slug":"sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpacks-taskscheduler-module","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpacks-taskscheduler-module\/","title":{"rendered":"Sending Automated emails with Send-MailMessage, ConvertTo-HTML, and the PowerShellPack&#8217;s TaskScheduler module"},"content":{"rendered":"<p>On October 15th I released a large collection of scripts called the <a href=\"http:\/\/code.msdn.microsoft.com\/PowerShellPack\">PowerShellPack<\/a>.&#160; The PowerShellPack has tons of PowerShell V2 scripts that can be used to do all sorts of fun and practical things.&#160; Today, we\u2019ll show how to use a module in the PowerShell Pack to schedule sending a daily automated email with information about the installed programs on a given system.<\/p>\n<p>Turning the output of a PowerShell script into an automated email is a snap in PowerShell V2, thanks to a nifty new cmdlet called Send-MailMessage.&#160; Send-MailMessage sends out emails with the credentials of the current user (or an arbitrary user) and an SMTP server.&#160; Send-MailMessage can send HTML emails by using the \u2013BodyAsHtml switch, and ConvertTo-HTML can take the output of a cmdlet and turn it into an HTML chunk.&#160; If the email needs some introduction and a signature, you can always use ConvertTo-HTML\u2019s new \u2013PreContent and \u2013PostContent parameters.<\/p>\n<p>Here\u2019s a quick chunk of script that will send the email message once.&#160; Notice that instead of having a very long line with a lot of parameters, I keep the parameters to Send-MailMessage in a Hashtable and use Splatting to provide them to Send-MailMessage<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">            $messageParameters<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">@{<\/span>\n                <span style=\"color: #000000\">Subject<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Installed Program report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())&quot;<\/span>\n                <span style=\"color: #000000\">Body<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Get-WmiObject<\/span> <span style=\"color: #8a2be2\">Win32_Product<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Select-Object<\/span> <span style=\"color: #8a2be2\">Name<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #8a2be2\">Version<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #8a2be2\">Vendor<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Sort-Object<\/span> <span style=\"color: #8a2be2\">Name<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">ConvertTo-Html<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Out-String<\/span>\n                <span style=\"color: #000000\">From<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Me@MyCompany.com&quot;<\/span>\n                <span style=\"color: #000000\">To<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Me@MyCompany.com&quot;<\/span>\n                <span style=\"color: #000000\">SmtpServer<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;SmtpHost&quot;<\/span>\n            <span style=\"color: #000000\">}<\/span>\n            <span style=\"color: #0000ff\">Send-MailMessage<\/span> <span style=\"color: #ff4500\">@messageParameters<\/span> <span style=\"color: #000080\">-BodyAsHtml<\/span>                        <\/pre>\n<p>Now that we&#8217;ve got that chunk of code down, let&#8217;s go ahead and make sure that any problems we encounter get put into a file somewhere.&#160; I can do this by setting one of PowerShell\u2019s magic variables $ErrorActionPreference = \u201cStop\u201d, and adding a try\/catch around all of my code: <\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">        $ErrorActionPreference<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Stop&quot;<\/span>\n        <span style=\"color: #00008b\">try<\/span> <span style=\"color: #000000\">{<\/span>\n            <span style=\"color: #ff4500\">$messageParameters<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">@{<\/span>\n                <span style=\"color: #000000\">Subject<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Installed Program report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())&quot;<\/span>\n                <span style=\"color: #000000\">Body<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Get-WmiObject<\/span> <span style=\"color: #8a2be2\">Win32_Product<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Select-Object<\/span> <span style=\"color: #8a2be2\">Name<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #8a2be2\">Version<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #8a2be2\">Vendor<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Sort-Object<\/span> <span style=\"color: #8a2be2\">Name<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">ConvertTo-Html<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Out-String<\/span>\n                <span style=\"color: #000000\">From<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Me@MyCompany.com&quot;<\/span>\n                <span style=\"color: #000000\">To<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Me@MyCompany.com&quot;<\/span>\n                <span style=\"color: #000000\">SmtpServer<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;SmtpHost&quot;<\/span>\n            <span style=\"color: #000000\">}<\/span>\n            <span style=\"color: #0000ff\">Send-MailMessage<\/span> <span style=\"color: #ff4500\">@messageParameters<\/span> <span style=\"color: #000080\">-BodyAsHtml<\/span>\n        <span style=\"color: #000000\">}<\/span> <span style=\"color: #00008b\">catch<\/span> <span style=\"color: #000000\">{<\/span>\n            <span style=\"color: #ff4500\">$_<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                <span style=\"color: #0000ff\">Out-File<\/span> <span style=\"color: #8a2be2\">$env:TEMP\\ProblemsSendingHotfixReport.log.txt<\/span> <span style=\"color: #000080\">-Append<\/span> <span style=\"color: #000080\">-Width<\/span> <span style=\"color: #800080\">1000<\/span>\n        <span style=\"color: #000000\">}<\/span><\/pre>\n<p>Finally, I\u2019ll just go ahead and use the TaskScheduler module from PowerShellPack to make sure the email arrives every day.&#160; To do this, I\u2019ll need to download the PowerShellPack to the machine I\u2019ll be scheduling the task on.&#160;&#160; Scheduling a task with the PowerShell Pack involves a short pipeline:&#160; Ours will look something like:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #0000ff\">New-Task<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-TaskAction<\/span> <span style=\"color: #000080\">-Script<\/span> <span style=\"color: #000000\">{<\/span>\n        <span style=\"color: #006400\"># Our Emailer Here<\/span>\n    <span style=\"color: #000000\">}<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-TaskTrigger<\/span> <span style=\"color: #000080\">-Daily<\/span> <span style=\"color: #000080\">-At<\/span> <span style=\"color: #8b0000\">&quot;9:00 AM&quot;<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-TaskTrigger<\/span> <span style=\"color: #000080\">-OnRegistration<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Register-ScheduledTask<\/span> <span style=\"color: #8b0000\">&quot;DailyHotfixReport&quot;<\/span>                <\/pre>\n<p>Piece of cake, right?&#160; In our case, we\u2019re sending an email at 9 AM every day (and when the task is registered) with the output of a simple cmdlet, but I could also use this to run a more complex task, and I can play around with the different trigger types to send the emails whenever I\u2019d like.<\/p>\n<p>Here\u2019s the full script:<\/p>\n<p>&#160;<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #0000ff\">New-Task<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-TaskAction<\/span> <span style=\"color: #000080\">-Hidden<\/span> <span style=\"color: #000080\">-Script<\/span> <span style=\"color: #000000\">{<\/span>\n        <span style=\"color: #ff4500\">$ErrorActionPreference<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Stop&quot;<\/span>\n        <span style=\"color: #00008b\">try<\/span> <span style=\"color: #000000\">{<\/span>\n            <span style=\"color: #ff4500\">$messageParameters<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">@{<\/span>\n                <span style=\"color: #000000\">Subject<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Installed Program report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())&quot;<\/span>\n                <span style=\"color: #000000\">Body<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Get-WmiObject<\/span> <span style=\"color: #8a2be2\">Win32_Product<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Select-Object<\/span> <span style=\"color: #8a2be2\">Name<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #8a2be2\">Version<\/span><span style=\"color: #a9a9a9\">,<\/span> <span style=\"color: #8a2be2\">Vendor<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Sort-Object<\/span> <span style=\"color: #8a2be2\">Name<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">ConvertTo-Html<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                    <span style=\"color: #0000ff\">Out-String<\/span>\n                <span style=\"color: #000000\">From<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Me@MyCompany.com&quot;<\/span>\n                <span style=\"color: #000000\">To<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;Me@MyCompany.com&quot;<\/span>\n                <span style=\"color: #000000\">SmtpServer<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #8b0000\">&quot;SmtpHost&quot;<\/span>\n            <span style=\"color: #000000\">}<\/span>\n            <span style=\"color: #0000ff\">Send-MailMessage<\/span> <span style=\"color: #ff4500\">@messageParameters<\/span> <span style=\"color: #000080\">-BodyAsHtml<\/span>\n        <span style=\"color: #000000\">}<\/span> <span style=\"color: #00008b\">catch<\/span> <span style=\"color: #000000\">{<\/span>\n            <span style=\"color: #ff4500\">$_<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n                <span style=\"color: #0000ff\">Out-File<\/span> <span style=\"color: #8a2be2\">$env:TEMP\\ProblemsSendingHotfixReport.log.txt<\/span> <span style=\"color: #000080\">-Append<\/span> <span style=\"color: #000080\">-Width<\/span> <span style=\"color: #800080\">1000<\/span>\n        <span style=\"color: #000000\">}<\/span>\n    <span style=\"color: #000000\">}<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-TaskTrigger<\/span> <span style=\"color: #000080\">-Daily<\/span> <span style=\"color: #000080\">-At<\/span> <span style=\"color: #8b0000\">&quot;9:00 AM&quot;<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-TaskTrigger<\/span> <span style=\"color: #000080\">-OnRegistration<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Register-ScheduledTask<\/span> <span style=\"color: #8b0000\">&quot;DailyHotfixReport&quot;<\/span><\/pre>\n<p>That wasn&#8217;t bad, was it?&#160; It took only 24 lines of PowerShell script to create a schedule task that emails me a fairly detailed report of all of the installed programs on the machine. As you can see, PowerShell V2 makes sending automated emails a snap, and the <a href=\"http:\/\/code.msdn.microsoft.com\/PowerShellPack\">PowerShellPack<\/a> makes scheduling this or any other script on a regular basis a piece of cake.<\/p>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On October 15th I released a large collection of scripts called the PowerShellPack.&#160; The PowerShellPack has tons of PowerShell V2 scripts that can be used to do all sorts of fun and practical things.&#160; Today, we\u2019ll show how to use a module in the PowerShell Pack to schedule sending a daily automated email with information [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[270,275],"class_list":["post-3421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-powershell-v2","tag-powershellpack"],"acf":[],"blog_post_summary":"<p>On October 15th I released a large collection of scripts called the PowerShellPack.&#160; The PowerShellPack has tons of PowerShell V2 scripts that can be used to do all sorts of fun and practical things.&#160; Today, we\u2019ll show how to use a module in the PowerShell Pack to schedule sending a daily automated email with information [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3421"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}