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

[玩转系统] 如何使用 PowerShell 删除 SharePoint 中的自定义操作?

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

如何使用 PowerShell 删除 SharePoint 中的自定义操作?


要求: 使用 PowerShell 删除 SharePoint 中的自定义操作。

在 SharePoint 中使用 PowerShell 删除自定义操作

以下是如何使用 PowerShell 在 SharePoint 2013 中删除自定义操作:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

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

Try {
    #Get the Web
    $Web = Get-SPWeb $SiteURL
 
    #Get the Custom Actions Filter by Title
    $CustomActions = $web.UserCustomActions | Where { $_.Title -eq $CustomActionTitle } | Select ID, Title

    If($CustomActions -ne $Null)
    {
        #Delete custom action(s)
        $CustomActions | ForEach-Object {
            #Remove the custom action
            $web.UserCustomActions.Item($_.Id).Delete()
            Write-Host -f Green "Custom Action '$($_.Title)' Deleted Successfully!"
        }
    }
    Else
    {
        write-host -f Yellow "Custom Action '$CustomActionTitle' Doesn't Exist!"
    } 
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

要从给定范围中删除所有自定义操作,只需从脚本中删除“Where”类即可!

让我们把它变成一个可重用的函数:


Function Remove-CustomAction
{
    param 
    (        
[parameter(Mandatory=$true, ParameterSetName='Site')]
[Microsoft.SharePoint.SPSite]$Site,
[parameter(Mandatory=$true, ParameterSetName='Web')]
[Microsoft.SharePoint.SPWeb]$Web,
[parameter(Mandatory=$true, ParameterSetName='Site')] 
[parameter(Mandatory=$true, ParameterSetName='Web')]
[string]$ActionName
    )
 
    begin
    {
        $existingActions = @()
    }
    process
    {
        if( $PSCmdlet.ParameterSetName -eq "Site" )
        {
            $Site.UserCustomActions | ? { $_.Title -eq $ActionName } | % { $existingActions += $_ }
            $existingActions | % { $Site.UserCustomActions.Item($_.Id).Delete() }
        }
        else
        {
            $Web.UserCustomActions | ? { $_.Title -eq $ActionName } | % { $existingActions += $_ }
            $existingActions
            $existingActions | % { $Web.UserCustomActions.Item($_.Id).Delete() }
        }
    }
    end
    {
    }
}
#Call the function
Remove-CustomAction -Site $site -ActionName "SiteBanner"

我关于在 SharePoint Online 中删除自定义操作的相关文章:如何使用 PowerShell 在 SharePoint Online 中删除自定义操作?

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

取消回复欢迎 发表评论:

关灯