Shell is not a new thing for any older timer … or for folks working with forbidden OSes like Unix and Linux. It is primary utility used by users to interact with those systems (and in older days it was perhaps the only way of interaction). Till few days back, I was obsessed with DOS and batch files. And I tried to do most of system automation tasks using them. I even received comments from Boss, that I can do better if I use PowerShell instead of DOS. But like some old lady obsessed with her old hat or something, I was reluctant to give away old love for DOS.
But recently I went to local Access user group meeting here at CT. And there was presentation for “Introduction to PowerShell for Database Developers”. And I realized that all this time I was just ignorant to not to use PowerShell. You have whole .NET stack for you to consume in addition to support of both DOS and Unix Shell commands !! In simple words, PowerShell is really A Shell on Steroids.
And with this new “inspiration” I have decided to give PowerShell a try and use it for automation tasks in newer systems (mostly Post Windows Server 2003 R2).
Couple of days back, I created a VB Script to send email notification to users using any publically available SMTP servers like Google or Yahoo. That script was actually using built in class “CDO.Message” for configuration and sending email. This can be in done same manner by creating simple .NET console application just like below,
MailMessage message = new MailMessage(); message.To.Add("ToMail"); message.Subject = "Test Subject for Email"; message.From = new MailAddress("fromMail"); message.Body = "Message from Google SMTP"; SmtpClient smtpG = new SmtpClient("smtp.gmail.com"); smtpG.EnableSsl = true; smtpG.Port = 587; System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential("loginEmail", "password"); smtpG.Credentials = mailAuthentication; smtpG.Send(message); Console.WriteLine("Mail sent using Gmail"); Console.ReadLine();
And if I am to create PowerShell script that does EXACTLY same, you will see that script itself it also same. It is not a surprise because in the end, PowerShell accesses .NET stack directly at OS level. So in one word, if you can do something in VB or C# .NET you can do same thing in PowerShell without any issue. So finally I ended up having PowerShell script as below,
cls Write-Host "Sending Email" #SMTP server name $smtpServer = "smtp.gmail.com" #Creating a Mail object $msg = new-object Net.Mail.MailMessage #Email structure $msg.From = "FromEmail" $msg.To.Add("ToEmail") $msg.subject = "My Subject" $msg.body = "This is the email Body." #Creating SMTP server object $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.EnableSsl = 1 $smtp.Port =587 $cred = New-Object Net.NetworkCredential("loginEmail","password") $smtp.Credentials = $cred #Sending email $smtp.Send($msg) Write-Host "Mail Sent"
With end of this post I have promised my self to try PowerShell further and use it where ever and when ever it is possible to user it.
That’s it for now…
It’s Just A Thought …