Tuesday, 23 January 2018

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.









No comments:

Post a Comment