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()
}
Like this:
Like Loading...