网站搜索

SharePoint Online:使用 PowerShell 在所有站点上激活功能


要求:使用 PowerShell 为所有网站启用 SharePoint Online 功能。

使用 PowerShell 激活所有网站的网站集功能

您想要在所有 SharePoint Online 网站上激活该功能吗?在管理大量网站时,使用 PowerShell 激活 SharePoint Online 网站上的功能可以节省时间和精力。本指南将向您展示如何使用 PowerShell 激活所有 SharePoint Online 网站上的功能。我们需要为租户中的所有网站集启用 SharePoint Online“默认在客户端应用程序中打开文档”功能。

以下是为所有网站集启用一项功能的 PowerShell:


#Parameters
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
#Site Collection feature "Open Documents in Client Applications by Default"
$FeatureId = "8a4b8de2-6fd8-41e9-923c-c7c3c00f8295" 

#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred

#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0","SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")

#Loop through each site collection
ForEach($Site in $SitesCollections)
{
    #Connect to site collection
    Write-host -f Yellow "Trying to Activate the feature on site:"$Site.Url    
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred

    #Get the Feature
    $Feature = Get-PnPFeature -Scope Site -Identity $FeatureId

    #Check if feature is activated
    If($Feature.DefinitionId -eq $null)
    {
        #Enable site collection feature
        Enable-PnPFeature -Scope Site -Identity $FeatureId -Force

        Write-host -f Green "`tFeature Activated Successfully!"
    }
    Else
    {
        Write-host -f Cyan "`tFeature is already active!"
    }
}

此 PowerShell 脚本会激活 SharePoint Online 租户中所有网站上的一项功能。如果您需要在多个站点上快速激活该功能,这会很有用。顺便说一句,您需要网站集管理员权限才能完成这些步骤。

使用 PowerShell 在 SharePoint Online 网站集的所有网站中启用网站功能

如果您想要在所有网站(包括给定网站集的根网站和子网站)上激活 Web 范围的功能,该怎么办?比如说,我们要在 SharePoint Online 网站集中的所有网站(网站)上激活“发布”功能。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FeatureId = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" #Web Scoped Publishing Feature
 
#Function to Activate a Feature in SharePoint Online Web
Function Activate-PnPWebFeature
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web,
        [parameter(Mandatory = $true, ValueFromPipeline = $False)] $FeatureId
    )
 
    Try {
        Write-host -f Yellow "Trying to Activate Feature on:"$web.Url
        #Get the Feature to activate
        $Feature = Get-PnPFeature -Scope Web -Identity $FeatureId -ErrorAction Stop
  
        #Check if the Feature is Activate
        If($Feature.DefinitionId -eq $null)
        {    
            #Activate feature            
            Enable-PnPFeature -Scope Web -Identity $FeatureId -Force -Verbose -ErrorAction Stop
  
            Write-host -f Green "`tFeature Activated Successfully!"
        }
        Else
        {
            Write-host -f Cyan "`tFeature is already active!"
        }
    }
    Catch {
        write-host "`tError Activating Feature: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Connect to the site collection
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Call the Function for Root Web & all Subwebs
Get-PnPSubWeb -IncludeRootWeb -Recurse | ForEach-Object { Activate-PnPWebFeature $_ -FeatureId $FeatureId}

如果您的 Web 范围的功能对网站集功能有任何依赖性,您必须首先激活网站集功能!在我们的例子中,我们必须在网站集中激活发布功能,然后才能在所有网站上激活该功能。

PowerShell 在租户的所有站点上激活功能

如何为租户中的所有站点激活站点功能?以下 PowerShell 脚本在所有网站上启用给定功能,包括 SharePoint Online 租户中所有网站集的根网站及其子网站。


#Parameters
$TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
$FeatureId = "a7a2793e-67cd-4dc1-9fd0-43f61581207a" #"Following Content" Web Scoped Feature
 
#Function to Activate a Web Feature in SharePoint Online
Function Activate-PnPWebFeature
{ 
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true, ValueFromPipeline = $True)] $Web,
        [parameter(Mandatory = $true, ValueFromPipeline = $False)] $FeatureId
    )
 
    Try {
        Write-host -f Yellow "Trying to Activate Feature on:"$web.Url
        #Get the Feature to activate
        $Feature = Get-PnPFeature -Scope Web -Identity $FeatureId -ErrorAction Stop
 
        #Check if the Feature is Activate
        If($Feature.DefinitionId -eq $null)
        {
            #Activate the feature
            Enable-PnPFeature -Scope Web -Identity $FeatureId -Force -ErrorAction Stop 
            Write-host -f Green "`tFeature Activated Successfully!"
        }
        Else
        {
            Write-host -f Cyan "`tFeature is already active!"
        }
    }
    Catch {
        write-host "`tError Activating Feature: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Connect to Admin Center
$Cred = Get-Credential
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
 
#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SitesCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0","SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
 
#Loop through each site collection
ForEach($Site in $SitesCollections)
{
    #Connect to site collection
    $SiteConn = Connect-PnPOnline -Url $Site.Url -Credentials $Cred
 
    #Call the Function for Web & all Subwebs
    Get-PnPSubWeb -IncludeRootWeb -Recurse | ForEach-Object { Activate-PnPWebFeature $_ -FeatureId $FeatureId} 
}

如果您想要激活特定网站上的功能,请参阅本文,使用:SharePoint Online:使用 PowerShell 激活功能

总之,通过执行本文中提供的步骤,可以轻松地使用 PowerShell 激活所有 SharePoint Online 网站上的功能。通过连接到租户、检索网站列表、激活每个网站上的功能以及验证功能激活,您可以快速高效地管理您的 SharePoint Online 环境。

您还可以使用 PowerShell 与 SharePoint Online Management Shell 和 CSOM 在所有网站集中启用 SharePoint Online 功能,如我的另一篇文章中所示:PowerShell 为 SharePoint Online 中的所有网站启用网站集功能

相关文章