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.

Set-ExecutionPolicy
  • 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



SharePoint Online Automation: Checking Site Quota for an Office 365 tenant

Today I am going to write about SharePoint Online Automation script for Checking Site Quota.  SharePoint or Office 365 Automation provide powerful proficiencies to automate tasks with SharePoint online to reduce Human effort and error as well. In this post, I will cover a scenario where you connect to SharePoint Online from Using SharePoint Online PowerShell Management and display the list of all provisioned site collections URL and Site Quota for an Office 365 tenant. This post is mainly geared towards getting you started on SharePoint Online or Office 365 Automation Once prepared with this knowledge you can to repeat task easily and hassle-free.
Prerequisites
First things, we need to have Office 365 subscription and SharePoint Online Management installed on your local system. If you don't already have them you may want to register for a free trial version of Office 365. I also recommend reading below articles before you start with this demonstration.


Below is the step by step solution with screen shots.


  • Open SharePoint ISE with Admin account and enter below script.


Import-Module Microsoft.Online.SharePoint.Powershell

 

$UserName="Global account"   

$Password =Your Password'

$UsagePercentagelimit ="1"  

#Hash Table to hold results   

$groups = @{}  

#Setup Credentials to connect   

$Credentials = New-Object System.Management.Automation.PSCredential($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))   

#Connect-MsolService  -Credential $Credentials  

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

  

foreach($spoSite in (Get-SPOSite -Limit all -IncludePersonalSite:$false))

{  

   $groups.Add($spoSite.Url,@{})    

   $StorageGB = [System.Math]::Round(($spoSite.StorageUsageCurrent/1024),2)  

   $QuotaGB = ($spoSite.StorageQuota/1024)   

   $groups[$spoSite.Url].Add("QuotaGB",$QuotaGB)       

   $groups[$spoSite.Url].Add("StorageGB",$StorageGB)  

    if ($QuotaGB -eq 0) {

       $UsagePercentage = [math]::Round($StorageGB, 2) * 100

   } else {

       $UsagePercentage = [System.Math]::Round(($spoSite.StorageUsageCurrent/$spoSite.StorageQuota)*100,2)

   }

   $groups[$spoSite.Url].Add("Size",$spoSite.StorageUsageCurrent)  

   $groups[$spoSite.Url].Add("UsagePercentage",$UsagePercentage)  

  

}  

$exp = @("Url,Storage USed,Storage Limit,Percentage Used %,")  

Foreach($g in $groups.keys) {

if($($groups[$g]["UsagePercentage"]) -ge $UsagePercentagelimit){  

 

   $exp += ("$($g)","$($groups[$g]["StorageGB"])","$($groups[$g]["QuotaGB"])","$($groups[$g]["UsagePercentage"])" -join ",")  

     }

}

 
$exp | Out-File "C:\testing\Quota.csv" -Encoding ascii -Force  


  • In Above script, you need to modify below part:


$UserName="Global account"   Enter your Global Admin Account email address
$Password =Your Password' Enter Your Account Password, make sure you're keeping this password in single quote or else it will throw an error.
$UsagePercentagelimit ="1”   Enter the result you want to get Maximum % site usages result.

Connect-SPOService -Url "https://spsolutiontips-admin.sharepoint.com/" -Credential $Credentials Enter Your tenant id here to connect with SPO service.

$exp | Out-File "C:\testing\Quota.csv" -Encoding ascii -Force   Enter out file path with Force command so it will overwrite existing same named file


  • Once you have done with above modification press f5 and wait for some time to get result in given file location.




  • You can see some warning message that you can ignore.


Note: Make sure you have entered correct user name and password in order to proceed with above PowerShell script.
  • Once script is completed, navigate to output file and you can see result has been written




  • Open the CSV file and can see the result format is as excepted


  • Above result will bring the result of storage used Site collection only.