In above script there was a problem in below logic
$renderingId.ToString().TrimEnd($seedSuffix)
This will truncate the last three character if have same number before and after underscore like below example:
$renderingId = "5c43afaf-958c-4d79-8395-41ad5aefd371_1"
and $seedSuffix = "_1"
then output of $renderingId.ToString().TrimEnd($seedSuffix)
will be "5c43afaf-958c-4d79-8395-41ad5aefd37"
I have updated the script to handle this issue which is working fine now.
# specify your path here. It is most likely be page templates and page content
$startPath = "/sitecore/content"
Get-ChildItem -Path $startPath -Recurse | ForEach-Object {
$item = $_;
Get-Rendering -Item $_ -FinalLayout | ForEach-Object {
$rendering = $_;
$matches = [regex]::Matches($_.Placeholder,'(?<guid>_[\d\w]{8}\-(?:[\d\w]{4}\-){3}[\d\w]{12})(?<seed>_?\d?)')
if ($matches.Success) {
Write-Host "Match found in item - [$($item.Paths.FullPath)]"
Write-Host "Old Placeholder - [$($rendering.Placeholder)]"
$newPlaceholder = $rendering.Placeholder
$matches | ForEach-Object {
$match = $_
$renderingId = $match.Value
if (!([string]::IsNullOrEmpty($match.Groups["seed"])))
{
$seedSuffix = $match.Groups["seed"].ToString()
$seedValue = $seedSuffix.Trim("_")
# trim seed suffix off end
if($renderingId.ToString().lastIndexOf($seedSuffix) -ge 0)
{
$unsuffixedRenderingId = $renderingId.ToString().Substring(0, $renderingId.ToString().lastIndexOf($seedSuffix))
}
else
{
$unsuffixedRenderingId = $renderingId.ToString()
}
# add seed value to end
$newPlaceholder = $newPlaceholder.Replace($renderingId, "{$($unsuffixedRenderingId.ToUpper())}-$seedValue")
}
else
{
$newPlaceholder = $newPlaceholder.Replace($renderingId, "{$($renderingId.ToUpper())}-0")
}
}
$newPlaceholder = $newPlaceholder.Replace('{_', "-{")
Write-Host "New Placeholder - [$($newPlaceholder)]"
# comment following 2 lines if you don't want to do replacement, but want to have log of upcoming placeholder changes
$rendering.Placeholder = $newPlaceholder
Set-Rendering -Item $item -Instance $rendering -FinalLayout
}
}
}