Wednesday 10 December 2014

Trigger Change Log Retention Timer using PowerShell

Param($SiteURL);
Add-PSSnapin Microsoft.SharePoint.PowerShell

function Main($SiteURL)
{
    $wa=Get-SPWebApplication $SiteURL
    Write-Host "Web application URL: " $wa.Url

    [TimeSpan]$OneDay=[timespan]"01.00:00:00"
  
    $wa.ChangeLogRetentionPeriod=$OneDay
    $wa.Update()
    Write-Host -ForegroundColor DarkGreen "Change Log Retention Period reduced to a single day.."
    Write-Host -ForegroundColor DarkGreen "Getting the Changelog timer job..."
    $job = Get-SPTimerJob "job-change-log-expiration" -WebApplication $wa
    Write-Host -ForegroundColor DarkGreen "Running the Changelog timer job..."
    Start-SPTimerJob $job  

    $maxwait = 120
    $currentwait = 0

    $jobName = $job.Name
    Write-Host -NoNewLine "[WAIT] Wait 10 mins to finish the job $jobName"       
    while (($currentwait -lt $maxwait))
      {
        Write-Host -f Green -NoNewLine .
        $currentwait = $currentwait + 1
        Start-Sleep -Seconds 5    
      }
    Write-Host  -f Green "...Done!"   

    $wa.ChangeLogRetentionPeriod=[timespan]"60.00:00:00"
    $wa.Update()
    Write-Host -ForegroundColor DarkGreen "Change Log Retention Period again increased to 60 days.."
    Write-Host -ForegroundColor DarkGreen "Done!!"
}


#--------------------------------------------------------------
# Main Script
#--------------------------------------------------------------
Start-Transcript 

Main $SiteURL

Stop-Transcript
#--------------------------------------------------------------

No comments:

Post a Comment