繁体   English   中英

清空共享点在线文件夹

[英]Empty sharepoint online folder

我正在寻找一种每天一次自动清空 sharepoint online 文件夹的方法。 我认为最好的方法是使用来自本地服务器的 powershell 脚本。 我试图在谷歌上搜索这个,但后来我只能找到如何删除文件夹。 但我想删除文件夹中的所有内容,而不是文件夹本身。 有没有人知道一个好的解决方案或powershell脚本?

好吧,我从 Theo 提供的 URL 中获得了一个有效的 powershell 脚本:[ https://www.sharepointdiary.com/2018/05/sharepoint-online-delete-all-files-in-document-library-using-powershell.html ][1]

脚本如下所示:

#Load SharePoint CSOM Assemblies
#Download Sharepoint CSOM Assemblies from https://www.microsoft.com/en-us/download/details.aspx?id=42038
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"

#Variables for Processing
$SiteURL = "https://tenantname.sharepoint.com/sites/TestTeam"
$LibraryRelativeURL = "/sites/TestTeam/Shared Documents/TEST/TestFolder"

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

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

#Get the Library and Files
$Folder=$Ctx.Web.GetFolderByServerRelativeUrl($LibraryRelativeURL)
$Ctx.Load($Folder)
$Files = $Folder.Files
$Ctx.Load($Files)
$Ctx.ExecuteQuery()

#Iterate through each File in the Root folder
Foreach($File in $Files)
{
    #Delete the file
    $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null
    Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
}
$Ctx.ExecuteQuery()

#Process all Sub Folders
$SubFolders = $Folder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
    #Exclude "Forms" and Hidden folders
    If( ($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
    {
        #Delete the folder
        $Folder.Folders.GetByUrl($SubFolder.ServerRelativeUrl).Recycle()| Out-Null
        Write-host -f Green "Deleted Folder '$($SubFolder.Name)' from '$($SubFolder.ServerRelativeUrl)'"
    }
    $Ctx.ExecuteQuery()
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}

暂无
暂无

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

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