SharePoint 2007 - Content Audit: Print last modified & ownership of all sites

If you are planning to migrate from SharePoint 2007 to 2010 or to 2013, then first thing you should do is to audit your content to determine what’s being used & thus should be migrated.
I wrote the below script to determine the site collections modified recently & their site owners. I started with “LastModifiedDate” property of SPWeb object but quickly realised that search crawler updates this property every time a crawl takes place. Thus the solution is to go through each list within the site collection & get the most recent modified date. This gives us more accurate figure we can present to business owners.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")


# Get the last updated date for a site collection
# Iterate through all webs & all lists of the site collection and returns the most recent updated date
function getLastestChangeDate([Microsoft.SharePoint.SPSite]$site){
    
    $datesArray = @()
    foreach($web in $site.AllWebs){
        
        foreach($list in $web.Lists){
            if($list.Title -ne "User Information List" -and $list.Title -ne "Master Page Gallery"){
                $datesArray += $list.LastItemModifiedDate
            }
        }
        
        $web.Dispose()
    }
     
    $datesArray = $datesArray | Sort-Object;
    $arrayLength = $datesArray.GetUpperBound(0)
    
    if($arrayLength -ge 0){
        $lastUpdated = $datesArray[$arrayLength] 
    }
    return $lastUpdated
}



# Print last modified date & ownership for all site collections within the web application
function PrintSiteDetails(){

    $siteUrl = ""
    $mainSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
    $webApp = $mainSite.WebApplication

    foreach($site in $webApp.Sites){

        $rootWeb = $site.RootWeb
        
        ## Cannot use LastModifiedDate property of SPWeb because it's reset by search crawler
        #"URL: " + $rootWeb.Url + ", Created By:" + $rootWeb.Author + ", Created: " + $rootWeb.Created + ", Last Modified: " + $rootWeb.LastItemModifiedDate
        #"Template: " + $rootWeb.WebTemplate +", Template ID: " + $rootWeb.WebTemplateId
        #"Site Admins: " + $rootWeb.SiteAdministrators
        #"Site Owners: " + $rootWeb.AssociatedOwnerGroup.Users
        #"Usage: " + $site.Usage.Storage / 1024 /1024 / 1024
        
        $siteOwners = ""
        
        foreach($user in $rootWeb.AssociatedOwnerGroup.Users){
            
            if($user.LoginName -ne $null){
                $loginName = $user.LoginName.toLower() -replace "\\", ""
                
                if($loginName -ne "" -and $loginName -ne ""){    
                    $siteOwners += $user.Name + ";" #+ "`n" 
                }
            }
        }
        
        
        # Last Updated Date
        # $web.LastItemModifiedDate is reset by search crawler so not useful
        $lastUpdated = getLastestChangeDate($site)
        
        # Get Content Database Name
        $contentDB = $site.ContentDatabase
        $propContentDBName = [Microsoft.SharePoint.Administration.SPContentDatabase].GetProperty("Name")
        $contentDBName = $propContentDBName.GetValue($contentDB, $null)
        
        # Size in GB
        $sizeInGB = $site.Usage.Storage / 1GB
        $sizeInGB = "{0:N3}" -f $sizeInGB
        
        $data = @{
            "Site Title" = $rootWeb.Title
            "Site URL" = $rootWeb.Url
            "Site Owners" = $siteOwners
            "Storage (GB)" =  $sizeInGB
            "Subsites Count" = $site.AllWebs.Count - 1
            "Content DB" = $contentDBName
            "Web Template" = $rootWeb.WebTemplate + " (" + $rootWeb.WebTemplateId + ")"
            "Created By" = $rootWeb.Author
            "Created" = $rootWeb.Created
            "Last Modified" = $lastUpdated
        }
        New-Object PSOBJECT -Property $data
        
        
        $rootWeb.Dispose()
        $site.Dispose()
    }


    #$webApp;
    $mainSite.Dispose();
}


# Print to GridView
PrintSiteDetails | OUT-GRIDVIEW 

# Export to CSV
#PrintSiteDetails | Select-Object "Site Title", "Site Url" , "Storage (GB)", "Web Template", "Subsites Count", "Content DB", "Created", "Last Modified", "Site Owners" | Export-Csv -NoTypeInformation -Path "C:\SiteInventory.csv"


Comments

Popular posts from this blog

Hybrid Apps using Cordova/PhoneGap: Ditch JQuery Mobile

SharePoint 2013 Search Service Activation Error

VMWare Workstation - Assign a static IP address