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

[玩转系统] SharePoint Online:如何使用 PowerShell 应用网站策略?

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

SharePoint Online:如何使用 PowerShell 应用网站策略?


要求:使用 PowerShell 在 SharePoint Online 中应用网站策略。

如何使用 PowerShell 应用 SharePoint Online 网站策略?

网站策略是在 SharePoint Online 中实施治理的好方法。应用网站策略可以通过 SharePoint Online Web 浏览器界面来完成,但是,使用 PowerShell 可以更高效,并允许在大规模场景中实现自动化。在本指南中,我们将向您展示如何使用 PowerShell 在 SharePoint Online 中应用网站策略。

在 SharePoint Online 中创建网站策略

截至今天,我们无法通过 PowerShell 创建新的站点策略!但是,我们可以通过在内容类型中心创建网站策略而不是在每个网站集创建网站策略来节省时间。以下是在 SharePoint Online 内容类型中心创建网站策略的方法:

  1. 登录到您租户的内容类型中心:https://.sharepoint.com/sites/contentTypeHub
  2. 单击右上角的“设置”齿轮 >> 选择“站点设置”。
  3. 单击“网站集管理”部分下的“网站策略”链接。
  4. 根据要求创建新策略。我创建了一个策略,通过选择“不自动关闭或删除网站”并选中“网站集关闭时将处于只读状态”,使网站集处于只读状态。

    [玩转系统] SharePoint Online:如何使用 PowerShell 应用网站策略?

  5. 创建网站策略后,您必须将其发布到网站集。在内容类型中心的“网站策略”页面中,单击刚刚创建的网站策略旁边的“管理此策略的发布”链接。
  6. 选择“发布”或“重新发布”选项将您的网站策略推送到所有 SharePoint Online 网站集。

发布后,该策略需要一段时间才会出现在网站集中,并且您可能需要等待长达 24 小时。网站政策发布后,您应该会在目标网站集的“网站政策”页面上看到它。

如果尚未激活,请激活“站点策略”功能

必须在网站集级别激活“网站策略”功能才能应用该策略。如果尚未激活(在群组站点中,默认情况下禁用它!),请按照以下步骤操作:

  1. 转到设置>>站点设置>>单击“站点集功能”链接。
  2. 单击站点策略功能的“活动”按钮。

    [玩转系统] SharePoint Online:如何使用 PowerShell 应用网站策略?

将网站策略应用于网站集

使用 Web 界面应用站点策略非常简单。转到站点设置以应用站点策略,然后单击站点管理下的“站点关闭和删除”链接。选择您创建的站点策略并单击“确定”按钮。返回“网站关闭和删除”页面,单击“立即关闭此网站”按钮以关闭网站集。关闭网站集后将应用选定的网站策略。

PowerShell 将网站策略应用到网站集:

好吧,当您需要为大量站点应用站点策略时,我们可以使用 PowerShell 而不是 Web UI。一旦网站策略在目标网站中可用,我们就可以通过 PowerShell 将其应用到网站集。以下是要应用的 CSOM PowerShell 脚本:


#Load SharePoint Online 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"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.Office.Client.Policy.dll"

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PolicyName = "Set Site Read-Only"

#Setup Credentials to connect
$Cred = Get-Credential
     
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Get Site Policies
$Policies = [Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::GetProjectPolicies($Ctx, $Web)
$Ctx.Load($Policies)
$Ctx.ExecuteQuery()

#Get the Policy to Apply 
$SitePolicy =  $Policies | Where -Property Name -eq $PolicyName
If($SitePolicy)
{
    #Apply the policy
    [Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::ApplyProjectPolicy($Ctx, $Web, $SitePolicy)
    $Web.Update()
    $Ctx.ExecuteQuery()
    
    #Close the Site
    [Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::CloseProject($Ctx,$Web)
    $Ctx.ExecuteQuery()

    Write-host "Site Policy has been Applied & Site Closed Successfully!" -f Green
}

要再次打开该站点,请使用以下命令:


[Microsoft.SharePoint.Client.InformationPolicy.ProjectPolicy]::UnArchiveSite($ctx,$SiteURL)

PnP PowerShell 在 SharePoint Online 中应用站点策略

现在,让我们使用 PnP PowerShell 激活网站集中的“网站策略”功能(如果尚未激活),然后将“网站策略”应用到给定的网站集。


#Apply Site Policy for a Site collection
Function Apply-PnPSitePolicy([String]$SiteUrl, [String]$PolicyName)
{
    #Connect to the Site 
    Connect-PnPOnline -Url $SiteUrl -Interactive

    #Check if "Site Policy" Feature is active
    $SitePolicyFeature = Get-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site -Web $SiteUrl
    If($SitePolicyFeature.DefinitionId -eq $null)
    {
        #Activate "Site Policy" Feature for the site collection
        Enable-PnPFeature -Identity "2fcd5f8a-26b7-4a6a-9755-918566dba90a" -Scope Site
        Write-Host "Site Policy Feature is Activated at $($SiteUrl)" -ForegroundColor Green
    }
    
    #Get Policy to Activate
    $SitePolicyToActivate = Get-PnPSitePolicy -Name $PolicyName | Select-Object -Property Name
    If ($SitePolicyToActivate)
    {
        #Apply Site Policy
        Set-PnPSitePolicy -Name $PolicyName

        #Close the site
        Set-PnPSiteClosure -State Closed

        Write-Host "Site Policy Applied to $($SiteUrl)" -ForegroundColor Green
    }
    Else
    {
        write-Host "Site Policy '$($PolicyName)' not found in Site $($SiteUrl)" -ForegroundColor Yellow
    }
}

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Purchase"
$PolicyName= "Set Site Read-Only"

#Call the function to apply site policy
Apply-PnPSitePolicy -SiteUrl $SiteURL -PolicyName $PolicyName

如果您刚刚激活“网站策略”功能,在内容类型中心创建的策略不会立即显示在网站集中!您可能需要等待长达 24 小时。

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

取消回复欢迎 发表评论:

关灯