Quantcast
Viewing all articles
Browse latest Browse all 1058

Export Import Quota Templates in SharePoint with PowerShell

In SharePoint database attach method migration, Quota templates must be created manually between farms. Its a pain when you have multiple quotas defined. These PowerShell scripts simplifies the process by exporting quotas to a XML file and import it again to another farm.

PowerShell Script to Export/Import Quotas between environments:
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") 

function Export-QuotaTemplates([string]$FileName)
{
 #Get the SharePoint Web Application Service
 $ContentService =[Microsoft.SharePoint.Administration.SPWebService]::ContentService

 #Define Quota Templates XML -Container wrap
 $QuotaTemplateXML = '<QuotaTemplates>'
 #Get all Quota Templates
 foreach ($QuotaTemplate in $contentService.QuotaTemplates)
 {
    #Add Quota Templates to XML
    $QuotaTemplateXML += '<QuotaTemplate>'
    $QuotaTemplateXML += '<ID>'+ $QuotaTemplate.QuotaID +'</ID>'
    $QuotaTemplateXML += '<Name>'+ $QuotaTemplate.Name +'</Name>'
    $QuotaTemplateXML += '<StorageMaximumLevel>'+ [int](($QuotaTemplate.StorageMaximumLevel/ 1024)/1024) +'</StorageMaximumLevel>'
    $QuotaTemplateXML += '<StorageWarningLevel>'+ [int](($QuotaTemplate.StorageWarningLevel/ 1024)/1024) +'</StorageWarningLevel>'<# These two Properties applicable only for SharePoint 2010 and above!
    $QuotaTemplateXML += '<UserCodeMaximumLevel>'+ $QuotaTemplate.UserCodeMaximumLevel +'</UserCodeMaximumLevel>'
    $QuotaTemplateXML += '<UserCodeWarningLevel>'+ $QuotaTemplate.UserCodeWarningLevel +'</UserCodeWarningLevel>'
    #>
    $QuotaTemplateXML += '</QuotaTemplate>'
 }
 #Wrap Into the closing element
 $QuotaTemplateXML += '</QuotaTemplates>'
 $QuotaTemplateXML| Out-File $FileName
 Write-Host "Exported Quota Templates!"
}

 #Call Import Quota Templates Function
 Export-QuotaTemplates "QuotaTemplates.xml"

Once Exported, We can copy the exported xml file to target environment and run the Import script.

PowerShell Script to Import Quota Templates:
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") 

function Import-QuotaTemplates([string]$FileName)
{
   #Get the XML File Contents
   [xml]$QuotaTemplateXML = Get-Content $FileName

   $ContentService =[Microsoft.SharePoint.Administration.SPWebService]::ContentService
   #Get existing Quotas collection
   $QuotaTemplateColl = $ContentService.QuotaTemplates

   #Iterate throught each Quota Template
 foreach($QuotaTemplate in $QuotaTemplateXML.QuotaTemplates.QuotaTemplate)
 {
   Write-Host "Processing:" $QuotaTemplate.Name
  #Check if Quota Template already exists!
  if ($QuotaTemplateColl[$QuotaTemplate.Name] -ne $null) 
  {
   Write-Host ">> Quota template: " $QuotaTemplate.Name " Already exists!"
  }
  else #Create the Quota Template
  {
    $NewQuotaTemplate = new-object Microsoft.SharePoint.Administration.SPQuotaTemplate
   $NewQuotaTemplate.Name = $QuotaTemplate.Name
   $NewQuotaTemplate.StorageMaximumLevel = $QuotaTemplate.StorageMaximumLevel
   $NewQuotaTemplate.StorageWarningLevel = $QuotaTemplate.StorageWarningLevel<# These two Properties applicable only for SharePoint 2010 and above!
      $NewQuotaTemplate.UserCodeMaximumLevel = $QuotaTemplate.UserCodeMaximumLevel 
      $NewQuotaTemplate.UserCodeWarningLevel =  $QuotaTemplate.UserCodeWarningLevel
      #>
   $QuotaTemplateColl.Add($NewQuotaTemplate)

   Write-Host "    >> Quota template ", $NewQuotaTemplate.Name ," is imported to the Quota Templates"
  }
 }
 }

 #Call Import Quota Templates Function
 Import-QuotaTemplates "QuotaTemplates.xml"

In some cases, I saw site collections set to "Individual Quota" even before database attachment I copied the quota templates (dunno the cause but!). So, we can apply quotas in bulk. Here is how:
Set-SPSite -Identity "http://sharePoint.company.com/sites/sales" -QuotaTemplate "Gold - 1 GB"

Here "Gold - 1 GB" is our existing quota defined! For MOSS 2007, The code goes like:
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") 

function global:Get-SPSite($url)
 {
    return new-Object Microsoft.SharePoint.SPSite($url)
 }

$contentService =[Microsoft.SharePoint.Administration.SPWebService]::ContentService

$QuotaTemplate = $contentService.QuotaTemplates["Gold - 1 GB"]
$site = Get-SPSite "http://sharepoint.company.com/"
$site.quota=$QuotaTemplate 

Courtesy: http://sharepointpsscripts.codeplex.com/
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 1058

Trending Articles