Weekend Scripter: A Windows PowerShell Carol: Ebenezer Script Learns to Send Email

Weekend Scripter: A Windows PowerShell Carol: Ebenezer Script Learns to Send Email

  • Comments 2
  • Likes

  

Summary: Conclusion to the Windows PowerShell Carol in which Ebenezer Script learns how to send email from inside a PowerShell script.

 

Microsoft Scripting Guy Ed Wilson here. It is almost the end of the year and we have decided to devote some posts to the holiday season. We even have guest bloggers from around the world to share some holiday spirit. This week we have had Sean Kearney. You can learn more about Sean in Monday’s blog introduction. Be sure to check out Sean’s theme song for this week’s PowerShell Carol posts.

 

Ebenezer awoke with a start.

Light poured onto his face like a fresh hot shower. It was rejuvenating and it was invigorating. With the sudden realization that he was not a terminated process but was still alive, he was quite ENERGIZED!

He quickly searched online for something fun to do. That was how he used to script. “Passion and Joy to release the Toy,” he muttered eyes fluttering left and right.

Moments later he had himself a small script that made his name appear letter by letter. Just for fun.

$Phrase=“Happy Holidays Mr. Script!”
Foreach ($count in 0..(($Phrase.Length)-1)) { WRITE-HOST $Phrase.substring($count,1) ” “ -nonewline; start-sleep -milliseconds 100 }; WRITE-HOST

 

It played to the screen, delighting the old master. It was a spark not seen in years.

Jumping up and down like a jackrabbit, Ebenezer burst out the front door (still in boxer shorts) screaming at the top of his lungs “I’M USING WINDOWS POWERSHELL! I’M USING WINDOWS POWERSHELL!” (Strangely, he was singing it to an AC/DC rock tune.)

A few passersby looked at him oddly. But then he looked over and saw a small kid with a Zune. “Hey, you! What time is it? Are the computer shops opened up yet?” he yelled to the lad like a maniac.

“It is time for you to put some clothes on, Sir!” the young fellow chattered back, “and yes, yes the computer shops are open!”

“Silly boy and truthful, too,” mulled Ebenezer.  Ebenezer dashed back inside to grab some swag from an old TechNet tour to put on. Back outside he yelled back.

“Boy! Does the computer shop still have that honking new laptop?”

The fellow’s eyes lit up. “Do you mean the new Windows 7 Tablet PC with Multitouch display, i7 Intel Quadcore chip, mirrored solid state drives, Emotive.com interface tied into Kinect?”

“Yup! That’d be the one!” Ebenezer chortled back.

“Run up the road and block anybody from buying it! I’m about to make somebody’s day! Do it fast and I’ll buy you a MultiTouch netbook of your own! Stop anybody from buying it and I’ll toss in a Windows Phone 7!”

The kid tore off down the road like a rocket, knocking over dogs, oranges and a small Volkswagen. “Windows Phone 7,” he muttered with his eyes lit up like a Blue Laser mouse.

Mr. Script giggled. “Tiny Shim is to become a Chartered Accountant. Bah! I’ll see to it such a cruel thing never happens! I’ll encourage that wee lad.”

With that, Mr. Script went online to the Microsoft store and maxed out his Triple platinum card, four times over.

The next day, Bob Snipit showed up at the office. He stumbled in dropping his copy of Windows PowerShell Step by Step.

Ebenezer looked down, pretending to be angry.

“Baaaah! More of that nonsense, eh? Codebug! Come over here right now! I’ve got a word to give to you!”

Bob shuffled over fearing the worst. Not another day of listening to him whine about, “In my day, we didn’t have a fancy GUI. We hard coded our own editors by flipping switches directly…”

“Mr. SNIPIT!” barked Mr. Script, “I’ve had enough of your flippant disregard for VBScript!  This complete habberdash. Codebug! This WINDOWS POWERSHELL! So that’s it! I’m going to have to…”

Bob braced for it, fearing termination or worse; having to work in LOGO.

“…have to get you to teach it to me.” he giggled madly throwing poor Bob for a loop as Mr. Script quipped:

GET-CONTENT C:\BobSnipitReaction.txt

 

Bob’s mouth hit the floor so hard he looked like something out of an old Saturday morning cartoon.

