当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 查找 SharePoint 中的所有自定义操作

作者:精品下载站 日期:2024-12-14 14:39:24 浏览:15 分类:玩电脑

使用 PowerShell 查找 SharePoint 中的所有自定义操作


PowerShell 在 SharePoint 中获取自定义操作

自定义操作可以部署到站点、Web 或列表范围。以下是一个 PowerShell 脚本,用于列出给定 Web 范围中的所有自定义操作。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Site variable
$SiteURL="https://intranet.crescent.com"

Try {
    #Get the Web
    $Web = Get-SPWeb $SiteURL
 
    #Get All Custom Actions of the web
    $CustomActions = $web.UserCustomActions

    If($CustomActions -ne $Null)
    {
        #Get custom action
        ForEach($CustomAction in $CustomActions)
        {
            Write-Host -f Green "Custom Action Title: '$($CustomAction.Title)' ID: $($CustomAction.ID)"
        }
    }
    Else
    {
        write-host -f Yellow "No Custom Actions Found!"
    } 
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

使用 PowerShell 查找网站集中的所有自定义操作


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom function to get custom actions from given scope
Function Get-CustomActions([Microsoft.SharePoint.SPUserCustomActionCollection]$UserCustomActionCollection, [string] $ScopeReference, [string] $ReportOutput)
{
        $CustomActionsData = @()
        ForEach($CustomAction in $UserCustomActionCollection)
        {
            Write-Host -f Green "`t `t `t Found a Custom Action: '$($CustomAction.Title)' at:" $ScopeReference
            #Get Custom Action Data
            $CustomActionsData += New-Object PSObject -Property @{
                    'Deployed At' = $ScopeReference
                    'Name' = $CustomAction.Name
                    'Title' = $CustomAction.Title
                    'ID' = $CustomAction.ID
                    'Group' = $CustomAction.Group
                    'Location' = $CustomAction.Location                
                    'Sequence' = $CustomAction.Sequence
                    'Url' = $CustomAction.Url
                    'Scope' = $CustomAction.Scope
                    'ScriptBlock' = $CustomAction.ScriptBlock
                    'ScriptSrc' = $CustomAction.ScriptSrc 
                    }
        }
        #Export the data to CSV
        $CustomActionsData | Export-Csv $ReportOutput -NoTypeInformation -Append  

}

#Set Site variable
$SiteURL="https://intranet.crescent.com"
 
$ReportOutput = "C:\Temp\CustomActions.csv"
#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }
 
#Get the Site Collection
$Site = Get-SPSite $SiteURL

Write-host -f Yellow "Searching Site Scoped Custom Actions at:" $SiteURL
$ScopeReference = "Site Collection:"+$Site.Url
If($Site.UserCustomActions -ne $Null) { Get-CustomActions $Site.UserCustomActions  $ReportOutput}
    
#Iterate through each web (subsite) and Get All custom actions
ForEach ($web in $Site.AllWebs)
{
    Write-host -f Yellow "`t Searching Web Scoped Custom Actions at:" $Web.Url
    
    #Get all lists of the web - Exclude hidden
    $ListCollection = $web.Lists | Where-Object  { ($_.hidden -eq $false)}

    #Call the function to Get custom actions at web
    $ScopeReference = "Web:"+$Web.URL
    If($web.UserCustomActions -ne $null) { Get-CustomActions $web.UserCustomActions $ScopeReference $ReportOutput }

    #Get Custom Actions at List level
    Write-host -f Yellow "`t `t Searching List Scoped Custom Actions at:" $Web.Url
    ForEach($List in $ListCollection)
    {
        $ScopeReference = "List:"+$list.RootFolder.ServerRelativeUrl
        If($List.UserCustomActions -ne $Null) {Get-CustomActions $list.UserCustomActions $ScopeReference $ReportOutput}
    }
}

此脚本从给定网站集的网站、Web 和列表范围获取所有自定义操作,并将它们导出到 CSV 文件。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