Tuesday, 23 January 2018

SharePoint Online Automation -E-mail notification using PowerShell script in Office 365

Office 365 Automation -E-mail notification using PowerShell script in Office 365

  • In the below following article, described you about How to Check Site Quota in Office 365 using PowerShell.
Continue with the Automation series, going to describe you about how to send E-Mail notification after fetching Quota Report and send to Concern team and Distributed List. As we know, sometimes we need to send a confirmation email by email if the user is not in or working remotely. There is an easy PowerShell cmdlet called ‘Send-MailMessage’ that allows us to send an email to Exchange Server. The same function we can also use in Office 365.
There are two major options in which we address the Office 365 mail infrastructure
Anonymous SMTP session
Authenticated SMTP or TLS session
Here we are going to use “Authenticated SMTP or TLS session” in the simple word we are going to user credentials and an encrypted communication channel.
We need to use a PowerShell script that needs to use user credentials, here we can enter User Name and password (using Variable we will store) or we can choose Read line method to ask User Name and Password manually. Using Password in PowerShell is the easiest way to do but its something going to breach security part. Your Credentials will be in plain text and can be used by anyone if it is accessible for all users in your Domain.
So, we can encrypt the Password to avoid security breach using encrypted format, we will need to provide PowerShell the “user password” and the PowerShell command will take this password, encrypt the password and save it in a text file. In supplementary words, the information is not readable by a human called encrypt the password.
Below is the step by step command to “Send a mail PowerShell script” in office 365.
  • Open SharePoint Management Shell with required permission and Set Execution Policy as unrestricted or Remote signed. As by default, the PowerShell console doesn’t allow to run a PowerShell script.

  • Once execution policy set we can use below script in addition to below “How to Get Quota” script.

Import - Module Microsoft.Online.SharePoint.Powershell  
$UserName = "Global account"  
$Password = ‘Your Password '  

#Write-host "Email has been sent and script has been executed successfully"


$Credentials1 = New-Object System.Management.Automation.PSCredential($UserName1,(ConvertTo-SecureString $Password1 -AsPlainText -Force))

Connect-SPOService -Url "https://spsolutiontips-admin.sharepoint.com/" -Credential $Credentials1


$SiteURL = "https://spsolutiontips.sharepoint.com/sites/psp/Lists/test"


$smtp = "smtp.office365.com"


#$from = Read-host "Please provide your email address which you use for sending email"

$from = "admin@spsolutiontips.onmicrosoft.com"


$subject = "SharePoint Sites Quota Details"  


$body = "Dear Team <br><br>"


$body += "Good day:) <br><br>"


$body += "Click <a href= $SiteURL > here </a> to check the uploaded SPO sites quota Details file <br><br>"


$body += "Thank you<br><br>"


$body += "SharePoint Support Team"


$body += "Please do not respond to this email"


#send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority low


Send-MailMessage -From $from -To "admin@spsolutiontips.onmicrosoft.com" -Subject $subject -Body $body -BodyAsHtml -smtpserver $smtp -Port 587  -Credential $Credentials1 -UseSsl  


Write-host "Thank you, the script has been run successfully" -f Green  

  • In the above script, you need to modify the below part.
$UserName="Global account" Enter your Global Admin Account email address
$Password =Your Password' Enter Your Account Password, make sure you are keeping this password in the single quote or else it will throw an error.
    $SiteURL = Your Library URL where Docs will save
    $from = "You’re from Email address"

  • As mentioned the above, we are going to use secure SMTP email connections by using Port 587
  • Once all modification is done, hit F5 on PowerShell ISE and wait for some time to get the result.

  • Here You can see your Output email has been sent to your defined email or distributed List.
  • Check your email box and verify the same

No comments:

Post a Comment