使用 PowerShell 查找 SharePoint 网站中使用的所有 Web 部件
要求:生成报告以获取 SharePoint 网站集中所有正在使用的 Web 部件的清单。
用于生成网站集中使用的 Web 部件的 PowerShell 脚本:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration parameters
$SiteURL = "https://intranet.crescent.com"
$ReportOutput="C:\Webparts-in-use.csv"
$ResultCollection = @()
#Get All Subsites in a site collection and iterate through each
$Site = Get-SPSite $SiteURL
ForEach($Web in $Site.AllWebs)
{
write-host Processing $Web.URL
# If the Current Web is Publishing Web
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
{
#Get the Publishing Web
$PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
#Get the Pages Library
$PagesLib = $PubWeb.PagesList
}
else
{
$PagesLib = $Web.Lists["Site Pages"]
}
#Iterate through all Pages
foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"})
{
$PageURL=$web.site.Url+"/"+$Page.File.URL
$WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
#Get All Web Parts data
foreach ($WebPart in $WebPartManager.WebParts)
{
$Result = New-Object PSObject
$Result | Add-Member -type NoteProperty -name "Site URL" -value $web.Url
$Result | Add-Member -type NoteProperty -name "Page URL" -value $PageURL
$Result | Add-Member -type NoteProperty -name "Web Part Title" -value $WebPart.Title
$Result | Add-Member -type NoteProperty -name "Web Part Type" -value $WebPart.GetType().ToString()
$ResultCollection += $Result
}
}
}
#Export results to CSV
$ResultCollection | Export-csv $ReportOutput -notypeinformation
此脚本获取网站集中的所有 Web 部件并生成 CSV 格式的报告。
如何查找特定 Web 部件的使用情况?
假设您想要查找 SharePoint 环境中的所有 Bamboo Web 部件。很简单,只需将第 34 行更改为:
ForEach ($WebPart in $WebPartManager.WebParts | Where-Object { $_.GetType().ToString() -like "*bamboo*"} )
查找 Web 应用程序级别的 Web 部件使用情况:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration parameters
$WebAppURL = "https://intranet.crescent.com"
$ReportOutput="C:\Webparts-in-use.csv"
$ResultCollection = @()
#Get All Site Collections in the web application
$Sites = Get-SPWebApplication $WebAppURL | Get-SPSite
#Iterate through each Site collection
ForEach($Site in $Sites)
{
ForEach($Web in $Site.AllWebs)
{
write-host Processing $Web.URL
# If the Current Web is Publishing Web
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
{
#Get the Publishing Web
$PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
#Get the Pages Library
$PagesLib = $PubWeb.PagesList
}
else
{
$PagesLib = $Web.Lists["Site Pages"]
}
#Iterate through all Pages
foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"})
{
$PageURL=$web.site.Url+"/"+$Page.File.URL
$WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
#Get All Web Parts data
foreach ($WebPart in $WebPartManager.WebParts)
{
$Result = New-Object PSObject
$Result | Add-Member -type NoteProperty -name "Site URL" -value $web.Url
$Result | Add-Member -type NoteProperty -name "Page URL" -value $PageURL
$Result | Add-Member -type NoteProperty -name "Web Part Title" -value $WebPart.Title
$Result | Add-Member -type NoteProperty -name "Web Part Type" -value $WebPart.GetType().ToString()
$ResultCollection += $Result
}
}
}
#Export results to CSV
$ResultCollection | Export-csv $ReportOutput -notypeinformation -append
}
