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

[玩转系统] SharePoint Online:使用 PowerShell 删除版本历史记录

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

SharePoint Online:使用 PowerShell 删除版本历史记录


SharePoint Online 中的版本控制功能允许您访问文档或列表项的先前副本。默认情况下,SharePoint Online 文档库启用版本控制。当用户进行编辑时,SharePoint 会自动创建一个包含元数据(例如创建者、时间戳等)的新版本。

如何删除 SharePoint Online 中的版本历史记录?

在这篇博文中,我们将了解如何删除 SharePoint Online 中的版本历史记录。如果您需要清除占用存储空间的旧版本文件,或者只是想清理以前的版本,这会派上用场。我们还将向您展示如何使用 PowerShell 快速轻松地删除 SharePoint Online 中的版本历史记录。

要删除 SharePoint 中文档的所有早期版本,请执行以下步骤:

  1. 导航到您的 SharePoint 库,选择文件,然后从功能区菜单中单击“版本历史记录”。

    [玩转系统] SharePoint Online:使用 PowerShell 删除版本历史记录

  2. 在版本历史记录页面中,单击“删除所有版本”链接,并根据提示进行确认!

    [玩转系统] SharePoint Online:使用 PowerShell 删除版本历史记录

SharePoint Online:使用 PowerShell 删除版本历史记录

您可以删除文档的所有版本,也可以保留最新的“N”个版本,然后删除其余版本。以下是用于删除 SharePoint Online 中的版本的 PowerShell 脚本:

情况 1:删除特定文件的所有版本:

此 PowerShell 脚本删除 SharePoint Online 中的所有版本。


#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"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$FileURL="/Sites/Sales/ProjectDocuments/ProjectMetrics.xlsx"

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

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)

    #Get all versions of the file
    $Versions = $File.Versions
    $Ctx.Load($Versions)
    $Ctx.ExecuteQuery()

    Write-host -f Yellow "Total Number of Versions Found :" $Versions.count

    #Delete all versions of the file
    $Versions.DeleteAll()
    $Ctx.ExecuteQuery()

    Write-host -f Green "All versions Deleted for given File!"   
}
Catch {
    write-host -f Red "Error deleting versions!" $_.Exception.Message
}

案例 2:删除库中所有文件的所有版本:

此 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"
    
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$LibraryName="Branding"
$BatchSize = 500
  
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web and Library
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($LibraryName)
   
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>" 
    Do {
        #Get items from the list in batches
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    
        #Loop through each file in the library
        Foreach($Item in $ListItems | Where {$_.FileSystemObjectType -eq "File"})
        {
            #Get all versions of the file
            $Ctx.Load($Item.File)
            $Ctx.Load($Item.File.Versions)
            $Ctx.ExecuteQuery()
            Write-host "Processing Item:" $Item.file.Name
  
            #Delete all versions of the file
            If($Item.File.Versions.count -gt 0)
            {
                $Item.File.Versions.DeleteAll()
                $Ctx.ExecuteQuery()
                Write-host -f Green "`tAll Versions deleted on "$Item.file.Name
            }
        }
    } While ($Query.ListItemCollectionPosition -ne $null)
}
Catch {
    write-host -f Red "Error Deleting version History!" $_.Exception.Message
}

情况 3:保留最后五个版本并删除库中的其余版本:

这次,我们只保留最后“N”个版本,并修剪其余的版本。


#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"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$LibraryName="Project Documents"
$VersionsToKeep=5
$BatchSize = 500

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

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web and Library
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($LibraryName)
 
    #Get all items from the library
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>" 
    Do {
        #Get items from the list in batches
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
 
        #Loop through each file in the library
        Foreach($Item in $ListItems | Where {$_.FileSystemObjectType -eq "File"})
        {
            #Get all versions of the file
            $Versions = $Item.File.Versions
            $Ctx.Load($Versions)
            $Ctx.Load($Item.File) #To get File Name
            $Ctx.ExecuteQuery()

            Write-host -f Yellow "Total Number of Versions Found in '$($Item.File.Name )' : $($Versions.count)"

            #Check if number of versions are more than limit
            While($Item.File.Versions.Count -gt $VersionsToKeep)
            {
                $Versions[0].DeleteObject()
                $Ctx.ExecuteQuery()
                Write-host -f Green "`tDeleted Version:" $Versions[0].VersionLabel
    
                #Reload versions
                $Ctx.Load($Item.File.Versions)
                $Ctx.ExecuteQuery()
            }
        }
    } While ($Query.ListItemCollectionPosition -ne $null)
}
Catch {
    write-host -f Red "Error deleting versions!" $_.Exception.Message
}

