使用 PowerShell 删除 SharePoint Online 文件版本控制

Delete SharePoint Online file versioning with PowerShell

我正在寻找一种方法来清理 SharePoint 以释放一些 space,这时我偶然发现了一些 PowerShell 脚本可以删除 SharePoint Online 中文件的某些版本。 假设您的租户设置了一个文件的 500 个版本,您想要删除您不想删除但也不需要它的 500 个版本的旧文件的版本控制。

话虽如此,我正在为如何连接 SharePoint Online、选择特定站点、特定文件然后删除所有版本而苦恼。 我在 Internet 上找到了这个脚本,并且正在研究它,但似乎我应该有一些我不满足的先决条件才能使用这个脚本。 例如,我不理解 Add-Type -Path 部分,据我了解,此脚本适用于 SharePoint 服务器,而不适用于 SharePoint Online。

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
}

特别是在使用 Get-SPWeb cmdlet 时,我收到此错误消息:

Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-SPWeb
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

任何帮助和澄清将不胜感激,提前致谢!

在我这边测试了相同的 PowerShell,它按预期工作:

在原始问题的错误消息中,Get-SPWeb 用于本地 SharePoint 而不是 SharePoint Online。

对于 SharePoint Online,请在此处下载并安装 SharePoint Online CSOM:

SharePoint Online Client Components SDK

安装后,PowerShell 应该可以运行了。