Wednesday, February 6, 2013

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"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...