【问题标题】:Delete SharePoint Online file versioning with PowerShell使用 PowerShell 删除 SharePoint Online 文件版本控制
【发布时间】:2020-09-07 07:48:55
【问题描述】:

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

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

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 sharepoint versioning


    【解决方案1】:

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

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

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

    SharePoint Online Client Components SDK

    安装后,PowerShell 应该可以工作了。

    【讨论】:

    • 您好,我已经安装了您提供的 SharePoint Online 客户端组件 SDK,很遗憾,它仍然无法在我这边工作。您介意告诉我是否还有其他先决条件吗?您是在常规 PowerShell 终端还是在 SharePoint Online 管理 shell 中运行脚本?
    • 你这边有错误提示吗? PowerShell 实际上在我身边工作。我在没有更多配置的普通 PowerShell ISE 中运行。如果您仍然遇到问题,请尝试下载 SharePoint Online Manageshell 并直接在其中运行 PowerShell:microsoft.com/en-us/download/details.aspx?id=35588 这是可以直接运行 SharePoint Online PowerShell 的 managementshell。
    • 我已经尝试了所有这些,脚本实际上正在运行并且我相信它正在工作,但是我在执行过程中被停止并出现以下错误消息ExecuteQuery" with "0" argument(s): "Access denied. You do not have permission to perform this action or access this resource.
    • 您在目标 SharePoint 网站和列表中是否拥有足够的权限?尝试使用完全控制帐户登录。
    • 您好,更新代码后,终于可以运行了。我终于意识到我的文件路径是错误的,因为它在我的资源管理器中显示为 /Site/Document/whatever_here 但实际路径是 /Site/Share Documents/whatever_here 这有很大的不同!我觉得自己很愚蠢,但至少它现在可以工作了!
    猜你喜欢
    • 2018-02-12
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2017-11-04
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多