Skip to content

Instantly share code, notes, and snippets.

@trnktms
Created June 20, 2019 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trnktms/4b14522f67b55d424b1795691c2321c0 to your computer and use it in GitHub Desktop.
Save trnktms/4b14522f67b55d424b1795691c2321c0 to your computer and use it in GitHub Desktop.
$sourceItems = Get-ChildItem -Recurse -Path "/sitecore/content/"
$query = "/sitecore/content//*[@@id!='{0}' and @OldUrl='{1}']"
$queryWithContains = "/sitecore/content//*[@@id!='{0}' and (@OldUrl='{1}' or contains(@OldUrl, '{2}'))]"
$foundDuplicatesCollection = New-Object System.Collections.ArrayList
Write-Host "SourceItemParentPath, SourceItemPath, SourceItemId, FoundDuplicateParentPath, FoundDuplicatePath, FoundDuplicateId, OriginalUrl, FoundDuplicateUrl" -ForegroundColor Green
foreach ($sourceItem in $sourceItems) {
$originalUrl = $sourceItem["OldUrl"]
if ($originalUrl -eq "") {
#Write-Host "Without original url - " $sourceItem.Paths.FullPath -ForegroundColor Magenta
continue
}
$builtQuery = ""
# change this condition if you need a different or remove it completely
# this condition checks links like old-domain.org?id=123456789
if ($originalUrl -like '*?id=*') {
$uri = New-Object -TypeName System.Uri -ArgumentList $originalUrl
$id = [System.Web.HttpUtility]::ParseQueryString($uri.Query)["id"]
$builtQuery = [string]::Format($queryWithContains, $sourceItem.ID, $originalUrl, "?id=" + $id)
}
else {
$builtQuery = [string]::Format($query, $sourceItem.ID, $originalUrl)
}
#Write-Host $builtQuery
$foundDuplicates = Get-Item -Path "master:" -Query $builtQuery
foreach ($foundDuplicate in $foundDuplicates) {
$reverseResults = $foundDuplicatesCollection | Where-Object { $_.SourceItemId -eq $foundDuplicate.ID -and $_.FoundDuplicateId -eq $sourceItem.ID }
if ($reverseResults.Count -gt 0) {
continue
}
$result = New-Object System.Object
$result | Add-Member -MemberType NoteProperty -Name "SourceItemParentPath" -Value $sourceItem.Parent.Paths.FullPath
$result | Add-Member -MemberType NoteProperty -Name "SourceItemPath" -Value $sourceItem.Paths.FullPath
$result | Add-Member -MemberType NoteProperty -Name "SourceItemId" -Value $sourceItem.ID
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicateParentPath" -Value $foundDuplicate.Parent.Paths.FullPath
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicatePath" -Value $foundDuplicate.Paths.FullPath
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicateId" -Value $foundDuplicate.ID
$result | Add-Member -MemberType NoteProperty -Name "OriginalUrl" -Value $originalUrl
$result | Add-Member -MemberType NoteProperty -Name "FoundDuplicateUrl" -Value $foundDuplicate["OldUrl"]
$foundDuplicatesCollection.Add($result) | Out-Null
Write-Host $result.SourceItemParentPath"," -ForegroundColor DarkYellow -NoNewline
Write-Host $result.SourceItemPath"," -ForegroundColor DarkYellow -NoNewline
Write-Host $result.SourceItemId"," -ForegroundColor DarkYellow -NoNewline
Write-Host $result.FoundDuplicateParentPath"," -ForegroundColor Cyan -NoNewline
Write-Host $result.FoundDuplicatePath"," -ForegroundColor Cyan -NoNewline
Write-Host $result.FoundDuplicateId"," -ForegroundColor Cyan -NoNewline
Write-Host $result.OriginalUrl"," -ForegroundColor DarkYellow -NoNewline
Write-Host $result.FoundDuplicateUrl -ForegroundColor Cyan
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment