繁体   English   中英

删除 SharePoint 在线文件版本控制 PowerShell

[英]Delete SharePoint Online file versioning with PowerShell

当我偶然发现一些 PowerShell 脚本来删除 SharePoint 中的某些文件版本时,我正在寻找一种清理 SharePoint 以释放一些空间的方法。 假设您的租户设置了一个文件的 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,它按预期工作:

在此处输入图像描述

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

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

SharePoint 在线客户端组件 SDK

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM