Set Default Quota for SharePoint Online Sites including office 365 group Sites

We are managing a large Tenant and utilizing most of the features of Office 365, because of which sharepoint Sites are rapidly increasing in terms of numbers.

All sites like office 365 group sites, communication sites are created by default with 1 TB quota. We want users to use the storage effectively and with a caution so decided to provide each site with a 10 Gb of Quota.

This script has been written to cater this goal and has been scheduled to run nightly to set quota for any newly created office 365 group or other sites to 10 GB along with the report that is being sent to us.

Download and Extract the script from below links:

https://gallery.technet.microsoft.com/scriptcenter/Set-Default-Quota-for-8b8f2fa5

Update the Variables inside SETSPDefaultquota.ps1

##########################Load variables & Logs##################

$log = Write-Log -Name “process_SpSiteDefaultQuota” -folder logs -Ext log

$report = Write-Log -Name “SetSPQuota” -folder Report -Ext csv

$orgname = “Company”   #########Enter the Organization name

$collection = @()

$collection1 = @()

$quota = “10240”  ###########10 GB quota to be set

$quotawarning = “9216”  ############90 % warning to be set

$msdefquota = “1048576” #####default quota set by MS on all sites

$smtpserver = “smtp.labtest.com”  

$from = “Automate@labtest.com”

$erroremail = “Reports@labtest.com”

###########################################################

Below is the part to use the account that can connect to sharepoint online to update the quotas of the sites i.e. Sharepoint Administrator Service account.

Encrypt.bat  that you will find in the extracted folder can be used to encrypt the password to be used inside the script.

It will generate the password and save it in password1.txt file (you can get the encrypted password from there and paste it inside the script)

#####################userid/password##########################

$userId = “COMP-Auto-SVC@labtest.com”

$encrypted1 = “01000000d08c9ddf000008a2352c3c28df30db90961f410b80029a2af3555767e135”

$pwd = ConvertTo-SecureString -string $encrypted1

$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $userId, $pwd

############################################################

Script is fetching all the sites with 1 TB quota except sites under portals(i.e. video channels)

