Friday, February 20, 2015

PowerShell script for updating values in Individual Quota for each site collection in SharePoint 2013

The below script is used to update values for Individual Quota as desired by user for each site collection under a respective web application as follows:

Just for your information:

1 KB = 1024 bytes
1 MB = 1024 KB
1 GB = 1024 MB
1 TB = 1024 GB
1 byte= 8 bits

$MinQuota = "50GB",
$Bytes="53687091200",
$WebAppUrls = @("https://test1.com/", "https://test2.com/", "https://test3.com/")


$Location = get-location;
$date = Get-Date
$outFile = ".\Quota_" + $date.ToFileTime() + ".log"

Write-Host "Site Collection Quota Script"
Write-Host ("Setting Min Quota: " + ($MinQuota / 1GB) + " GB")
Write("Setting Min Quota:" + ($MinQuota / 1GB) + " GB" + "`r`n") >> $outFile
Write-Host


ForEach($WebAppUrl in $WebAppUrls)
{
   Write-Host "Reading webapplication $webApp"

   $webApp = Get-SPWebApplication -Identity $WebAppUrl
 
   Write("Reading web application $webApp") >> $outFile
   ForEach($site in $webApp.Sites)
   {
       try
       {
        if($site.ReadOnly -ne $null)
        {
$siteUrl = $site.Url
        $siteQuota = $site.Quota.StorageMaximumLevel
     
        Write-Host " $siteUrl... " -NoNewLine
        Write("Looking for site URL " + $site.Url) >> $outFile
     
        if($siteQuota -ne $null)
        {
            Write-Host ("" + ($siteQuota / 1MB) + "MB") -NoNewline
            Write("Site Quota :" + ($siteQuota / 1MB) + "MB") >> $outFile
        }
        else
        {
            Write-Host "SiteQuota is null" -NoNewline
            Write("SiteQuota is null") >> $outFile
        }
        if($siteQuota -ne $null -and $siteQuota -ne 0 -and $siteQuota -lt $MinQuota)
        {
            Write-Host ("The value is setting to " + ($MinQuota/ 1GB) + " GB")
            Write("The value is setting to " + ($MinQuota/ 1GB) + " GB") >> $outFile
            $site.Quota.StorageMaximumLevel =  $Bytes
            Write("" + ($site.Quota.StorageMaximumLevel)) >> $outFile
         
        }
        else
        {
            Write-Host "Dont set as site quota is either empty or zero or less than MinQuota"
            Write("Dont set as site quota is either empty or zero or less than MinQuota") >> $outFile
         
        }
 $site.Dispose()
}
   }
   catch
{

                Add-Content $outFile -Value "Error occured - "
                Add-Content $outFile -Value $_.Exception
                Write-Host "Error occurred -  : $_.Exception" -ForegroundColor Red `r`n

}

}

Write-Host ""
Write("`r`n") >> $outFile
}
Write-Host
Write-Host "The script executed successfully" -ForegroundColor Yellow
Write("The script executed successfully") >> $outFile
Write-Host