Ads

Thursday, 6 April 2017

PowerShell to get all site collections details like URL, Root Template, Contnet DB in CSV file

Below is the full script with all functions that can be used to retrieve site collection details like URL, Site ID, Template name, Size of site, Content DB. Follow the steps
  1. Copy all below contents in one text file 
  2. Provide the web application URL
  3. Change the extension of text file from .txt to .ps1 file. (Like - GetAllSites.ps1
  4. Execute this .ps1 file (GetAllSites.ps1) in SharePoint management shell

#Start:  Custom Variable Entry
$webAppUrl = "Your Web Application URL Here"
#End: Custom Variable Entry

if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.SharePoint.Powershell
}
function Execute-GetAllSites
{
    $WebApplication = Get-SPWebApplication -Identity $webAppUrl
   
    if ($WebApplication -ne $null)
     {
        foreach ($SiteCollection in  $WebApplication.Sites)
        {
            Try
            {
                if ($SiteCollection -ne $null)
                {                   
                    $SiteTitle = $SiteCollection.RootWeb.Title
                    Write-Host "$($SiteTitle.Substring(0,1))"  -NoNewLine
                   
                    $RootWeb = $SiteCollection.RootWeb
                    $WebTemplate = $RootWeb.WebTemplate
                   
                    #$Size = [string]$SiteCollection.Usage.Storage/1000000
                    $SizeinMB = [System.Math]::Round((($SiteCollection.Usage.Storage)/1MB),2)
                   
                    $contentDB = $SiteCollection.ContentDatabase.Name
                   
                    Write-Excel-SiteCollections $LogFile_Excel $SiteCollection.Url $SiteCollection.ID $WebTemplate $SizeinMB $contentDB
                   
                    
                    $SiteCollection.Dispose()      
                }
            }
            Catch {}                      
        }
       
        Write-Host "----- Complete -----"
     }   
}
function Get_DateTime()
{
    $LogFileDayF = Get-Date -Format "s";
    $LogFileDateTime = $LogFileDayF.Replace(":","_")                          
    return $LogFileDateTime;
}
Function CreateExcelLogFile($LogFileName)
{   
    $logFilePath ="";
    $Date_Time = Get_DateTime   
    Try
    {       
        #$JobLogPath = $CurrentPath | Join-Path -ChildPath ("Log_AdminReport")
        $JobLogPath = $CurrentPath
    
        #Create folder path if not exists
        if(!(Test-Path $JobLogPath))
        {
           New-Item -Path $JobLogPath -ItemType directory
        }
        $JobLogPath = $JobLogPath + "\" + $LogFileName + "_" + $Date_Time + ".csv"
    }
    Catch
    {}     
    return $JobLogPath
}
function Write-Excel-SiteCollections($logFilePath,$SiteCollectionUrl,$SiteCollectionID, $WebTemplate, $Size, $contentDB)
{   
    $SiteCollectionUrl = $SiteCollectionUrl.Replace(',','%2C')
   
    if((Test-Path $logFilePath) -eq $false)
    {
        New-Item $logFilePath -type file | Out-Null
        Add-Content -Path $logFilePath -Value 'Site Collection URL,Site Collection ID,Root Web Template, Size in MB, Content DB Name'
    }

    $msg = "$($SiteCollectionUrl),$($SiteCollectionID),$($WebTemplate),$($Size),$($contentDB)"
    Add-Content -path $logFilePath -value $msg
}
function Get-ScriptDirectory
{
  $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  Split-Path $Invocation.MyCommand.Path
}
#----------- Main Start point that execute ----------
$CurrentPath = Get-ScriptDirectory
$LogFile_Excel = CreateExcelLogFile 'AllSiteDetails'
Execute-GetAllSites

Result Output in CSV file


Some recommendations for you:
Powershell to create & write result output in CSV file as per need  
Powershell script to read CSV file to do any automation


No comments:

Post a Comment

Ads