$collection = Get-SPOSite -Limit All | where{ ($_.lockstate -eq “Unlock”) -and ($_.Url -notlike “*/portals/*”) -and ($_.StorageQuota -eq $msdefquota) } | select url, owner, template, StorageUsageCurrent, StorageQuota

After fetching the Sites it loops thru them and set the quota and warning quota as per you have provided in the $quota and $quotawarning variable.

Script will send the report on sites on which it has actioned.

Schedule the script to run daily or as per your enviornmental needs.

End of the script contains code to recycle the log and reports (its set to 60 days, you can change it accordingly)

<#     
    .NOTES 
    =========================================================================== 
     Created on:       2/27/2018 1:37 PM 
     Created by:       Vikas Sukhija (http://SysCloudPro.com) 
     Organization:      
     Filename:         SetSPDefaultQUota.ps1 
    =========================================================================== 
    .DESCRIPTION 
        This script will set the deafult quota of 10 GB to all Newly created sites 
#> 
$error.clear() 
try { stop-transcript | out-null } 
catch { $error.clear() } 
 
function Write-Log 
{ 
    [CmdletBinding()] 
    param 
    ( 
        [Parameter(Mandatory = $true)] 
        [array]$Name, 
        [Parameter(Mandatory = $true)] 
        [string]$Ext, 
        [Parameter(Mandatory = $true)] 
        [string]$folder 
    ) 
     
    $log = @() 
    $date1 = get-date -format d 
    $date1 = $date1.ToString().Replace("/""-") 
    $time = get-date -format t 
     
    $time = $time.ToString().Replace(":""-") 
    $time = $time.ToString().Replace(" """) 
     
    foreach ($n in $name) 
    { 
         
        $log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext" 
    } 
    return $log 
} 
 
function LaunchSPO 
{ 
    param 
    ( 
        $orgName, 
        $cred 
    ) 
     
    Write-Host "Enter Sharepoint Online Credentials" -ForegroundColor Green 
    $userCredential = $cred 
    Connect-SPOService -Url "https://$orgName-admin.sharepoint.com" -Credential $userCredential 
} 
 
Function RemoveSPO 
{ 
     
    disconnect-sposervice 
} 
##########################Load variables & Logs#################### 
$log = Write-Log -Name "process_SpSiteDefaultQuota" -folder logs -Ext log 
$report = Write-Log -Name "SetSPQuota" -folder Report -Ext csv 
$orgname = "Company" 
$collection = @() 
$collection1 = @() 
$quota = "10240" 
$quotawarning = "9216" 
$msdefquota = "1048576" 
$smtpserver = "smtp.labtest.com" 
$from = "Automate@labtest.com" 
$erroremail = "Reports@labtest.com" 
#####################userid/password########################## 
$userId = "COMP-Auto-SVC@labtest.com" 
$encrypted1 = "01000000d08c9ddf0115d1103660000c000000010000000edb3510a5e61c98c23c3ee14e6ac000bda93b938c97851f02070753d27b3bcd200000008a2352c3c28df30db902f7029fd8f0cd3c7bd3e88e4b865bbb76ffb412d03627140000006f7bbee447fb961f410b80029a2af3555767e135" 
$pwd = ConvertTo-SecureString -string $encrypted1 
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $userId$pwd 
##########Start Script main############## 
 
Start-Transcript -Path $log 
$cred = $Credential 
try 
{ 
    LaunchSPO -orgName $orgname -cred $cred 
} 
catch 
{ 
    write-host "$($_.Exception.Message)" -foregroundcolor red 
    Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "SP Online ConnectionError Site owner Report" -Body $($_.Exception.Message) 
    break 
} 
$collection = Get-SPOSite -Limit All | where{ ($_.lockstate -eq "Unlock"-and ($_.Url -notlike "*/portals/*"-and ($_.StorageQuota -eq $msdefquota) } | select url, owner, template, StorageUsageCurrent, StorageQuota 
 
if ($collection) 
{ 
    $collection | foreach-object{ 
        $mcoll = "" | select  url, owner, template, StorageUsageCurrent, StorageQuota, ModifiedQuota 
        $mcoll.url = $_.url 
        $mcoll.owner = $_.owner 
        $mcoll.template = $_.template 
        $mcoll.StorageUsageCurrent = $_.StorageUsageCurrent 
        $mcoll.StorageQuota = $_.StorageQuota 
        Set-SPOSite -identity $_.url -StorageQuota $quota -StorageQuotaWarningLevel $quotawarning 
        if ($error) 
        { 
            $mcoll.ModifiedQuota = "Error" 
        } 
        else 
        { 
            $mcoll.ModifiedQuota = $quota 
        } 
        $mcoll 
        $collection1 +$mcoll 
    } 
    $collection1 | Export-Csv $report -NoTypeInformation 
    if ($error) 
    { 
        Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error Occured - Set Default Quota SPOnline Sites" -Body $error[0].tostring() -Attachments $report 
    } 
} 
RemoveSPO 
 
########################Recycle reports & logs############## 
$path1 = ".\report\" 
$path2 = ".\Logs\" 
$limit = (Get-Date).AddDays(-60) #for report recycling 
Get-ChildItem -Path $path1 | Where-Object { 
    $_.CreationTime -lt $limit 
} | Remove-Item -recurse -Force 
 
Get-ChildItem -Path $path2 | Where-Object { 
    $_.CreationTime -lt $limit 
} | Remove-Item -recurse -Force 
 
get-date 
Write-Host "Script finished" -ForegroundColor green 
Stop-Transcript 
######################################################### 
 
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Transcript Log - Set SPO Default Quota" -Attachments $log 
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Report - Set SPO Default Quota" -Attachments $report 
 
############################################################

Thanks for reading and downloading

Sukhija Vikas

http://SysCloudPro.com

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s