Home > Net >  Sharepoint 2013 PowerShell Script Replace with Wildcards Multiple occurrences of string
Sharepoint 2013 PowerShell Script Replace with Wildcards Multiple occurrences of string

Time:06-03

We host a site (one of many, this one is causing unnecessary pain) on SharePoint 2013. I am using a powershell script to try and replace, soon to be invalid, part of the webpart content with an empty string.

I am not sure how to properly use the PowerShell replace with wildcards and between the multiple examples onl-line, have trouble to figure it out. Maybe just tired.

I am going to use one example to demonstrate what I have and the actual result.

Powershell Script:

    cls
Add-PSSnapin "Microsoft.SharePoint.PowerShell"


#Get All Webs (sites)
$webs = Get-SPWebApplication "mysite" | Get-SPSite -Limit All | Get-SPWeb -Limit All
  
 #Iterate through webs
 foreach ($web in $webs)
 {
  #Get All Pages from site's Root into $AllPages Array
  $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
  
  #Search All Folders for Pages
  foreach ($folder in $web.Folders)
      {
          #Add the pages to $AllPages Array
          $AllPages  = @($folder.Files | Where-Object {$_.Name -match ".aspx"})
      }
 
   #Iterate through all pages
   foreach($Page in $AllPages)
   {
        if ($Page.name -inotlike "home*" -and $Page.name -inotlike "dispform*" -and $Page.name -inotlike "editform*" -and $Page.name -inotlike "default*" -and $Page.name -inotlike "allitems*" -and $Page.name -inotlike "newform*" -and $Page.name -inotlike "newsfeed*" -and $Page.name -inotlike "*all *" -and $Page.name -inotlike "how to use*" -and $Page.name -inotlike "myitems*" -and $Page.name -inotlike "active*" -and $Page.name -inotlike "duetoday*" -and $Page.name -inotlike "MyGrTsks*" -and $Page.name -inotlike "GettingStarted*" -and $Page.name -like "Land-Use-Management-and-Sustainability*" )
        {
        write-host $Page.Name
        #Web Part Manager to get all web parts from the page
        $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
 
           #Iterate through each web part
           foreach($webPart in $WebPartManager.WebParts)
           {
                #$webPart.ViewContentTypeId.ToString()
                $ViewContentTypeID = $webPart.Title
                $ViewContent = $webPart.Content.InnerText
                $ViewContentX = $ViewContent -replace '&FolderCTID=.*">','">HHH'
                $IconImagePath = $webPart.CatalogIconImageUrl
                write-host "  -Webpart: $ViewContentTypeID"
                write-host "  -WebpartContent: $ViewContent"
                write-host "  -WebpartContentX: $ViewContentX"
                #write-host "  -CatalogIconImageUrl: '$IconImagePath'"
                #write-host "Webpart: '$webPart.title'"
                #$OldTitle=$webPart.title
                ##Get the Content Editor web part with specific Title
                #if($webPart.title -like "*Crescent Inc*")
                # {
                #      #Replace the Old Title
                #      $webPart.title = $webPart.title.Replace("Crescent Inc.", "Lunar Inc.")
                # 
                #      #Same method goes to update any other custom properties.
                #      #E.g. To update Page viewer web part's link property:
                #      #$webPart.ContentLink = "https://www.sharepointdiary.com"
                #      #To set built-it properties, E.g. To set Set the Chrome type programmatically use:
                #      #$webPart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleAndBorder
                #       
                #      #Save the changes
                #      $webPartManager.SaveChanges($webPart)
                #     
                #      write-host "Updated '$($OldTitle)' on $($web.URL)$($Page.ServerRelativeUrl)"
                # }
           }
      }
  }
}


#Read more: https://www.sharepointdiary.com/2013/08/update-web-part-properties-programmatically.html#ixzz7V2okwR00

What I have:

<p><span><strong >Related Documentation:</strong><span ></span><img  alt="stick_figure_holding_folder_9748.png" src="/PublishingImages/Pages/SHEQ-Policy/stick_figure_holding_folder_9748.png" style="margin: 5px; width: 131px;"/></spa
n></p><p><span ><span ><a href="/sites/PMW/Palabora SHEQ Management System/Forms/AllItems.aspx?RootFolder=
/sites/PMW/Palabora SHEQ Management System/02 Standards/4.6 SHE Standards/4.6.25 Land Use Management and Sustainability&amp;FolderCTID=0x01200
0CEE56914143D1444809437B68BE10DBE&amp;View={53CE6768-B28C-4261-9E86-92A5904DE9CD}">Standards</a></span></span></p><div><a href="/sites/PMW/Palabora SHEQ Management 
System/04  Palabora Operating Procedures/4.6 SHE Standards/4.6.25 Land Use Management and Sustainability"><span ><em>Palabora Operating 
Procedures</em></span></a></div><div>&#160;</div><p><span ><span ><a href="/sites/PMW/Palabora SHEQ Manageme
nt System/Forms/AllItems.aspx?RootFolder=/sites/PMW/Palabora SHEQ Management System/15 Forms/4.6 SHE Standards/4.6.25 Land Use Management and%
20Sustainability&amp;FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&amp;View={53CE6768-B28C-4261-9E86-92A5904DE9CD}">Forms</a></span></span></p><p><span ><span ><span ><a href="/sites/PMW/Palabora SHEQ Management System/Forms/AllItems.aspx?Roo
tFolder=/sites/PMW/Palabora SHEQ Management System/20 Management Plan/4.6 SHE Standards/4.6.25 Land Use Management and Sustainability&amp;Fo
lderCTID=0x012000CEE56914143D1444809437B68BE10DBE&amp;View={53CE6768-B28C-4261-9E86-92A5904DE9CD}">Management Plan</a></span></span></span></p><a href="/sites/PMW/Palabor
a SHEQ Management System/Forms/AllItems.aspx?RootFolder=/sites/PMW/Palabora SHEQ Management System/16 Guidance Notes/4.6 SHE Standards/4.6.25 
Land Use Management and Sustainability&amp;FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&amp;View={53CE6768-B28C-4261-9E86-92A5904DE9CD}"><span ><em>Guidance Notes</em></span></a>

Actual current output:

<p><span><strong >Related Documentation:</strong><span ></span><img  alt="stick_figure_holding_folder_9748.png" src="/PublishingImages/Pages/SHEQ-Policy/stick_figure_holding_folder_9748.png" style="margin: 5px; width: 131px;"/></spa
n></p><p><span ><span ><a href="/sites/PMW/Palabora SHEQ Management System/Forms/AllItems.aspx?RootFolder=
/sites/PMW/Palabora SHEQ Management System/02 Standards/4.6 SHE Standards/4.6.25 Land Use Management and Sustainability">HHH<span ><em>Guidance Notes</em></span></a>

The replace should actually just replace every occurrence of "&FolderCTID=0x01200 0CEE56914143D1444809437B68BE10DBE&View={53CE6768-B28C-4261-9E86-92A5904DE9CD}" with "". Currently it seems like it starts at the first occurrence and replace everything between it and the last occurrence.

CodePudding user response:

Strangely it seems to be working ok for me!

But the issue may be due to regex and how it does lazy/greedy matching.

I cant test it, but try changing the '&amp;FolderCTID=.*">' to '&amp;FolderCTID=.*?">'

This will then match your pattern until the next occurence, not to the last occurence.

  • Related