0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 42min

0 / 7 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

Send Emails from Gmail with PowerShell

Saving Progress...

In this lesson, you will be learning how to send emails from your Gmail account with Windows PowerShell.

There are several reasons why you may want to programmatically send emails with Windows PowerShell.  For example, you may want to automatically email users when their Active Directory passwords are about to expire, or when their AD account locks out.

Note: This functionality is disabled in the online IT labs due to security reasons.

In order to send emails directly from your either personal or company Gmail account, we are going to need to allow access first. This means we need to allow “less secure apps” to authenticate.

A less secure app is basically anything that simply uses a username and password rather than something that uses OAuth tokens. In the security world, this is a very bad practice… but for the simplicity of this tutorial, I am going to do it anyway.

I went to my GSuite admin panel and allowed users to manage their less secure apps: https://admin.google.com/u/1/ac/security/lsa?hl=en

Next, I logged into the target account I wanted to send the emails from and enabled “Allow less secure apps”: https://myaccount.google.com/lesssecureapps

Now we can send emails programmatically from windows PowerShell with the function below:

  1. function Send-Email() {
  2.     param(
  3.         [Parameter(mandatory=$true)][string]$To,
  4.         [Parameter(mandatory=$true)][string]$Subject,
  5.         [Parameter(mandatory=$true)][string]$Body
  6.     )
  7.  
  8.     $username   = (Get-Content -Path 'C:\Users\Paul Hill\credentials.txt')[0]
  9.     $password   = (Get-Content -Path 'C:\Users\Paul Hill\credentials.txt')[1]
  10.     $secstr     = New-Object -TypeName System.Security.SecureString
  11.     $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
  12.  
  13.     $hash = @{
  14.         from       = $username
  15.         to         = $To
  16.         subject    = $Subject
  17.         smtpserver = "smtp.gmail.com"
  18.         body       = $Body
  19.         credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
  20.         usessl     = $true
  21.         verbose    = $true
  22.     }
  23.  
  24.     Send-MailMessage @hash
  25. }

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments