IT Consultant Everyday Notes

Just some problems/solutions storage

Azure Automation: Send Email from Azure Automation Script via GMAIL

I am working with Azure Automation scripts. One of them stops all my Lab VMs after working hours to save some money. Script is based on one from Automation Gallery, but I wanted to add a notification feature.

There are several posts about using O365 for this, but I do not think it is a good idea since 0365 is not free.

I tried Outlook.com (AKA Hotmail) first, trying to stick with Microsoft platform, but did not get any success (authentication kept failed for me). So, the second choice was Gmail.com. From some posts I understood Azure does not have root certificates from GMAIL CA and SSL connection does not work. To workaround the issue I downloaded Google root certificate and created a Certificate Asset in Automation console

SNAGHTML9554a11d 

Interesting enough I do not need to use it in my script apparently simply existence of it is enough….

Here is the script to check if all machines are in stop(Deallocated) state and send email otherwise. The script uses a PS Credential Asset: ‘Azure Credentials’ and my MSDN Platform subscription.

I created a test account at Gmail: azure.automation.service@gmail.com and add an Automation Asset (PS Credentials) including Gmail user name and password – “Gmailcreds” that allows do not put user name/password in the script.

workflow test-mail
{  
   $Cred = Get-AutomationPSCredential -Name ‘Azure Credentials’
   $Gmailcreds = Get-AutomationPSCredential -Name ‘Gmailcerds’
   Add-AzureAccount -Credential $Cred
   Select-AzureSubscription -SubscriptionName “MSDN Platforms”   
   $vms = Get-AzureVM
   $ss=””
   ForEach ($vm in $vms ) {
      if ($vm.Status -ne “StoppedDeallocated”) {$ss=$ss+$vm.name+” – “+$vm.Status + “`r`n”}
   }
   if ($ss -ne “”) {
      $mail_body= ‘Attention! One or more VMs are in a state other than “Stopped (Deallocated)”‘ `
      +”`r`n”+$ss
      Send-MailMessage -SmtpServer smtp.gmail.com -Port 587 -Credential $Gmailcreds `
         -UseSsl -From ‘azure.automation.service@gmail.com’ -To ‘alex.ignatenko@onx.com’ `
         -Subject ‘Alarm: Azure Automation – Running VM!’ -body $mail_body
   }
}

This script can be added to schedule to run every night.

Advertisements

4 responses to “Azure Automation: Send Email from Azure Automation Script via GMAIL

  1. Peter April 1, 2015 at 12:35 am

    Hi, I was looking for a script exactly like this one. I configured it but getting this error

    Cannot process argument transformation on parameter ‘Credential’. A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials. (A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials.)

    any idea?

    Thanks!

    Peter

  2. Peter April 1, 2015 at 10:51 pm

    yeah, I did that and still getting the same error

    Cannot process argument transformation on parameter ‘Credential’. A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials. (A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials.)

    bummer 😦

  3. Peter April 1, 2015 at 11:40 pm

    looks it was a mistmatch between the subscription I put in the script and the one used to run the AutomationAccount so now it does not throw out this error but I still don’t get any email. Must be something with SMTP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: