Sharepoint 2013 PowerShell 脚本替换为通配符多次出现的字符串
Sharepoint 2013 PowerShell Script Replace with Wildcards Multiple occurrences of string
我们在 SharePoint 2013 上托管一个网站(许多网站之一,这个网站造成了不必要的痛苦)。我正在使用 powershell 脚本尝试用空字符串替换部分 Web 部件内容,很快就会失效.
我不确定如何正确使用PowerShell 替换通配符以及在线的多个示例之间,很难搞清楚。也许只是累了。
我将用一个例子来演示我所拥有的和实际的结果。
Powershell 脚本:
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
我有:
<p><span><strong class="ms-rteFontSize-3 ms-rteForeColor-1">Related Documentation:</strong><span class="ms-rteFontSize-3 ms-rteStyle-Emphasis"></span><img class="ms-rtePosition-2 ms-
rteStyle-Emphasis" 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 class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=
%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F02%20Standards%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability&FolderCTID=0x01200
0CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Standards</a></span></span></p><div><a href="/sites/PMW/Palabora%20SHEQ%20Management%20
System/04%20%20Palabora%20Operating%20Procedures/4.6%20SHE%20Standards/4.6.25%20Land%20Use%20Management%20and%20Sustainability"><span class="ms-rteFontSize-2"><em>Palabora Operating
Procedures</em></span></a></div><div> </div><p><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Manageme
nt%20System/Forms/AllItems.aspx?RootFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F15%20Forms%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%
20Sustainability&FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Forms</a></span></span></p><p><span class="ms-rt
eStyle-Emphasis"><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?Roo
tFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F20%20Management%20Plan%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability&Fo
lderCTID=0x012000CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Management Plan</a></span></span></span></p><a href="/sites/PMW/Palabor
a%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F16%20Guidance%20Notes%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20
Land%20Use%20Management%20and%20Sustainability&FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D"><span class="ms-rt
eFontSize-2"><em>Guidance Notes</em></span></a>
实际电流输出:
<p><span><strong class="ms-rteFontSize-3 ms-rteForeColor-1">Related Documentation:</strong><span class="ms-rteFontSize-3 ms-rteStyle-Emphasis"></span><img class="ms-rtePosition-2 ms-
rteStyle-Emphasis" 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 class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=
%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F02%20Standards%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability">HHH<span class="ms-rt
eFontSize-2"><em>Guidance Notes</em></span></a>
替换实际上应该只替换每次出现的“&FolderCTID=0x01200
0CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D" with ""。目前它似乎从第一次出现开始并替换它和最后一次出现之间的所有内容。
奇怪的是它对我来说似乎工作正常!
但问题可能是由于正则表达式及其如何 lazy/greedy 匹配。
我无法测试,但请尝试将 '&FolderCTID=.*">'
更改为 '&FolderCTID=.*?">'
这将匹配您的模式直到下一次出现,而不是最后一次出现。
我们在 SharePoint 2013 上托管一个网站(许多网站之一,这个网站造成了不必要的痛苦)。我正在使用 powershell 脚本尝试用空字符串替换部分 Web 部件内容,很快就会失效.
我不确定如何正确使用PowerShell 替换通配符以及在线的多个示例之间,很难搞清楚。也许只是累了。
我将用一个例子来演示我所拥有的和实际的结果。
Powershell 脚本:
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
我有:
<p><span><strong class="ms-rteFontSize-3 ms-rteForeColor-1">Related Documentation:</strong><span class="ms-rteFontSize-3 ms-rteStyle-Emphasis"></span><img class="ms-rtePosition-2 ms-
rteStyle-Emphasis" 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 class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=
%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F02%20Standards%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability&FolderCTID=0x01200
0CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Standards</a></span></span></p><div><a href="/sites/PMW/Palabora%20SHEQ%20Management%20
System/04%20%20Palabora%20Operating%20Procedures/4.6%20SHE%20Standards/4.6.25%20Land%20Use%20Management%20and%20Sustainability"><span class="ms-rteFontSize-2"><em>Palabora Operating
Procedures</em></span></a></div><div> </div><p><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Manageme
nt%20System/Forms/AllItems.aspx?RootFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F15%20Forms%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%
20Sustainability&FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Forms</a></span></span></p><p><span class="ms-rt
eStyle-Emphasis"><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?Roo
tFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F20%20Management%20Plan%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability&Fo
lderCTID=0x012000CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Management Plan</a></span></span></span></p><a href="/sites/PMW/Palabor
a%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F16%20Guidance%20Notes%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20
Land%20Use%20Management%20and%20Sustainability&FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D"><span class="ms-rt
eFontSize-2"><em>Guidance Notes</em></span></a>
实际电流输出:
<p><span><strong class="ms-rteFontSize-3 ms-rteForeColor-1">Related Documentation:</strong><span class="ms-rteFontSize-3 ms-rteStyle-Emphasis"></span><img class="ms-rtePosition-2 ms-
rteStyle-Emphasis" 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 class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=
%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F02%20Standards%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability">HHH<span class="ms-rt
eFontSize-2"><em>Guidance Notes</em></span></a>
替换实际上应该只替换每次出现的“&FolderCTID=0x01200 0CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D" with ""。目前它似乎从第一次出现开始并替换它和最后一次出现之间的所有内容。
奇怪的是它对我来说似乎工作正常!
但问题可能是由于正则表达式及其如何 lazy/greedy 匹配。
我无法测试,但请尝试将 '&FolderCTID=.*">'
更改为 '&FolderCTID=.*?">'
这将匹配您的模式直到下一次出现,而不是最后一次出现。