Powershell Tips Series Part 2 : Get details of orphans in Content DB and Timer job history by date and time range in SharePoint 2010

Sriram Varadarajan
 
Solution Architect
October 3, 2015
 
Rate this article
 
Views
9480

As a continuations of PowerShell script series, in this blog post , the first script enables to find orphans in SharePoint and the second script helps to find out the timer job history for a specified date and time range in SharePoint 2010

Detecting Orphans for the content DB in sharepoint 2010:

 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".FindOrphanSitesPatch-$LogTime.txt"
 
 if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
 {Add-PSSnapin Microsoft.SharePoint.Powershell}
  
 $dbs = Get-SPContentDatabase
 start-transcript $logfile
 ForEach ($db in $dbs)
 {
     Write-Host “Detecting Orphans for the content DB: ” $db.name 
      
     $db.repair($false)
 }
 stop-transcript
 ########### end of script ################################################
 

 

Retrieve timer job history for a specified time range in sharepoint 2010:

 # Retrieve timer job history for a specified time range
 # Initial settings 
 Add-pssnapin Microsoft.SharePoint.Powershell
 $Wa = Get-SPWebApplication "https://XXX.am.XXX.com"    # Supply the web app url here 
 $From= "8/24/2015 02:00:00 AM"  # mm/dd/yyyy hh:mm:ss 
 $To = "8/24/2015 04:00:00 AM"
 
 # Retrieve all jobs in the time range 
 Write-Host "Listing all timer jobs that have run between $From to $To and storing it in CSV format" -ForeGroundColor Blue 
 $Wa.JobHistoryEntries | Where-Object {($_.StartTime -gt $From) -and ($_.StartTime -lt $To)} | Export-Csv TimerJobHistory.csv –NoType
 
 Write-Host "Done.." -ForeGroundColor Green
 
 # Retrieve all failed jobs in the time range 
 Write-Host "Listing all timer jobs that have failed to run between $From to $To and storing it in CSV format" -ForeGroundColor Red 
 $Wa.JobHistoryEntries | Where-Object {($_.StartTime -gt $From) -and ($_.To -lt $To) -and ($_.Status -ne 'Succeeded')} | Export-Csv FailedTimerJobHistory.csv –NoType
 
 Write-Host "Done.." -ForeGroundColor Green
 

Author Info

Sriram Varadarajan
 
Solution Architect
 
Rate this article
 
Sriram is a Technology Evangelist with 15+ years experience in Microsoft Technologies. He is an enterprise architect working for large pharmaceutical organization which has presence globally with largest Microsoft implementation ...read more
 

Leave a comment