Wednesday, April 29, 2009

Site Collection Inventory with Powershell

In my recent project I was asked to get site collection inventory between various enviornments. Requirement was to get a automated file with certain information of each site. Information that was needed for each site is as below.

Name
Title
Description
Theme
WebTemplate
Author
Created
Modified
Sites
Users
ParentWeb
Url
ServerRelativeUrl
ID

Requirement seemed to be simple but there was no easy answer who what my soluton would be.
Initially when I started to think of a solution various few things that came to my mind including copy and paste through Content and Structure under sitecollection settings, wirting object model code and then it clicked with check what powershell has to offer.

Using power shell I wrote useful script to get all the required information for each site and exported it to a .csv file. Following is the script where $url should be replaced with the actual site collection.

$Path = $pwd.ToString() + "\SiteInformation.csv"
$SiteInfo = Get-SiteInformation $Url $SiteInfo Export-Csv $Path -NoTypeInformation

function Get-SiteInformation ([string]$Url) {
$SPSite = New-Object Microsoft.SharePoint.SPSite($Url) $AllWebs = $SPSite.AllWebs

$AllWebs ForEach {
if ($_.Theme -eq "")
{ $Theme = "Default Theme" }
else
{ $Theme = $_.Theme }

$SiteInformation = New-Object PsObject
$SiteInformation Add-Member -memberType NoteProperty "Name" -Value $_.Name $SiteInformation Add-Member -memberType NoteProperty "Title" -Value $_.Title $SiteInformation Add-Member -memberType NoteProperty "Description" -Value $_.Description $SiteInformation Add-Member -memberType NoteProperty "Theme" -Value $Theme $SiteInformation Add-Member -memberType NoteProperty "WebTemplate" -Value $_.WebTemplate $SiteInformation Add-Member -memberType NoteProperty "Author" -Value $_.Author $SiteInformation Add-Member -memberType NoteProperty "Created" -Value $_.Created $SiteInformation Add-Member -memberType NoteProperty "Modified" -Value $_.LastItemModifiedDate $SiteInformation Add-Member -memberType NoteProperty "Sites" -Value ($_.Webs).Count $SiteInformation Add-Member -memberType NoteProperty "Users" -Value $_.SiteUsers.Count $SiteInformation Add-Member -memberType NoteProperty "ParentWeb" -Value $_.ParentWeb $SiteInformation Add-Member -memberType NoteProperty "Url" -Value $_.Url $SiteInformation Add-Member -memberType NoteProperty "ServerRelativeUrl" -Value $_.ServerRelativeUrl $SiteInformation Add-Member -memberType NoteProperty "ID" -Value $_.ID
$SiteInformation }}

I am excited to expose more about PowerShell and the useful features it brings to the Sharepoint as an platform so keep looking for furture posts. Thanks !

No comments:

Post a Comment