locked
Powershell to get size for site collection and "subsites" does not matches the size as per OOTB storage metrics-what is missing RRS feed

  • Question

  • <# Commented out for later use.
     Description: Script used to Get the stats for the given site collection                           
                                                               
     
    #>
    
    param( 
        [string] $url= "https://www.dev6888.snp-dev.kktet.com/sites/Enk", #$(throw "No URL parameter! Usage: GetUseageSPSitePerMonth.ps1 [site collection url] [Year][Report Location]"),
        [string] $year = "2015", #$(throw "No Year parameter! Usage: GetUseageSPSitePerMonth.ps1 [site collection url] [Year][File Path]"),
    	[string] $cSVFilePath = "D:\Sudhanshu-USIT\EnCreport2015KB_2.csv"  #$(throw "No FilePath parameter! Usage: GetUseageSPSitePerMonth.ps1 [site collection url] [Year][CSV File Path]")
        
    )
    
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }
    function Get-Stats {
            $createdDate =$((Get-Date).ToString())  #Get-Date -format G       
    		$dates = ($year +"-01-01" ),($year +"-02-01") ,($year +"-03-01"),($year +"-04-01"),($year +"-05-01"),($year +"-06-01"),($year +"-07-01"),($year +"-08-01"),($year +"-09-01"),($year +"-10-01"),($year +"-11-01"),($year +"-12-01")
    		$header = "URL,Name,ContentDB,DBSize in GB,SiteSize in KB,January Hits, January Unique Visitor,February Visitor,February Unique Visitor,March Visitor, March Unique Visitor,April Hits, April Unique Visitor,May Hits,May Unique Visitor,June Hits,June Uunique Visitor,July Hits,July Unique Visitor,August Hits,August Unique Visitor,September Hits, September Unique Visitor,October Hits,October Unique Visitor,November Hits,November Unique Visitor,December Hits,December Unique Visitor,Created Date"
    				
    		$location = $cSVFilePath| split-path
    		$tempTxtFile = $location + "\templog"  +".txt"
    		$header | Out-File $tempTxtFile 
    		
    		$searchApp = Get-SPEnterpriseSearchServiceApplication
    	    $site = Get-SPSite $url
    
    		$rootresult = $searchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$site.ID,[System.Guid]::Empty)
    		$ContentDBSizeInKB = $site.Usage.Storage
            $ContentDBSizeInGB = $SizeInKB/1024/1024/1024
    
            #$web = Get-SPWeb $site
        [long]$total = 0
       # $total += GetWebSize -Web $web
        #$total += GetSubWebSizes -Web $web
        $totalInkb = ($total/1024)
        $totalInMb = ($total/1024)/1024
        #$totalInMb = "{0:N2}" -f $totalInMb
        $totalInGb = (($total/1024)/1024)/1024
       # $totalInGb = "{0:N2}" -f $totalInGb
    
    		[string]$statspersite = $site.URL + "Over All Hits -" +","+ $site.Name+","+$site.ContentDatabase.Name+","+$ContentDBSizeInGB+","+$totalInkb
    		foreach ($datString in $dates) {
    			
    			$date = Get-Date $datString
    			[System.Int32]$hits =0
    			[System.Int32]$uniqueVisitor =0
    			
    		    $month =$date.Month
    			try {
    				$output = $rootresult.GetMonthlyData($date,[REF]$hits,[REF]$uniqueVisitor) 
    			} catch {	}
    			
    			$statspersite += "," + [System.string]$hits +"," +[System.string]$uniqueVisitor
    
    		}
            $statspersite+=","+""
    		$statspersite | Out-File $tempTxtFile -Append
    		$statspersite
    		foreach($web in $site.AllWebs)
    		{
                [long]$total = 0
                $total += GetWebSize -Web $web
                $total += GetSubWebSizes -Web $web
                $totalInkb = ($total/1024)
                $totalInMb = ($total/1024)/1024
                #$totalInMb = "{0:N2}" -f $totalInMb
                $totalInGb = (($total/1024)/1024)/1024
                #$totalInGb = "{0:N2}" -f $totalInGb
    
    			$result = $searchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$site.ID,$web.ID)
                $ContentDBSizeInKB = $web.Usage.Storage
                $ContentDBSizeInGB = $SizeInKB/1024/1024/1024
    
    			[string]$statsperWeb = $web.URL+"," +$web.Name+","+$web.ContentDatabase.Name+","+$ContentDBSizeInGB+","+$totalInkb
    			foreach ($datString in $dates) {
    				
    				$date = Get-Date $datString
    				[System.Int32]$hits = 0
    				[System.Int32]$uniqueVisitor = 0
    
    			    $month =$date.Month
    				try {
    					$output = $result.GetMonthlyData($date,[REF]$hits,[REF]$uniqueVisitor) 
    				} catch {	}
    				
    				$statsperWeb += "," + [System.string]$hits +"," +[System.string]$uniqueVisitor
    			}
                $statsperWeb+=","+$web.Created
    			$statsperWeb | Out-File $tempTxtFile -Append
    			$statsperWeb
    		}
    		Import-Csv -Delimiter ',' -Path $tempTxtFile | Export-Csv $cSVFilePath -NoTypeInformation	
    }
    function GetWebSize ($Web)
    {
        [long]$subtotal = 0
        foreach ($folder in $Web.Folders)
        {         
            $subtotal += GetStorageMetrics -Folder $folder
        }
        foreach($recycleBinItem in $web.RecycleBin)
            {
               $websize += $recycleBinItem.Size;
            }
        $subtotal+=$websize
        write-host "Site" $Web.Title "is" $subtotal "KB"
        return $subtotal
    }
    
    function GetSubWebSizes ($Web)
    {
        [long]$subtotal = 0
        foreach ($subweb in $Web.GetSubwebsForCurrentUser())
        {
            [long]$webtotal = 0
            foreach ($folder in $subweb.Folders)
            {
                $webtotal += GetStorageMetrics -Folder $folder            
            }        
           write-host "Site" $subweb.Title "is" $webtotal "Bytes"
            $subtotal += $webtotal
            $subtotal += GetSubWebSizes -Web $subweb
        }
        return $subtotal
    }
    
    function GetFolderSize ($Folder)
    {
        [long]$folderSize = 0  
        foreach ($file in $Folder.Files)
        {
            $folderSize += $file.Length;
        }
        foreach ($fd in $Folder.SubFolders)
        {
            $folderSize += GetFolderSize -Folder $fd
        }
        return $folderSize
    }
    
    Function GetStorageMetrics($Folder)
    {  
        $TotalSize=0
        #Get the size of each file in the folder
        foreach ($File in $Folder.Files)
        {
            #Get File Size
            $FileSize = $File.TotalLength
             
            $VersionSize=0
            #Get the Versions Size
            foreach ($FileVersion in $File.Versions)
            {
                $VersionSize += $FileVersion.Size
            }
            $TotalSize += $VersionSize+ $FileSize 
        }
         
     
          #Get Files in Sub Folders
            foreach ($SubFolder in $Folder.SubFolders)
            {  
             if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.
                 {
         #Call the function recursively for the subfolders
                    $TotalSize += GetStorageMetrics($SubFolder)         
                 }
            }
             
            #Return the object
         return $TotalSize
    }
    Get-Stats
    

    Hello All,

    We created a report by powershell which gives yearly hit count,sites,subsites,created date and size of the site.

    We found, sites size is not matching exactly with the OOTB storage metrics option.

    What is the missing things in this script?

    Can we do something with OOTB page to get the size (page: _layouts/15/storman.aspx)On this page why export option is not there could not understand.

    Any help will be appreciated.


    sudhanshu sharma Do good and cast it into river :)


    Wednesday, December 13, 2017 5:53 AM

All replies