Get SharePoint Site Quota Templates using PowerShell


A week or two ago I posted a function that allows you to create new SharePoint Site Collection Quota Templates using PowerShell. In that post I mentioned that two more functions (Get, Remove) would be coming soon.

Today I am sharing the function which allows you to get SharePoint Quota Templates. Again, this code is almost verbatim taken from the book by Gary and Shannon, but I have added comment-based-help and put it into an advanced function with CmdletBinding attributes.

The code is pretty simple, and again if you want a detailed walk through – I strongly encourage you to check out Gary and Shannon’s book!

Without further ado, here is the function which I have called Get-SPQuotaTemplate. I’m also finishing up the third function, Remove-SPQuotaTemplate – look for that post very soon!

Simply dot-source the function and then run it like a standard Windows PowerShell Cmdlet.

function Get-SPQuotaTemplate {
<#
.Synopsis
 This advanced function retrieves a SharePoint Site Quota Template.
.Description
 This function uses .NET code to retrieve the Quota Templates collection
 using a Microsoft.SharePoint.Administration.SPWebService object.
.Example
 C:\PS>Get-SPQuotaTemplate -Name "Custom"

 This example retrieves a SharePoint Quota Template called Custom in the
 current farm.
.Example
 C:\PS>Get-SPQuotaTemplate

 This example retrieves all SharePoint Quota Templates in the current farm.
.Notes
 Name: Get-SPQuotaTemplate
 Author: Ryan Dennis
 Last Edit: 5/10/2012
 Keywords: Quota Template, Quotas and Locks
.Link
 http://www.sharepointryan.com
 http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)][String]$Name
)
$Templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.QuotaTemplates
 if ($Name)
 {
 $Templates | Where-Object {$_.Name -eq $Name}
 }
 else
 {
 $Templates
 }
}

Create SharePoint Site Quota Templates using PowerShell


Last week I was stumbling through a site provisioning script, when I realized I was missing something that I needed – the ability to create a custom quota template and then apply it to a site collection. After doing some quick Get-Command work to try and find the out-of-the-box PowerShell Cmdlets, I quickly realized there are no OOTB Cmdlets to do this.

I was a little frustrated that this didn’t’ exist, but of course when I come across gaps in the OOTB PowerShell support for Microsoft SharePoint 2010 – I immediately look at that as an opportunity to not only figure out the problem, but to share the solution. Luckily for me, as I’ve done a few times now – I came across the solution in Gary Lapointe and Shannon Bray’s book. While they didn’t have a function, they did detail the essentials on how to create the objects necessary to provision quota templates.

After spending a little time this weekend putting it all together, I’m left with a pretty decent function that allows you to create site quota templates easily using PowerShell!

I have added comment-based help to this function, as my intention was to make it as reusable and thorough as possible. The code is pretty simple, and if you want a detailed walk through – I strongly encourage you to check out Gary and Shannon’s book!

Without further ado, here is the function which I have called New-SPQuotaTemplate. I’m also finishing up two other functions, Get-SPQuotaTemplate and Remove-SPQuotaTemplate – look for those soon!

Simply dot-source the function and then run it like a standard Windows PowerShell Cmdlet.

function New-SPQuotaTemplate {
<#
.Synopsis
	This advanced function creates a new Site Quota Template.
.Description
	This function uses .NET code to instantiate an instance of an 
	SPQuotaTemplate class. Once the object is created, an instance of the 
	SPWebService class is instantiated and the Quota Template is added to the 
	Quota Templates Collection.
.Example
	C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 2GB -StorageWarningLevel 1GB -UserCodeMaximiumLevel 100 -UserCodeWarningLevel 75
	
	This example creates an SP Quota Template called Custom with a maximum size
	of 2GB and a warning size of 1GB. Sandboxed solutions are 
	limited to 100, with a warning level of 75.
.Example
	C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 4GB -StorageWarningLevel 3GB
	
	This example creates an SP Quota Template called Custom with a maximum size
	of 4GB and a warning size of 3GB
.Notes
	Name: New-SPQuotaTemplate
	Author: Ryan Dennis
	Last Edit: 4/27/2012
	Keywords: Quota Template, Quotas and Locks
.Link
	http://www.sharepointryan.com
 	http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$Name,
[Parameter(Mandatory=$true)][Int64]$StorageMaximumLevel,
[Parameter(Mandatory=$true)][Int64]$StorageWarningLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeMaximumLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeWarningLevel
)
# Instantiate an instance of an SPQuotaTemplate class #
Write-Verbose "Instantiating an instance of an SPQuotaTemplate class"
$Quota = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate
# Set the Properties #
Write-Verbose "Setting properties on the Quota object"
$Quota.Name = $Name
$Quota.StorageMaximumLevel = $StorageMaximumLevel
$Quota.StorageWarningLevel = $StorageWarningLevel
$Quota.UserCodeMaximumLevel = $UserCodeMaximumLevel
$Quota.UserCodeWarningLevel = $UserCodeWarningLevel
# Get an Instance of the SPWebService Class #
Write-Verbose "Getting an instance of an SPWebService class"
$Service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
# Use the Add() method to add the quota template to the collection #
Write-Verbose "Adding the $($Name) Quota Template to the Quota Templates Collection"
$Service.QuotaTemplates.Add($Quota)
# Call the Update() method to commit the changes #
$Service.Update()
}