从 Sharepoint 下载特定文件夹的 Powershell 脚本

Powershell script to Download Specific folders from Sharepoint

我有一个脚本可以成功地从 Sharepoint 站点下载所有内容,但我想更改它,以便它只从具有特定名称的特定文件夹下载内容。

这是我正在使用的脚本:

#Function to Download All Files from a SharePoint Online Folder - Recursively 
Function Download-SPOFolder([Microsoft.SharePoint.Client.Folder]$Folder, $DestinationFolder)
{ 
    #Get the Folder's Site Relative URL
    $FolderURL = $Folder.ServerRelativeUrl.Substring($Folder.Context.Web.ServerRelativeUrl.Length)
    $LocalFolder = $DestinationFolder + ($FolderURL -replace "/","\")
    #Create Local Folder, if it doesn't exist
    If (!(Test-Path -Path $LocalFolder)) {
            New-Item -ItemType Directory -Path $LocalFolder | Out-Null
            Write-host -f Yellow "Created a New Folder '$LocalFolder'"
    }
           
    #Get all Files from the folder
    $FilesColl = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File
    #Iterate through each file and download
    Foreach($File in $FilesColl)
    {
        Get-PnPFile -ServerRelativeUrl $File.ServerRelativeUrl -Path $LocalFolder -FileName $File.Name -AsFile -force
        Write-host -f Green "`tDownloaded File from '$($File.ServerRelativeUrl)'"
    }
    #Get Subfolders of the Folder and call the function recursively
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder
    Foreach ($Folder in $SubFolders | Where {$_.Name -ne "Forms"})
    {
        Download-SPOFolder $Folder $DestinationFolder
    }
} 
 
#Set Parameters
$SiteURL = "https://zietza.sharepoint.com/sites/tm_Finance"
$LibraryURL = "/Shared Documents/Budgets/" #Site Relative URL
$DownloadPath ="C:\Reports\"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin 
 
#Get The Root folder of the Library
$Folder = Get-PnPFolder -Url $LibraryURL
 
#Call the function to download the document library
Download-SPOFolder $Folder $DownloadPath

这是我的 Sharepoint 目录结构:

/Shared Documents/Budgets/
--------------------------
/2022/January/2022-January.xls
/2022/February/2022-February.xls
up to December.
/2023/January/2023-January.xls
/2023/February/2023-February.xls
up to December.
/2024/
/2025/
etc

所以我只想下载“一月”文件夹的内容 并以 2022-January.xls2023-January.xls2024 结束-January.xls 等在我的下载文件夹 "C:\Reports"

我搜索过类似的脚本,知道它应该是这样的:

Foreach ($Folder in $SubFolders | Where-Object { $_.Name -like "January*"})

但无法下载文件夹的内容。

非常感谢任何正确方向的帮助。

干杯, 齐特扎

以下是我将如何实现您的要求:

$LocalFolder = 'C:\Reports'

$UrlSharepoint = 'https://zietza.sharepoint.com'
$UrlSite = '/sites/tm_Finance'
$LibraryFolder = '/Shared Documents/Budgets'

# Connect to Sharepoint and save the connection
$Connection = Connect-PnPOnline -Url "${UrlSharepoint}${UrlSite}" `
                    -Credential $Credential `
                    -ReturnConnection `
                    -ErrorAction Stop
try {
    Get-PnPFolderItem -FolderSiteRelativeUrl $LibraryFolder `
                      -Connection $Connection `
                      -ItemType File `
                      -Recursive | 
        Where-Object {$_.ServerRelativeUrl -like "*/January/*-January.xls" } | 
        Select-Object name, @{
                                name='SiteRelativePath'
                                expr={$_.ServerRelativeUrl.Substring($UrlSite.Length)} 
                            } | 
        ForEach-Object {
            Get-PnPFile -Url $_.SiteRelativePath `
                        -AsFile `
                        -Path $LocalFolder `
                        -Filename $_.name `
                        -Connection $Connection
        }
}
finally {
    # Make sure to disconnect
    Disconnect-PnpOnline -Connection $Connection
}

备注:

  • 我使用 Get-PnPFolderItem-Recursive 参数来检索位于 $LibraryFolder 及其子文件夹中的所有文件(比必须自己递归文件夹更容易,并且节省 back-and-forth 客户端和服务器之间的通信)

  • 只保留匹配通配符 */January/*-January.xls 的文件。

  • 文件被检索并直接存储到 $LocalFolder

  • 我连接了预定义的 $Credential,但如果需要,您可以使用 -UseWebLogin 参数,例如2FA认证。只需确保保留 -ReturnConnection 参数即可。