jan-h
4/10/2017 - 2:43 PM

Cross check the XPM component metadata on the staging website with the SDL Web CMS.

Cross check the XPM component metadata on the staging website with the SDL Web CMS.

# This script loads the HTML source of a page from your staging website, scans it for XPM tags, and verifies which items are out of date.

# The url of the page you want to check
$url = "http://localhost:82/se"

Import-Module Tridion-CoreService
# Configure Core Service module if it was not already persisted
#Set-TridionCoreServiceSettings -HostName "cmshostname" -Version Web-8.1 -Credential (Get-Credential)

$source = Invoke-WebRequest $url

$ErrorActionPreference = "Stop"
$sourceComponentPresentations = ([regex]'<!-- Start Component Presentation: {"ComponentID":"([tcm:0-9/-]+)".+"ComponentModified":"([0-9T:/-]+).+"ComponentTemplateID":"([tcm:0-9/-]+)".+"ComponentTemplateModified":"([0-9T:/-]+).+}').Matches($source)

foreach ($cp in $sourceComponentPresentations)
{
    $sourceComponentId = $cp.Groups[1].Value
    [datetime] $sourceComponentModified = $cp.Groups[2].Value
    $sourceComponentTemplateId = $cp.Groups[3].Value
    [datetime] $sourceComponentTemplateModified = $cp.Groups[4].Value
    Write-Host -ForegroundColor DarkYellow ("Found in source. ComponentId: {0}, ComponentModified: {1}, ComponentTemplateId: {2}, ComponentTemplateModified: {3}" -f $sourceComponentId, $sourceComponentModified, $sourceComponentTemplateId, $sourceComponentTemplateModified) 

    $item = (Get-TridionItem $sourceComponentId)
    [datetime] $cmsComponentModified = $item.VersionInfo.RevisionDate
    $cmsComponentModified = $cmsComponentModified.AddMilliseconds(-$cmsComponentModified.Millisecond) # set miliseconds to 0
    $cmsComponentTitle = $item.Title
    Write-Host -ForegroundColor DarkYellow ("Found in CMS: ComponentId: {0}, ComponentModified: {1}" -f $sourceComponentId, $cmsComponentModified) 

    $item = (Get-TridionItem $sourceComponentTemplateId)
    [datetime] $cmsComponentTemplateModified = $item.VersionInfo.RevisionDate
    $cmsComponentTemplateModified = $cmsComponentTemplateModified.AddMilliseconds(-$cmsComponentTemplateModified.Millisecond) # set miliseconds to 0
    $cmsComponentTemplateTitle = $item.Title
    Write-Host -ForegroundColor DarkYellow ("Found in CMS: ComponentTemplateId: {0}, ComponentTemplateModified: {1}" -f $sourceComponentTemplateId, $cmsComponentTemplateModified) 

    Write-Host -ForegroundColor DarkYellow ("soure and CMS component modified dates match: {0}" -f ($sourceComponentModified -eq $cmsComponentModified))
    Write-Host -ForegroundColor DarkYellow ("soure and CMS component template modified dates match: {0}" -f ($sourceComponentTemplateModified -eq $cmsComponentTemplateModified))
    
    if(($sourceComponentModified -ne $cmsComponentModified) -or ($sourceComponentTemplateModified -ne $cmsComponentTemplateModified))
    {
        
        Write-Host ("Component Presentation not in sync! ComponentId: {0}, ComponentTemplateId: {1}" -f $sourceComponentId, $sourceComponentTemplateId) 
        Write-Host ("Component: {0}" -f $cmsComponentTitle) 
        Write-Host ("ComponentTemplate: {0}" -f $cmsComponentTemplateTitle) 
        Write-Host ("Found in source. ComponentId: {0}, source component modified: {1}, CMS component modified: {2}" -f $sourceComponentId, $sourceComponentModified, $cmsComponentModified) 
        Write-Host ("Found in source. ComponentTemplateId: {0}, source component modified: {1}, CMS component modified: {2}" -f $sourceComponentTemplateId, $sourceComponentTemplateModified, $cmsComponentTemplateModified) 
    }
}