修剪站点中所有库的多余版本

对于 SharePoint Online 网站的所有文档库中的所有文件,我们保留最后“N”个版本并删除其余版本:


#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 Cleanup-SPOVersions
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [parameter(Mandatory=$false)][int]$VersionsToKeep = 100
    )
  
    #Config Parameters
    $BatchSize = 500

    #Exclude certain libraries
    $ExcludedLibraries = @("Form Templates", "Preservation Hold Library", "Site Assets","Site Pages", "Images",
                                "Site Collection Documents", "Site Collection Images","Style Library")
 
    #Setup Credentials to connect
    $Cred = Get-Credential
 
    Try {
        #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 and Libraries
        $Web=$Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Lists)
        $Ctx.executeQuery()

        #Get all document libraries in sharepoint online site
        $DocLibraries = $Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLibraries}

        #Loop through each document library and Get the Title
        Foreach ($Library in $DocLibraries)
        { 
            Write-host "Processing Document Library:"$Library.Title -f Yellow
            #Get all items from the library
            $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
            $Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
            Do {
                #Get items from the library in batches
                $ListItems = $Library.GetItems($Query)
                $Ctx.Load($ListItems)
                $Ctx.ExecuteQuery()
                $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
  
                #Loop through each file in the library
                Foreach($Item in $ListItems | Where {$_.FileSystemObjectType -eq "File"})
                {
                    #Get all versions of the file
                    $Versions = $Item.File.Versions
                    $Ctx.Load($Versions)
                    $Ctx.Load($Item.File) #To get File Name
                    $Ctx.ExecuteQuery()

                    #Calculate if versions to be cleaned for the file
                    $VersionsToDelete = $Versions.Count - $VersionsToKeep
                    If($VersionsToDelete -gt 0)
                    { 
                        Write-host -f Yellow "`tTotal Number of Versions Found in '$($Item.File.ServerRelativeUrl)' : $($Versions.count)"
 
                        #Check if number of versions are more than limit
                        While($Item.File.Versions.Count -gt $VersionsToKeep)
                        {
                            $Versions[0].DeleteObject()
                            $Ctx.ExecuteQuery()
                            Write-host -f Green "`tDeleted Version:" $Versions[0].VersionLabel
     
                            #Reload versions
                            $Ctx.Load($Item.File.Versions)
                            $Ctx.ExecuteQuery()
                        }
                    }
                }
            } While ($Query.ListItemCollectionPosition -ne $null)
        }
    }
    Catch {
        write-host -f Red "Error deleting versions!" $_.Exception.Message
    }
}

#Call the function to cleanup versions
Cleanup-SPOVersions -SiteURL "https://crescent.sharepoint.com/sites/marketing" -VersionsToKeep 10

如果您想保留特定文件的最后五个版本并删除其余版本,请使用:


    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)

    #Get all versions of the file
    $Versions = $File.Versions
    $Ctx.Load($Versions)
    $Ctx.ExecuteQuery()
    $Versionscount = $Versions.count

    Write-host -f Yellow "Total Number of Versions Found :" $Versionscount

    #Check if number of versions are more than limit
    While($File.Versions.Count -gt $VersionsToKeep)
    {
        write-host "Deleting Version:" $Versions[0].VersionLabel
        $Versions[0].DeleteObject()
        $Ctx.ExecuteQuery()
    
        #Reload versions
        $Ctx.Load($File.Versions)
        $Ctx.ExecuteQuery()
    }

要按标签删除特定版本,请使用:$Versions.DeleteByLabel($LabelID)

您还可以使用 PnP PowerShell 删除 SharePoint Online 中的版本:SharePoint Online:使用 PnP PowerShell 删除版本历史记录

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

取消回复欢迎 发表评论:

关灯