param( [Parameter(Mandatory=$true)] [string]$url, [Parameter(Mandatory=$true)] [string]$oldPmcRelativeUrl, [Parameter(Mandatory=$true)] [string]$newPmcRelativeUrl ) function Write-File($msg) { ($msg) | Out-File "log-page-layouts.txt" -Append } function Write-Info($msg) { Write-Host $msg -foregroundcolor green Write-File $msg } function Write-Warn($msg) { Write-Host $msg -foregroundcolor yellow Write-File $msg } function Update-Page-Layout-Page($publishingPage, $oldPmcRelativeUrl, $newPmcRelativeUrl) { $file = $publishingPage.ListItem.File Write-Info ("Check page: " + $file.Url) Write-Info ("Current page layout: " + $file.Properties["PublishingPageLayout"].ToString()) if ((-not [System.String]::IsNullOrEmpty($file.Properties["PublishingPageLayout"])) -and $file.Properties["PublishingPageLayout"].ToString().Contains("/" + $oldPmcRelativeUrl + "/")) { if ($file.CheckOutStatus -eq [Microsoft.SharePoint.SPFile+SPCheckOutStatus]::None) { $file.CheckOut() } else { $file.UndoCheckOut() $file.CheckOut() } $file.Properties["PublishingPageLayout"] = $file.Properties["PublishingPageLayout"].ToString().Replace("/" + $oldPmcRelativeUrl + "/", "/" + $newPmcRelativeUrl + "/") $file.Update() $file.CheckIn("Update page layout via PowerShell", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn) if ($publishingPage.ListItem.ParentList.EnableModeration) { $file.Approve("Update page layout via PowerShell"); } Write-Warn "Page layout was successfully updated" } } function Update-Page-Layout-Web($web, $oldPmcRelativeUrl, $newPmcRelativeUrl) { Write-Info ("Update page layouts for web: " + $web.Url) $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web); $pages = $pubWeb.GetPublishingPages() foreach ($page in $pages) { Update-Page-Layout-Page $page $oldPmcRelativeUrl $newPmcRelativeUrl } foreach ($subWeb in $web.Webs) { Update-Page-Layout-Web $subWeb $oldPmcRelativeUrl $newPmcRelativeUrl } } function Update-Page-Layout-Site($site, $oldPmcRelativeUrl, $newPmcRelativeUrl) { foreach ($web in $site.RootWeb.Webs) { Update-Page-Layout-Web $web $oldPmcRelativeUrl $newPmcRelativeUrl } } $site = Get-SPSite $url Update-Page-Layout-Site $site $oldPmcRelativeUrl $newPmcRelativeUrl #$web = Get-SPWeb $url #Update-Page-Layout-Web $web $oldPmcRelativeUrl $newPmcRelativeUrl