How to: Programmatically read the ChangeLog in PowerShell

Hi,

When I was working to get powershell scripts from MSDN samples to get/set information, I created too one for reading the changelog. It was a bit tricky to build, but I manage to port Querying for specific changes to PowerShell.

You can review the changelog API in the Microsoft SharePoint Developer Documentation Team Blog and MSDN (previous link):

We wanted to monitor which changes were been read by the crawl process and been able to filter specific changes.

## SharePoint Reference [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search.Administration") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")

function global:Get-ChangeLog($url, $minutes, $changetype)
{
 trap [Exception] {
  write-error $("ERROR: " + $_.Exception.GetType().FullName);
  write-error $("ERROR: " + $_.Exception.Message);
 
  continue;   
 }

 $s = new-Object Microsoft.SharePoint.SPSite($url);
 $startTime = [System.DateTime]::Now.Subtract([System.TimeSpan]::FromMinutes($minutes));
 
 $db_id = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("get_Id");
 $result_id = $db_id.Invoke($s.ContentDatabase, "instance,public", $null, $null, $null);
 
 $db_CurrentChangeToken = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("get_CurrentChangeToken");
 $result_CurrentChangeToken = $db_CurrentChangeToken.Invoke($s.ContentDatabase, "instance,public", $null, $null, $null);
 
 $startToken = New-Object Microsoft.SharePoint.SPChangeToken([Microsoft.SharePoint.SPChangeCollection+CollectionScope]::ContentDB, $result_id.ToString(), $startTime);
 
 $changeQuery = New-Object Microsoft.SharePoint.SPChangeQuery($False, $False);
 $changeQuery.User = $True;
 $changeQuery.ContentType = $True;
 $changeQuery.Add = $True;
 $changeQuery.Delete = $True;
 $changeQuery.Field = $True;
 $changeQuery.File = $True;
 $changeQuery.Folder = $True;
 $changeQuery.Group = $True;
 $changeQuery.GroupMembershipAdd = $True;
 $changeQuery.GroupMembershipDelete = $True;
 $changeQuery.Item = $True;
 $changeQuery.List = $True;
 $changeQuery.Move = $True;
 $changeQuery.Rename = $True;
 $changeQuery.Site = $True;
 $changeQuery.SystemUpdate = $True;
 $changeQuery.Update = $True;
 
 $changeQuery.ChangeTokenStart = $startToken;
 $changeQuery.ChangeTokenEnd = $result_CurrentChangeToken;
 
 $db_GetChanges = [Microsoft.SharePoint.Administration.SPContentDatabase].getmethod("GetChanges", [Microsoft.SharePoint.SPChangeQuery]);
 $result_GetChanges = $db_GetChanges.Invoke($s.ContentDatabase, "instance,public", $null, $changeQuery, $null);

 write-Output $result_GetChanges | Where-Object { $_.ChangeType -eq $changetype };

 $s.Dispose();
}

Get-ChangeLog -url https://your_site_url -minutes 120 -changetype Add

Take into account that the time will get changes based on UTC

Bye!