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

[玩转系统] SharePoint Online:使用 PowerShell 比较站点之间的功能

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

SharePoint Online:使用 PowerShell 比较站点之间的功能


您是否曾经想要比较 SharePoint 网站集或网站之间激活的功能?嗯,这个漂亮的 PowerShell 脚本正是这样做的!

[玩转系统] SharePoint Online:使用 PowerShell 比较站点之间的功能

用于比较 SharePoint Online 网站或网站集之间的功能的 PowerShell

要比较两个 SharePoint Online 网站中的可用功能,请执行以下步骤:

  1. 使用同一帐户登录到两个 SharePoint Online 网站。
  2. 导航到每个站点的站点设置页面。
  3. 在网站设置页面中,单击“网站操作”下的“网站功能”。
  4. 查看每个站点中激活的功能列表并记下差异。
  5. 对两个站点重复该过程。

让我们使用 PowerShell 自动化此过程来比较两个 SharePoint Online 网站中的功能!此 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"

Function Compare-SPOFeatures
{

[cmdletbinding()]

    Param(
        [Parameter(Mandatory=$True)][URI]$SourceSiteURL,
        [Parameter(Mandatory=$True)][URI]$TargetSiteURL,
        [Parameter(Mandatory=$True)][ValidateSet('Site','Web')][String]$Scope
    )

    #Get Credentials to connect
    $Cred = Get-Credential

    #Get Source and Target Site Contexts
    $SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL.AbsoluteUri)
    $SourceCtx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    $TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL.AbsoluteUri)
    $TargetCtx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
    #Get Features based on Given Scope
    Switch($Scope)
    {
        Site
        {
            Write-Host -f Yellow "Comparing Features Between Site Collections..."
            $SourceFeatures = $SourceCtx.Site.Features
            $TargetFeatures = $TargetCtx.Site.Features
            $SourceCtx.Load($SourceFeatures)
            $TargetCtx.Load($TargetFeatures)
            $SourceCtx.ExecuteQuery()
            $TargetCtx.ExecuteQuery()
        }
        Web
        {
            Write-Host -f Yellow "Comparing Features Between Sites..."
            $SourceFeatures = $SourceCtx.Web.Features
            $TargetFeatures = $TargetCtx.Web.Features
            $SourceCtx.Load($SourceFeatures)
            $TargetCtx.Load($TargetFeatures)
            $SourceCtx.ExecuteQuery()
            $TargetCtx.ExecuteQuery()
        }
    }

    $MismatchedFeatures = New-Object System.Collections.Arraylist
    ForEach($Feature in $SourceFeatures)
    {
        $Feature.Retrieve("DisplayName")
        $SourceCtx.Load($Feature)
        $SourceCtx.ExecuteQuery()

        If(!($TargetFeatures.DefinitionId -Match $Feature.DefinitionId))
        {
           $FeatureEntry = New-Object System.Object
           $FeatureEntry | Add-Member -MemberType NoteProperty -Name "Feature Name" -Value $Feature.DisplayName
           $FeatureEntry | Add-Member -MemberType NoteProperty -Name "Feature ID" -Value $Feature.DefinitionID
           $FeatureEntry | Add-Member -MemberType NoteProperty -Name "Activated In" -Value "Source Only"
           $MismatchedFeatures.Add($FeatureEntry) | Out-Null
        }
    }
    ForEach($Feature in $TargetFeatures)
    {
        $Feature.Retrieve("DisplayName")
        $TargetCtx.Load($Feature)
        $TargetCtx.ExecuteQuery()

        If(!($SourceFeatures.DefinitionId -Match $Feature.DefinitionId))
        {
           $FeatureEntry = New-Object System.Object
           $FeatureEntry | Add-Member -MemberType NoteProperty -Name "Feature Name" -Value $Feature.DisplayName
           $FeatureEntry | Add-Member -MemberType NoteProperty -Name "Feature ID" -Value $Feature.DefinitionID
           $FeatureEntry | Add-Member -MemberType NoteProperty -Name "Activated In" -Value "Target Only"
           $MismatchedFeatures.Add($FeatureEntry) | Out-Null
        }
    }
    Return $MismatchedFeatures
}

#Call the Function to compare features between site collections
Compare-SPOFeatures -SourceSiteURL "https://crescent.sharepoint.com/sites/marketing" -TargetSiteURL "https://crescent.sharepoint.com/sites/creditpipeline" -Scope Site  

您可以比较两个站点之间的“Web”范围和“Site”范围功能。

总之,比较两个 SharePoint Online 网站中激活的功能是了解两个网站之间的差异和相似之处的有用方法。此过程可以通过检查站点设置来手动执行,但是使用 Microsoft PowerShell 检索已激活功能的列表并进行比较将是有效的方法。此信息可用于识别某个站点缺少哪些功能,并决定是否激活它们。

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

取消回复欢迎 发表评论:

关灯