空共享点联机文件夹

时间:2020-01-03 10:19:25

标签: powershell sharepoint

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

1 个答案:

答案 0 :(得分:0)

好吧,我从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
}
相关问题