Saturday, September 27, 2014

Create and Set the quota template for site collections after moving to the SP2013 using PowerShell script


The following script will create and set the quota template for site collections after moving to the SP 2013. This script will generate the xml file.

Script:

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
}


function Get-SPQuotaTemplates
{
    # Prepare XML structure
    $xmlText =
@"
<?xml version="1.0" encoding="UTF-8" ?>
<QuotaTemplates AsOn="">
    <QuotaTemplate SiteUrl="Sample" Name="Sample" Value="0" QuotaID="0" StorageMaximumLevel="0" InvitedUserMaximumLevel="0" StorageWarningLevel="0" UserCodeWarningLevel="0" UserCodeMaximumLevel="0" UpgradedPersistedProperties="0" />
</QuotaTemplates>
"@

 
    # Read the XML string into an object
    $xmlDoc = [xml]$xmlText

    # Read the sample element
    $sampleQuotaTemplate = @($xmlDoc.QuotaTemplates.QuotaTemplate)[0]
    $service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
    #Get all the SiteCollections
    $spsite= Get-SPSite -Limit All
    #Iterate through all the sitecollections
 
    foreach ($spsiteitem in $spsite)
    {
        $spsiteitem.Quota
        if($spsiteitem -ne $null)
        {   $flag=0
            #$service.QuotaTemplates
            foreach($serviceitem in $service.QuotaTemplates)
            {
             
                if($serviceitem.QuotaID -eq $spsiteitem.Quota.QuotaID)
                {
                    $quotaName=[string]($serviceitem.Name)
                    $tempQuotaTemplate=$serviceitem
                    $flag=1
                 
                }
             

            }
            if($flag -eq 0)
            {            
                    $quotaName="Individual Quota"
                    $tempQuotaTemplate=$spsiteitem.Quota
            }

       
                  $newQuotaTemplate = $sampleQuotaTemplate.Clone()
     
       #$newQuotaTemplate.Name = [string]($SPSite.Quota.QuotaID)
                  $newQuotaTemplate.SiteUrl=[string]$spsiteitem
         $newQuotaTemplate.Name =$quotaName.ToString()
       $newQuotaTemplate.Value = [string]$tempQuotaTemplate.Value
                  $newQuotaTemplate.QuotaID = [string]$tempQuotaTemplate.QuotaID
       $newQuotaTemplate.StorageMaximumLevel = [string](($tempQuotaTemplate.StorageMaximumLevel) / 1024 / 1024)
       $newQuotaTemplate.StorageWarningLevel = [string](($tempQuotaTemplate.StorageWarningLevel) /1024 / 1024)
       $newQuotaTemplate.UserCodeMaximumLevel = [string]$tempQuotaTemplate.UserCodeMaximumLevel
       $newQuotaTemplate.UserCodeWarningLevel = [string]$tempQuotaTemplate.UserCodeWarningLevel
                  $newQuotaTemplate.UpgradedPersistedProperties =[string]$tempQuotaTemplate.UpgradedPersistedProperties
                  $newQuotaTemplate.InvitedUserMaximumLevel=[string]$tempQuotaTemplate.InvitedUserMaximumLevel


        # Add the element to the QuotaTemplates node as a new child
        $xmlDoc.QuotaTemplates.AppendChild($newQuotaTemplate)
        }
    }

    # Remove Sample node
     $sample = $xmlDoc.QuotaTemplates.QuotaTemplate | Where-Object { $_.Name -eq "Sample" }
     $xmlDoc.QuotaTemplates.RemoveChild($sample)

    # Set date and time when generated
    $now = Get-Date
    $xmlDoc.QuotaTemplates.AsOn = [string]$now

    $xmlDoc.Save("C:\QTforSiteCol.xml")
    # Report completion
    Write-Host -f Green "The quota template information has been saved to C drive - QTforSiteCol.xml."
}

Get-SPQuotaTemplates

No comments:

Post a Comment