“Bob. I’ve been a crabby old idiot. The Spirit of Scripting hit me hard last night. Over the years having to drop one technology and learn another, I did not think it so. Then last night I was visited by three spir... Well, let’s just say I had a revelation last night. I’d like for you to teach me the wonder of Windows PowerShell.”

Bob sat stunned.

“Bob, I have one more request. We’re not going to work today. I’d like to do something BIG in Windows PowerShell for the Holidays. Would you help me?”

Still shocked all Bob could mutter was “uh huh.”

“So Bob, I was thinking of emailing everybody a cheery greeting. But in VBScript it was difficult to send an email an e-…”

“An e-mail? That’s a Windows PowerShell command!” Burst out Bob, not realizing himself.

“Built in, eh?” Ebenezer blinked with delight, “Wonderful! So how do we do this?”

Bob looked over. “May I sir?” reaching for the keyboard.

“Go ahead…” the old master watched with great interest.

“Well Mr. Script, in Windows PowerShell to send an email I just use this command…”

SEND-MAILMESSAGE

 

“…and to see HOW to use it we just key in”

GET-HELP SEND-MAILMESSAGE –examples

 

Ebenenzer blinked with delight. “Wonderful! It has built in examples of how to use it!  So I see from the examples to send an email to you with an attachment I just key in:”

send-mailmessage -from “Mr.Script <grump@contoso.com>" -to "Bob <assistant@contoso.com>" -subject "Happy Holidays" -body "Happy Holidays from Mr. Script" -Attachment "Picture.JPG” -smtpServer smtp.fabrikam.com

 

Bob nodded. “But here’s the nicer part. Did you know with that same line you could send a custom email to a LIST of people? Each person receiving what appears to be a single, custom email?”

Ebenezer looked up. “Do you mean like when using a Mail Merge? You can do an email merge WITH Windows PowerShell?” He almost fainted.

Bob nodded. “With a single text file I will call “names.csv” with quotes about the names and commas separating the information like this:”

“FirstName”,”LastName”,”Email”
“Bob”,”Snipit”,”bob@contoso.com”
“Shim”,”Snipit”,”tinyshim@fabrikam.com”
“Ebenezer”,”Script”,”grump@contoso.com”

 

“We can make single custom email to each person from your original.”

IMPORT-CSV C:\Data\Names.csv | foreach {

$NameFrom=“Mr.Script <grump@contoso.com>"
$NameTo=$_.Firstname+” “+$_.Lastname+”<”+$_.Email+”>”
$Subject=”Happy Holidays “+$_.Firstname+”!”
$Body=”Hello “+$_.Firstname+” and have a Happy Holiday from Mr. Script!”

$File=”C:\Photo\Picture.JPG”\

send-mailmessage -from $NameFrom -to $NameTo  -subject $Subject -body $Body -Attachment $File -smtpServer smtp.fabrikam.com

}

 

Ebenezer blinked with great delight. He could not wait to start scripting!

 

Epilogue

And so the Spirit of Scripting took hold of Ebenezer. He dived into Windows PowerShell with a passion and became one of the most enjoyable people to deal with afterwards, soon becoming a great Presenter and Microsoft MVP.

Bob SnipIt never lost his passion for scripting and soon was hired as a Microsoft Scripting Guy. He moved to Redmond, Washington from his little shack outside Toronto, Ontario. He occasionally hummed tunes, but to his wife (and the Community’s) great delight; quietly wrote poetry instead.

Tiny Shim? How could we forget Tiny Shim? Chartered Accountancy never did claim him.

With that honking new laptop, Mr. Script bought him? That wee lad lit into his gift with a passion never seen before by humanity. He leapt beyond scripting and became one of the most Gifted Windows PowerShell Architects ever, sending inspiration everywhere with his passion for technology.

As Tiny Shim Tweeted one day:

GET-WORLD | SEND-GREETING ‘May we never lose sight of our passions and always see a better day.’

 

Happy Holidays everyone, and may you never forget the Spirit of Scripting! Write scripts in whatever language suits you best but never forget the joy of your first line of code. Never lose sight of your passion.

Thank you Sean for this week’s “A Windows PowerShell Carol.” Join us tomorrow as we wrap up Holiday Guest Blogger week with a most excellent post from Microsoft Premier Field Engineer (PFE) Chris Bellée from the land down under. That is right, Chris will join us from Australia.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment