{"id":1920,"date":"2024-11-04T14:48:37","date_gmt":"2024-11-04T20:48:37","guid":{"rendered":"https:\/\/pelnar.tech\/?p=1920"},"modified":"2024-11-04T14:48:37","modified_gmt":"2024-11-04T20:48:37","slug":"powershell-email","status":"publish","type":"post","link":"https:\/\/pelnar.tech\/?p=1920","title":{"rendered":"Powershell: Email"},"content":{"rendered":"\n<p>I use a lot of Powershell scripts, many of which are executed by a scheduled task on many different servers. I want to get some feedback from the script so I know it ran correctly, or at least so I can maybe determine when something <em>stopped <\/em>working correctly. <\/p>\n\n\n\n<p>I&#8217;ve begun adding simple email components to the scripts so I can get basic emails sent when the script runs. Here&#8217;s the components:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$PSEmailServer = \"EmailServerName\" #or IP works too\n$EmailTo = \"email@domain.com\"\n$EmailFrom = \"UserRunningScript@Server.com\" #See explanation below\n$EmailSubject = \"ScriptName successfully run on SeverName\"\n$EmailBody = \"\" #Whatever you want there\n\nSend-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body \"$EmailBody but you can add other things here.\"\n<\/code><\/pre>\n\n\n\n<p>In my case, I&#8217;m using an on-prem exchange server, so I don&#8217;t need the &#8220;from&#8221; email to be real, and I don&#8217;t need any other credentials. So for me, I put the username that runs the scheduled task as the username, and I use the server name as the domain. There&#8217;s plenty of ways to do this, but what I like about this over something like &#8220;do-not-reply@domain.com&#8221; is that I can put email rules to work to sort these messages. <\/p>\n\n\n\n<p>For the Body, other things I like to include are other variables from the script that might change over time. For instance, a common script for me might delete files over a certain age. The script will have a $CutOffDate so I might put that in the body. For instance the body might be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$EmailBody = \"Files were deleted if they were modified before \"<\/code><\/pre>\n\n\n\n<p>And then in the command I might put:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body \"$EmailBody $CutoffDate\"<\/code><\/pre>\n\n\n\n<p>Something I&#8217;d like to do that I haven&#8217;t figured out yet is a clean way to list out all items stored in a variable. Again, using the example of a script that deletes old files, I might have a variable of $files that was used to delete all of the files. In the email, I&#8217;d think it&#8217;d be easy enough to list out those files by just including the variable in the body, but I found it mashes them all together with no spaces or new lines. So I need to figure out a better way to do that. <\/p>\n\n\n\n<p>Speaking of new lines, adding a new line to the body can be an easy way to create a basic table of sorts by just adding the new line character `n to the body.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$EmailBody = \"Server:Server1 `nDate:$Date `nPath:C:\\Path\\Folder\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I use a lot of Powershell scripts, many of which are executed by a scheduled task on many different servers. I want to get some feedback from the script so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1920","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false,"newspaperly-grid":false,"newspaperly-slider":false,"newspaperly-small":false},"uagb_author_info":{"display_name":"admin","author_link":"https:\/\/pelnar.tech\/?author=1"},"uagb_comment_info":4,"uagb_excerpt":"I use a lot of Powershell scripts, many of which are executed by a scheduled task on many different servers. I want to get some feedback from the script so...","_links":{"self":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts\/1920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1920"}],"version-history":[{"count":1,"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts\/1920\/revisions"}],"predecessor-version":[{"id":1921,"href":"https:\/\/pelnar.tech\/index.php?rest_route=\/wp\/v2\/posts\/1920\/revisions\/1921"}],"wp:attachment":[{"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pelnar.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}