Powershell script to update masterpage:


Param(
 $csvOutPutName = "masterPageOutput-ts.csv"
 )
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint')
#SCRIPT VARIABLES
 [string]$siteUrl = $args[0]
function GetMissingParameter {
 $script:siteUrl = read-host "Enter Site URL"
 }
function ProcessDirectory($directory, $spfolder) {
 foreach ($file in $directory.GetFiles()) {
 $docUrl = $spfolder.Url + "/" + (Split-Path $file -Leaf)
 $fileToUpdate = $spfolder.ParentWeb.GetFile($docUrl)
if ($fileToUpdate.Exists) {
 $fileToUpdate.CheckOut()
 $newfile = $spfolder.Files.Add($file.Name, $file.OpenRead(), $true)
 $newfile.Update()
 $newfile.CheckIn("Checked-in file via PowerShell script.")
if ($newfile.Level -eq "Draft") {
 $newfile.Publish("Published file via PowerShell script.")
 if ($newfile.Level -ne "Published") {
 $newfile.Approve("Approved file via PowerShell script.")
 }
 }
Write-Host Uploaded file $file.Name to $spfolder.Name
 }
 }
 }
############
 # MAIN
 ############
#IF MISSING PARAM FOR SITE URL OR MANAGED PATH, ASK FOR INPUT TO FILL
 if($args.length -eq 0) {
 GetMissingParameter
 }
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
 $impactedSites = @()
cd Masterpages
 $currentfolder = get-item .
Write-Host "Copying masterpage files to $($site.Url)..." -foregroundcolor Yellow
foreach($web in $site.AllWebs) {
 $obj = New-Object PSObject -property @{ Url = $web.Url; OldCustomMasterPage = $web.CustomMasterUrl; OldMasterPage = $web.MasterUrl; SiteType = "Web"; }
 $obj | fl
$masterpageGallery = $web.GetCatalog(116).RootFolder
 ProcessDirectory $currentfolder $masterpageGallery
$impactedSites += $obj }
cd ..
$site.Dispose()
$impactedSites | ft
 $impactedSites | Export-Csv -Path $csvOutPutName -NoTypeInformation
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s