{"id":8451,"date":"2012-08-05T00:01:00","date_gmt":"2012-08-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/05\/weekend-scripter-introducing-the-scripting-guys-powershell-loveomatic\/"},"modified":"2012-08-05T00:01:00","modified_gmt":"2012-08-05T00:01:00","slug":"weekend-scripter-introducing-the-scripting-guys-powershell-loveomatic","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-introducing-the-scripting-guys-powershell-loveomatic\/","title":{"rendered":"Weekend Scripter: Introducing the Scripting Guys PowerShell LoveOmatic"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell to send random email messages at random times.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is Sunday in Charlotte, North Carolina in the deep south of the United States. This morning I am reflecting on a conversation I had with Ashley McGlone, a Microsoft PFE and my mentee. I had just finished hearing him make his first ever presentation at TechReady, and I was evaluating his performance (he did an awesome job, by the way). Somehow, I got off on a tangent (if you have ever talked to me, you will realize immediately how rare of an occurrence this is), and I began discussing a script I wrote when I was teaching a VBScript class based on my VBScript Step-by-Step book from Microsoft Press.<\/p>\n<p>I called the script the LoveOmatic (basing the title on the famous series of Scripting Guys tools&ndash;including <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=24121\" target=\"_blank\">my own PowerShell ScriptOmatic tool<\/a>). When I used to travel every week teaching scripting workshops around the world, I would send short email messages to Teresa at various times of the day (generally when the students were working on their labs, in addition to before class, after class, after dinner, and so on). In class one day, a student mentioned that I could write a script to do that, and the LoveOmatic was born.<\/p>\n<h2>First get rid of &ldquo;magic numbers&rdquo;<\/h2>\n<p>In my Windows PowerShell 2.0 Best Practices book, I talk at great length about the importance of readability for your Windows PowerShell code. One thing that destroys readability is the presence of &ldquo;magic numbers.&rdquo; A magic number is a number that appears in a script with very little context. It simply appears in the script. One generally has to look up the value to see what the script is doing. I like to assign values to well named variables, and then use the variable in my code. It promotes readability and facilitates modification of the script. The following code illustrates my point.&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$numberOfEmails = 6<\/p>\n<p style=\"padding-left: 30px\">$oneHour = 3600<\/p>\n<p style=\"padding-left: 30px\">$threeHours = 10800<\/p>\n<h2>Create an array of strings<\/h2>\n<p>Now, I need to create an array of messages. I create five different strings, and then I assign the variables that contain the strings into an array as shown here.<\/p>\n<p style=\"padding-left: 30px\">$message1 = &#8220;I am thinking of you.&#8221;<\/p>\n<p style=\"padding-left: 30px\">$message2 = &#8220;You are the sunshine of my life.&#8221;<\/p>\n<p style=\"padding-left: 30px\">$message3 = &#8220;You walk in beauty like the night.&#8221;<\/p>\n<p style=\"padding-left: 30px\">$message4 = &#8220;I am glad I met you.&#8221;<\/p>\n<p style=\"padding-left: 30px\">$message5 = &#8220;My life is richer with you part of it.&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$message = $message1,$message2,$message3,$message4,$message5<\/p>\n<h2>All the normal email stuff<\/h2>\n<p>I use four variables to store the usual email stuff. The four essential email fields are from, to, subject, and the name of the mail server. The following four variables illustrate the technique.<\/p>\n<p style=\"padding-left: 30px\">$from = &#8220;edwilson@nwtraders.msft&#8221;<\/p>\n<p style=\"padding-left: 30px\">$to = &#8220;ScriptingWife@nwtraders.msft&#8221;<\/p>\n<p style=\"padding-left: 30px\">$subject = &#8220;I am thinking of you&#8221;<\/p>\n<p style=\"padding-left: 30px\">$smtpServer = &#8220;smtphost.nwtraders.msft&#8221;<\/p>\n<h2>Sending random mail messages at random times<\/h2>\n<p>The heart of the LoveOmatic uses the following Windows PowerShell cmdlets:<\/p>\n<ul>\n<li>Get-Random<\/li>\n<li>Send-MailMessage<\/li>\n<li>Get-Random<\/li>\n<li>Start-Sleep<\/li>\n<\/ul>\n<p>Because of these cmdlets, the actual code to send email messages is very simple. I use a <b>for<\/b><i> <\/i>statement to send a specific number of mail messages. The <b>for<\/b><i> <\/i>statement counts up to the value stored in the <b>$numberOfEmails<\/b> variable. This is shown in the code that follows.<\/p>\n<p style=\"padding-left: 30px\">For($i=0; $i -le $numberOfEmails; $i++)<\/p>\n<p>Now, I need to generate a random number that goes from 0 to the number of messages I have stored in my <b>$message<\/b> array. I keep the returned random number in the <b>$rnd<\/b> variable. One thing to keep in mind when you use the <b>Get-Random<\/b> cmdlet, is that the <b>maximum<\/b><i> <\/i>number is never reached. Therefore, the command that is shown here creates random numbers between 0 and 4. It will never return a random number equal to 5.<\/p>\n<p style=\"padding-left: 30px\">$rnd = Get-Random -Minimum 0 -Maximum 5<\/p>\n<p>The following command sends the email message. It uses the <b>$to<\/b> variable and the values contained in <b>$from<\/b> and <b>$subject<\/b>. Because the <b>$message<\/b> variable is an array of message strings, I use the random number stored in the <b>$rnd<\/b> variable, and then index into the collection of messages. This permits selecting a random mail message to send. For more randomness, the same technique could be applied to the subject field. Here is the code that sends the email.<\/p>\n<p style=\"padding-left: 30px\">Send-MailMessage -to $to -body $message[$rnd] `<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -from $from -subject $subject `<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -smtpServer $smtpServer&nbsp;<\/p>\n<p>I display a message to the console just for status updates as the script runs. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">&#8220;message: $($message[$rnd]) sent at $((get-date).tostring())&#8221;<\/p>\n<p>Now I need to pause the script. I pause it a random amount between 1 hour and 3 hours. To do this, I use the <b>seconds<\/b><i> <\/i>parameter from the <b>Start-Sleep<\/b> cmdlet, and I choose a random number between 3600 and 10,800. But rather than listing these numbers directly, I store them in variables to make the code easier to read. The <b>Start-Sleep<\/b> cmdlet is shown here.<\/p>\n<p style=\"padding-left: 30px\">Start-Sleep -Seconds (Get-Random -Minimum $oneHour -Maximum $threeHours)<\/p>\n<p>When the script runs, output similar to the one shown in the following image appears.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8865.WES-8-5-12-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8865.WES-8-5-12-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>&nbsp;<\/p>\n<p>I uploaded <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/The-LoveOmatic-PowerShell-cd009acc\" target=\"_blank\">the complete script<\/a> to the Scripting Guys Script Repository.<\/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><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;Windows PowerShell, Scripting Guy!, Weekend Scripter, Messaging &amp; Communication, SMTP Mail, Sending email<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell to send random email messages at random times. Microsoft Scripting Guy, Ed Wilson, is here. It is Sunday in Charlotte, North Carolina in the deep south of the United States. This morning I am reflecting on a conversation I had with Ashley McGlone, [&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,3,182,358,61,45],"class_list":["post-8451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-messaging-communication","tag-scripting-guy","tag-sending-email","tag-smtp-mail","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell to send random email messages at random times. Microsoft Scripting Guy, Ed Wilson, is here. It is Sunday in Charlotte, North Carolina in the deep south of the United States. This morning I am reflecting on a conversation I had with Ashley McGlone, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8451","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=8451"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8451\/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=8451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}