当我偶然发现某些PowerShell脚本删除SharePoint Online中文件的某些版本时,我正在寻找一种清理SharePoint的方法,以释放一些空间。假设您的租户设置为具有文件的500版本,您想删除旧文件的版本控制,这些旧文件只是您不想删除,但也不需要500版本。
话虽这么说,我正在努力与SharePoint Online连接,选择了特定的站点,特定的文件,然后删除所有版本。我在Internet上找到了该脚本,并正在对其进行研究,但是似乎我必须具备一些不符合使用该脚本的先决条件。例如,我不了解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用于本地SharePoint而不是SharePoint Online。
对于SharePoint Online,请在此处下载并安装SharePoint Online CSOM:
SharePoint Online Client Components SDK
安装后,PowerShell应该可以正常工作。