Wednesday, November 25, 2015

Last Access Date or Last Modified Date


#############################################

# Title: LastModifiedDate.ps1      
# Created By : Suresh Kumar Durairaj
#############################################

#region Addins
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)            
{          
 Add-PSSnapin Microsoft.SharePoint.PowerShell          
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Publishing")
[System.Reflection.Assembly]::LoadWithPartialName("System")
[System.Reflection.Assembly]::LoadWithPartialName("System.IO")

#endregion

#region Log
$fileName = "LastAccessDateScript"
$logdatetime=Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$logFileName = $fileName +"_" +$logdatetime+ "_Log.txt"  
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$logPath = $directoryPath + "\" + $logFileName
$CSVPath= $directoryPath + "\" +$fileName +"_" +$logdatetime+".csv"
$isLogFileCreated = $False  
 
function Write-Log([string]$logMsg)
{  
    if(!$isLogFileCreated){  
        Write-Host "Creating Log File..."  
        if(!(Test-Path -path $directoryPath))
        {
            Write-Host "Please Provide Proper Log Path" -ForegroundColor Red  
        }  
        else  
        {  
            $script:isLogFileCreated = $True  
            Write-Host "Log File ($logFileName) Created..."  
            [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)  
            Add-Content -Path $logPath -Value $logMessage  
        }  
    }  
    else  
    {  
        [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)  
        Add-Content -Path $logPath -Value $logMessage  
    }  
}  

#endregion


# Declare an array to collect our result objects
$resultsarray =@()


$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$farmWebServices = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
foreach ($farmWebService in $farmWebServices) {
  foreach ($webApplication in $farmWebService.WebApplications) {
Write-Log( "WebApplication Url:: "+ $webApplication.Name)
Write-Log("=======================================================")
    foreach ($site in $webApplication.Sites)
    {

$contactObject = new-object PSObject
$sitecollectionOwners=$null
$sitecollectionOwners = $site.RootWeb.SiteAdministrators
     $scas = ""
 if($sitecollectionOwners)
 {
     foreach ($siteAdmin in $sitecollectionOwners.Users)
     {
       $scas = $siteAdmin.LoginName + "; " + $scas
     }
 }
 if([string]::IsNullOrEmpty($scas) )
 {
  $scas=$site.Owner.Name
 }
#Logging the data
Write-Log( "Site Url:: "+ $site.Url)
Write-Log( "Site Owner::" + $scas)
        Write-Log( "Modified Date::"+ $site.LastContentModifiedDate)



        # Add our data to $contactObject as attributes using the add-member commandlet
$contactObject | add-member -membertype NoteProperty -name "Site Url" -Value $site.Url
$contactObject | add-member -membertype NoteProperty -name "Site Owner" -Value $scas
$contactObject | add-member -membertype NoteProperty -name "Modified Date" -Value $site.LastContentModifiedDate
$resultsarray += $contactObject    
 

 if($site.AllWebs.Count -gt 0)
 {
 foreach ($subsite in $site.AllWebs)
     {
  $siteowner=""
           $siteOwners = $subsite.AssociatedOwnerGroup
           if($siteOwners)
           {
               foreach ($owner in $subsite.Users)
               {

$siteowner=$owner.Name+";"+$siteowner

               }
           }
 if([string]::IsNullOrEmpty($siteowner) )
 {
  $siteowner=$site.Owner.Name
 }

#Logging the data
Write-Log( "Site Url:: "+ $site.Url)
Write-Log( "Site Owner" + $scas)
       Write-Log( "Modified Date"+ $site.LastContentModifiedDate)

$contactObject = new-object PSObject
         
# Add our data to $contactObject as attributes using the add-member commandlet
$contactObject | add-member -membertype NoteProperty -name "Site Url" -Value $subsite.Url
$contactObject | add-member -membertype NoteProperty -name "Site Owner" -Value $siteowner
$contactObject | add-member -membertype NoteProperty -name "Modified Date" -Value $subsite.LastItemModifiedDate
# Save the current $contactObject by appending it to $resultsArray ( += means append a new element to ‘me’)
$resultsarray += $contactObject                      

 }


 }



 $resultsarray| Export-csv $CSVPath -notypeinformation
      $site.Dispose()
Write-Log("=======================================================")
    }
  }
}

#region Remove addins
Remove-PsSnapin Microsoft.SharePoint.PowerShell
#endregion

2 comments:

  1. Hi Suresh,

    Thank you for the script. Where do i need to add the output path in the script.

    Thanks,
    Naga

    ReplyDelete
  2. This won't work if you have published content types or there are changes to users in the site(s) where the user information list is being updated.

    ReplyDelete