Tuesday, 7 July 2015

Powershell Get Size SharePoint Sites or Webs

param(
[Parameter(Mandatory = $true, ValurFromPipelineByPropertyName=$true) ][System.String] $env,
[Parameter(Mandatory= $true, ValueFromPipelineByPropertyName = $true)][System.String] $outputFile
)

$snapin = Get_PSSnapin | Where-Object{$_.Name -eq "Microsoft.SharePoint.PowerShell"}
if($snapin -eq $null)
{
Write-Host "Adding SharePoint Snapin..."
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$table = New-Object System.Data.DataTable "WebSite Size Table"
$title = New-Object System.Data.DataTable Title,([system.string])
$url = New-Object System.Data.DataTable URL, ([system.string])
$size = New-Object System.Data.DataTable size, ([system.string])
$table.Columns.Add($title)
$table.Columns.Add($url)
$table.Columns.Add($size)

function GetSpWebSize($Web)
{
 [long]$subtotal = 0
    foreach ($folder in $Web.Folders)
    {
        $subtotal += GetFolderSize -Folder $folder
    }
  
    return $subtota
}

function GetSubWebSizes ($Web)
{
    [long]$subtotal = 0
    foreach ($subweb in $Web)
    {
        [long]$webtotal = 0
        foreach ($folder in $subweb.Folders)
        {
            $webtotal += GetFolderSize -Folder $folder
        }
       
        $subtotal += $webtotal
       
    }
    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
}


if($env  -eq "QA")
{
 $File = "AllQASites.csv"
}
elseif($env -eq "Live")
{
 $File = "AllLiveSites.csv"
}
else
{
Write-Host "Please  choose the environment either QA or Live!"
return
}

$getContent = Import-Csv -path $File

Foreach($rowin in $getContent)
{
  Write-Host "Fetching details of " $rowin.Site -ForegroundColor Green
if($rowin.Type -eq "Site")
{
 $WebSite = Get-SPSite -Identity $rowin.URL -ErrorAction SilentlyContinue
}
elseif($rowin.Type -eq "Web")
{
$WebSite = Get-SPWeb -Identity $rowin.URL -ErrorAction SilentlyContinue
}
else
{
Write-Host "Please choose type either Site or web!"
return
}
try{
if($WebSite -ne $null)
{
 $row = $table.NewRow()
if($rowin.Type -eq "Site")
{
 $row.Title = $WebSite.RootWeb.Title
$row.URL = $WebSite.URL
$row.size = $WebSite.usage.storage/1MB
}
else if($rowin.Type -eq "Web")
{
 $row.Title = $WebSite.Title
$row.URL = $WebSite.URL

[long]$total = 0

$total += GetSPWebSize -Web $WebSite.URL
$total += GetSubWebSizes -Web $site.Url

            $Folder = $site.Folders
            foreach ($file in $Folder.Files)
            {
                $total += $file.Length;
            }
            foreach ($fd in $Folder.SubFolders)
            {
        $total += GetFolderSize -Folder $fd
                }

$row.size = ($total/1024)/1024
}
$table.Rows.Add($row)
$table.AcceptChanges()
$WebSite.Dispose()
}
}
Catch{
 Write-Host $_.Exception.Message -ForegroundColor Red
}
}

$table | Export-Csv -Path $outputFilePath -NoTypeInformation

$endDate = Get-Date
Write-Host "script completed at" $endDate -ForegroundColor Yellow


#Please write your queries on "Arpit_Mohan@outlook.com"

No comments:

Post a Comment