# Function: Apply-MasterPageTheme # Description: Upload the New Master Page and apply it on all sites # Parameters: SiteCollectionURL URL for Site Collection # SiteCollRelativeURL Relative URL for Site Collection # FileMasterPage Master Page to Apply # ThemeName Theme Name function Apply-MasterPageTheme([string]$SiteCollectionURL, [string]$SiteCollRelativeURL, [string]$FileMasterPage, [string]$ThemeName) { [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) $web = $site.openweb($SiteCollRelativeURL) $MasterURL = $SiteCollRelativeURL + "/_catalogs/masterpage/" + $FileMasterPage # Write-Host "FileMasterPage", $FileMasterPage # Write-Host "MasterURL", $MasterURL Update-MasterTheme $web $MasterURL $ThemeName } # Function: Update-MasterTheme # Description: Update the New Master Page and the Theme # Parameters: web SPWeb Object # TempFileMasterPage Master Page # TempThemeName Theme Name function Update-MasterTheme([object]$web, [string]$TempFileMasterPage, [string]$TempThemeName) { # Write-Host "FileMasterPage", $TempFileMasterPage # Write-Host "Theme", $web.Theme $web.MasterUrl = $TempFileMasterPage $web.Update() $web.ApplyTheme($TempThemeName) $web.Update() foreach ($subweb in $web.GetSubwebsForCurrentUser()) { Update-MasterTheme $subweb $TempFileMasterPage $TempThemeName } } # Function: Upload-MasterPage # Description: Upload the New Master Page # Parameters: SiteCollectionURL URL for Site COllection # RelativeWebURL Relative Web Url # PathMasterPage Path Master Page # MasterPageName Master Page Name function Upload-MasterPage([string]$SiteCollectionURL, [string]$RelativeWebURL, [string]$PathMasterPage, [string]$MasterPageName) { # Write-Host "SiteCollectionURL", $SiteCollectionURL # Write-Host "RelativeWebURL", $RelativeWebURL # Write-Host "PathMasterPage", $PathMasterPage $propbag = @{"ContentType"="Master Page"} $docliburl = $SiteCollectionURL + "/_catalogs/masterpage" [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null $site = new-object Microsoft.SharePoint.SPSite($docliburl) $web = $site.openweb($RelativeWebURL) $folder = $web.getfolder($docliburl) $list = $web.lists[$folder.ContainingDocumentLibrary] $stream = [IO.File]::OpenRead($PathMasterPage) # Write-Host "MasterPageName", $MasterPageName $folder.files.Add($MasterPageName, $stream, $propbag, $true) > $null $stream.close() } # Function: Update-All-SiteCollections # Description: Upload the New Master Page # Parameters: WebAppURL URL for Web Application # LocalPathMasterPage Local Path for Master Page # LocalMasterPageName Local Master Page # LocalTheme Local Theme function Update-All-SiteCollections([string]$WebAppURL, [string]$LocalPathMasterPage, [string]$LocalMasterPageName, [string]$LocalTheme) { [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null $Thesite = new-object Microsoft.SharePoint.SPSite($WebAppURL) $oApp = $Thesite.WebApplication foreach ($Sites in $oApp.Sites) { $mySubweb = $Sites.RootWeb $TempRelativeURL = $mySubweb.Url Write-Host "TempRelativeURL", $TempRelativeURL $RelativeURL = $TempRelativeURL -replace $WebAppURL, "" Write-Host "RelativeURL", $RelativeURL Upload-MasterPage $mySubweb.Url $RelativeURL $LocalPathMasterPage $LocalMasterPageName Apply-MasterPageTheme $mySubweb.Url $RelativeURL $LocalMasterPageName $LocalTheme } } Update-All-SiteCollections "http://win-urb7xo3pw9h" "E:\TOOLS\PowerShellScripts\sample.master" "sample.master" "VINTAGE"