Thursday, November 13, 2014

Get All Farm Information with Web application,Site Collection, no of sub sites/webs ,content db name and size with powershell

Add-PSSnapin Microsoft.SharePoint.Powershell
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
foreach ($websvc in $websvcs)
{
    foreach ($webapp in $websvc.WebApplications)
    {
        echo $webapp.Name
        echo $webapp.Url
        #give the path where .csv file will be saved
          $path = "D:\mycsvfolder\Test\"+$webapp.Name+"_Webapplication"+".csv"
             $csv = "WebApplication,SiteCollection,Sub Sites,Content Database,Size of Site Collection`r`n"
         
                 $fso = new-object -comobject scripting.filesystemobject
$file = $fso.CreateTextFile($path,$true)
   $file.write($csv)
  try{    
         Write-Host $webapp.Sites
         Write-Host $webapp.Sites.Count
          foreach ($site in $webapp.Sites)
         {
            echo $site.HostName $site.Url $site.LastContentModifiedDate $site.ContentDatabase.Name $site.WebApplication.Name
         try{
       
         Write-Host $site.AllWebs
             Write-Host "Count:"$site.AllWebs.Count
         
                 #echo $web.Title $web.Url $web.Created $web.LastItemModifiedDate $web.WebTemplate $web.Site.
           
              #$urlweb=$web.Url
         
                       $csv =$webapp.Url+","+$site.Url+","+$site.AllWebs.Count+","+$site.ContentDatabase.Name+","+$site.Usage.Storage+ "`n";
           $file.write($csv)
     
           
           
                         }
                        catch
                        {
                              $csv =$webapp.Url+","+$site.Url+","+"No access"+","+$site.ContentDatabase.Name+","+$site.Usage.Storage+ "`n";
           $file.write($csv)
                        }
                       
                       
             }}
             catch{
             Write-Host "Webapplication does not have site collection"
             }
        }

#$file.close()
       
    }