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

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

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

SharePoint Online:使用 PowerShell 查找所有自定义操作


要求:在 SharePoint Online 中查找所有自定义操作

PowerShell 获取 SharePoint Online 中的所有用户自定义操作:

自定义操作是在 SharePoint 上添加自定义的强大机制。我需要准备特定站点上所有自定义操作的清单,下面是用于查找给定站点上的自定义操作的 PowerShell 脚本。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
 
Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get All Custom Actions
    $UserCustomActions= $Ctx.Web.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    If($UserCustomActions.count -gt 0)
    {
        ForEach($CustomActions in $UserCustomActions)
        {
            Write-Host -f Green "Custom Action Name: '$($CustomActions.Name)' ID: $($CustomActions.ID)"
        }
    }
    Else
    {
        write-host -f Yellow "No Custom Actions Found!"
    }
}
Catch {
        write-host -f Red "Error Getting Custom Actions!" $_.Exception.Message
}

该脚本列出了给定 Web 的所有用户自定义操作。您可以将对象更改为站点,以便查找站点对象的所有自定义操作。例如,$UserCustomActions= $Ctx.Web.UserCustomActions 到 $UserCustomActions= $Ctx.Site.UserCustomActions

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

让我们稍微更改一下上面的脚本,以迭代给定网站集中的所有子网站,以获取网站和 Web 对象的所有用户自定义操作的清单,并将我们的发现导出到 CSV 文件。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Get-SPOCustomActions([String]$SiteURL,[String]$Scope)
{  
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web and Sub Sites
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Webs)
        $Ctx.ExecuteQuery()

        #Get All Custom Actions based on given scope: Site or Web
        If($Scope -eq "Site")
        {
            $UserCustomActions= $Ctx.Site.UserCustomActions
            Write-host -f Yellow "Searching $Scope Scoped Custom Actions at '$SiteURL'"
        }
        ElseIf($Scope -eq "Web")
        {
            $UserCustomActions= $Ctx.Web.UserCustomActions
            Write-host -f Yellow "Searching $Scope Scoped Custom Actions at '$SiteURL"
        }
        $Ctx.Load($UserCustomActions)
        $Ctx.ExecuteQuery()

        $CustomActionsData = @()
        ForEach($CustomAction in $UserCustomActions)
        {
            Write-Host -f Green "Found a Custom Action: '$($CustomAction.Name)' at : $($SiteURL)"
        
            #Get Custom Action Data
            $CustomActionsData += New-Object PSObject -Property @{
                    'Site URL' = $SiteUrl
                    'Custom Action Title' = $CustomAction.Title
                    'Name' = $CustomAction.Name
                    '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
    
        #Get the Root Web's Custom Actions
        If($Scope -eq "Site")
        {         
            #Call the function to get Root Web's Custom Actions
            Get-SPOCustomActions -SiteURL $SiteURL -Scope "Web"   
        }
        Else
        {
            #Iterate through each subsite of the current web
            ForEach ($Subweb in $Ctx.Web.Webs)
            {
                #Call the function recursively 
                Get-SPOCustomActions -SiteURL $Subweb.url -Scope "Web"
            }
        }
    }
    Catch {
        write-host -f Red "Error Generating Custom Actions Report!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

$ReportOutput = "C:\Temp\CustomActions.csv"
#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }

#Call the function 
Get-SPOCustomActions -SiteURL $SiteURL -Scope "Site" 

使用 PnP PowerShell 获取所有自定义操作

要检索所有自定义操作,请使用 PnP PowerShell cmdlet Get-PnPCustomAction


#Variable
$SiteURL = "https://Crescent.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all custom actions
Get-PnPCustomAction -Scope All

您可以将范围设置为“网站”或“Web”以检索网站集或特定于网站的自定义操作。

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

取消回复欢迎 发表评论:

关灯