SharePoint Online:使用 PowerShell 从 CSV 下载多个文件
要求:从 SharePoint Online 中的 CSV 文件下载所有文件。
用于从 SharePoint Online 中的 CSV 批量下载所有文件的 PowerShell 脚本
当您需要从 SharePoint Online 网站下载大量文件时,您可以使用 CSV 和 PowerShell 自动执行该过程。在这篇博文中,我们将引导您完成从 SharePoint Online 网站下载多个文件的过程。我们将使用 CSV 文件作为下载源,并使用 PowerShell 从 SharePoint Online 下载文件。
我有许多 Excel 文件,想将它们全部下载到我的本地计算机上。这是我的 CSV 文件 - 一个制表符分隔的文本文件(因为文件名中可能包含逗号,CSV 会损坏!)
此 PowerShell 脚本从给定的 SharePoint Online 网站下载所有文件。创建所有可能失败的日志文件,例如,该文件在站点上不存在!
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/KPMG"
$CSVFile = "C:\Temp\KPMG.txt" #Tab Separated file
$DownloadPath = "C:\Temp\KPMG"
$LogFile = "C:\Temp\Error.log"
Try {
#Get Data from CSV File
$CSVData = Import-CSV -path $CSVFile -Encoding UTF8 -Delimiter "`t"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
#Iterate through each Row in the CSV
foreach ($Row in $CSVData)
{
#Prepare the Local Folder
$FileDownloadPath = $DownloadPath + $Row.ServerRelativeURL.Replace("/","\")
$FileName = Split-Path $FileDownloadPath -leaf
$ParentFolder = Split-Path $FileDownloadPath -Parent
#Create the Target Folder if it doesn't exists
If(!(test-path $ParentFolder))
{
New-Item -ItemType Directory -Force -Path $ParentFolder | Out-Null
}
Try {
Get-PnPFile -Url $Row.ServerRelativeURL -Path $ParentFolder -AsFile -ErrorAction Stop
Write-host "Downloaded File" $Row.ServerRelativeURL
}
Catch {
write-host -f Red "Error Downloading File $($Row.ServerRelativeURL) :" $_.Exception.Message
$Row.ServerRelativeURL | Out-File $LogFile -Append
}
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
