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

[玩转系统] SharePoint Online:使用 PowerShell 设置版本控制限制

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

SharePoint Online:使用 PowerShell 设置版本控制限制


要求:在 SharePoint Online 文档库中设置版本历史记录限制。

如何在 SharePoint Online 中配置版本历史记录限制?

默认情况下,在 SharePoint Online 中创建的任何文档库都启用版本历史记录限制为 500。但是,有时您可能需要增加或减少为给定文档库存储的版本数限制。在本文中,我们将引导您完成在 SharePoint Online 中设置文档库的版本控制限制所需的步骤。

要设置 SharePoint Online 中库的版本数量限制,请执行以下操作:

  1. 转到您的文档库>>单击“设置”>>单击“库设置”。
  2. 在常规设置下,单击版本控制设置链接。
  3. 在“版本控制设置”页面中,选择“创建主要版本”,对于“保留以下数量的主要版本”,您可以设置版本数量的限制。

    [玩转系统] SharePoint Online:使用 PowerShell 设置版本控制限制

PowerShell 设置文档库的版本历史记录限制

使用 PowerShell,我们对特定文档库的版本历史记录应用限制:


#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
 
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Documents"
$VersioningLimit = 100

#Get 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 List - Versioning settings
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$List.Retrieve("EnableVersioning")
$Ctx.ExecuteQuery()
         
#Set version history limit
If($List.EnableVersioning)
{
    $List.MajorVersionLimit = $VersioningLimit
    $List.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Version History Settings has been Updated on '$($ListName)'"
}
Else
{
    Write-host -f Yellow "Version History is turned-off on '$($ListName)'"
}

使用 PnP PowerShell 可以完成同样的操作,如下所示:


$SiteURL = "https://crescent.sharepoint.com/sites/Funds"
$LibraryName = "Reports"

#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive

#Get the Library
$Library = Get-PnPList -Identity $LibraryName

#Set Version History Limit
Set-PnPList -Identity $Library -MajorVersions 5

如果您想减小网站的文件大小,或者您只是不需要或不想保留太多旧版本的文档,这会很有帮助。 SharePoint Online 网站中的所有文档库怎么样?让我们添加一些错误处理,将上面的脚本包装到一个函数中,并使其可重用。


#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
 
Function Set-SPOSiteVersionHistoryLimit()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [parameter(Mandatory=$false)][int]$VersioningLimit = 100
    )
    Try {
        #Get 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 All Lists from the Web
        $Lists = $Ctx.Web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()
         
        #Array to exclude system libraries
        $SystemLibraries = @("Form Templates", "Pages", "Preservation Hold Library","Site Assets", "Site Pages", "Images",
                            "Site Collection Documents", "Site Collection Images","Style Library")
         
        #Get All document libraries
        $DocumentLibraries = $Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $SystemLibraries}
        
        #Set Versioning Limits
        ForEach($Library in $DocumentLibraries)
        {
            #Get the Library's versioning settings
            $Library.Retrieve("EnableVersioning")
            $Ctx.ExecuteQuery()

            #powershell to set limit on version history
            If($Library.EnableVersioning)
            {
                #Set versioning limit
                $Library.MajorVersionLimit = $VersioningLimit
                $Library.Update()
                $Ctx.ExecuteQuery() 
                Write-host -f Green "Version History Settings has been Updated on '$($Library.Title)'"
            }
            Else
            {
                Write-host -f Yellow "Version History is turned-off at '$($Library.Title)'"
            }
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Call the function to set version history limit
Set-SPOSiteVersionHistoryLimit -SiteURL "https://Crescent.sharepoint.com"

此 PowerShell 脚本将所有文档库的主要版本限制设置为 100(来自可选参数)。

SharePoint Online:PnP PowerShell 对网站集中的所有文档库设置版本控制限制

这次,我们为网站集中的所有文档库配置版本历史记录的限制。


#Function to Set versioning limit on all lists in a web
Function Set-PnPVersionHistoryLimit
{
    param
    (
        [Parameter(Mandatory=$true)] $Web,
        [parameter(Mandatory=$false)][int]$VersioningLimit = 100
    )
    
    Try {
        Write-host "Processing Web:"$Web.URL -f Yellow
        Connect-PnPOnline -Url $Web.URL -Interactive

        #Array to exclude system libraries
        $SystemLibraries = @("Form Templates", "Pages", "Preservation Hold Library","Site Assets", "Site Pages", "Images",
                            "Site Collection Documents", "Site Collection Images","Style Library")
        
        $Lists = Get-PnPList -Includes BaseType, Hidden, EnableVersioning
        #Get All document libraries
        $DocumentLibraries = $Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $SystemLibraries}
        
        #Set Versioning Limits
        ForEach($Library in $DocumentLibraries)
        {
            #powershell to set limit on version history
            If($Library.EnableVersioning)
            {
                #Set versioning limit
                Set-PnPList -Identity $Library -MajorVersions $VersioningLimit
                Write-host -f Green "`tVersion History Settings has been Updated on '$($Library.Title)'"
            }
            Else
            {
                Write-host -f Yellow "`tVersion History is turned-off at '$($Library.Title)'"
            }
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message
    }
}

#Parameters
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"
    
#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get webs of the Site Collection
$Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb
 
ForEach($Web in $Webs)
{ 
    Set-PnPVersionHistoryLimit -Web $Web
}

总之,在 SharePoint Online 中设置版本控制限制有助于控制旧版本文档使用的存储量,并保持环境平稳运行。通过使用 PowerShell,您可以轻松配置特定库或列表的版本控制限制,从而更好地控制 SharePoint 环境。通过遵循本指南中概述的步骤,您现在应该能够使用 PowerShell 在 SharePoint Online 中设置版本控制限制。设置版本控制限制时,重要的是要考虑组织的需求,例如合规性需要多少个版本或有多少存储空间可用。此外,定期检查版本控制限制设置是一个很好的做法,以确保它们仍然满足组织的需求。

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

取消回复欢迎 发表评论:

关灯