Advertisement
private775

SP2010: update copyrights info in master pages (Regex)

Dec 30th, 2015
2,627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $webApp = "http://acme"
  2. $crRx = "Copyright\s*201[0-5]"
  3. $rplTxt = "Copyright 2016"
  4.  
  5. $siteColls = get-spsite -Limit ALL -WebApplication $webApp
  6.  
  7. function ProcessSite([Microsoft.SharePoint.SPSite]$site){
  8.     $site.AllWebs|%{ProcessWeb $_.Url}
  9. }
  10.  
  11. function ProcessWeb([string]$webUrl){
  12.     $web = get-spweb $webUrl
  13.     write-host "Processing: $($web.Url)"
  14.     $wSRUrl = $web.ServerRelativeUrl
  15.     if($wSRUrl -eq "/"){
  16.         $wSRUrl = ""
  17.  
  18.     }
  19.     $catalogRx = "^$($wSRUrl)/_catalogs/masterpage"
  20.     $masterUrl = $web.MasterUrl
  21.     if($masterUrl -match $catalogRx){
  22.             $tempFileName =  [System.IO.Path]::GetTempFileName()
  23.  
  24.             $f = $web.GetFile($masterUrl)
  25.             if($f.Exists){
  26.                     write-host "** $($masterUrl): $($f.Length)"
  27.                     $ss = $f.OpenBinaryStream([Microsoft.SharePoint.SPOpenBinaryOptions]::SkipVirusScan)
  28.                     $bytes = New-Object Byte[] $($ss.Length)
  29.                     $ss.Read($bytes, 0, $ss.Length)
  30.                     $ss.Close()
  31.                     $content = [System.Text.Encoding]::UTF8.GetString($bytes)
  32.                     if($content -ne $null -and $content -ne ""){
  33.                             if($content -match $crRx){
  34.                                     write-host "!!!!Found Copyrights"
  35.                                     $newContent = $content -replace $crRx,$rplTxt
  36.                                     $newContentBytes = [System.Text.Encoding]::UTF8.GetBytes($newContent)
  37.                                     $f = $web.GetFile($masterUrl)
  38.                                     if($f.RequiresCheckout){
  39.                                         $f.CheckOut()
  40.                                     }
  41.                                     $f.SaveBinary($newContentBytes)
  42.                                     if($f.RequiresCheckout){
  43.                                         $f.CheckIn("Copyrights update", [ Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
  44.                                         $f.Approve("Copyrights update")
  45.                                     }
  46.  
  47.  
  48.                                 } else {
  49.                                     write-host "----No Copyrights"
  50.                                 }
  51.                         } else {
  52.                     write-host "The GetFile function returned empty string"
  53.                         }
  54.                 } else {
  55.                     write-host "File not exists"
  56.                 }
  57.  
  58.         } else {
  59.             write-host "No local MasterPage"
  60.         }
  61. }
  62.  
  63.  
  64. $siteColls|%{ProcessSite $_}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement