ดาวน์โหลดเอกสาร SharePoint จากไฟล์ CSV โดยใช้ PowerShell
ข้อกำหนด: ดาวน์โหลดไฟล์ในไลบรารีเอกสาร SharePoint จากไฟล์ CSV ไปยังดิสก์ในเครื่อง
สคริปต์ PowerShell เพื่อดาวน์โหลดเอกสารจาก SharePoint :
หากคุณต้องการดาวน์โหลดเอกสาร SharePoint จากไฟล์ CSV อย่างรวดเร็วและง่ายดาย PowerShell คือเครื่องมือสำหรับคุณ คู่มือฉบับย่อนี้จะแสดงวิธีการนำเข้าข้อมูลจากไฟล์ CSV จากนั้นดาวน์โหลดเอกสารจากไฟล์ CSV โดยใช้ PowerShell
CSV ของฉันมีคอลัมน์ “URL” และ “ชื่อไฟล์” อยู่ในนั้น และนี่คือ PowerShell สำหรับดาวน์โหลดเอกสาร SharePoint จากไฟล์ CSV ไปยังดิสก์ในเครื่องหรือไดรฟ์เครือข่าย
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
#Set Parameters
$SiteURL = "https://intranet.crescent.com"
$CSVFilePath = "C:\Docs\DocList.csv"
$DownloadPath = "C:\Docs\Download\"
$Counter = 1
Try {
#Get the data from CSV file
$Web = Get-SPWeb $SiteURL
$CSVFile = Import-CSV $CSVFilePath
#Read CSV file and download each file
ForEach($Row in $CSVFile)
{
#Get the File
$File = $Web.GetFile($Row.URL)
#Download the File
$Data = $File.OpenBinary()
$FilePath= Join-Path $DownloadPath $Row.FileName
[System.IO.File]::WriteAllBytes($FilePath, $Data)
Write-host -f Green "`tDownloaded the File ($Counter of $($CSVFile.Count)):"$File.ServerRelativeURL
$Counter++
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}
สคริปต์แสดงความคืบหน้าบนหน้าจอพร้อมข้อความขณะดาวน์โหลดเอกสาร
ดาวน์โหลดไฟล์จาก CSV โดยใช้ PowerShell
ในอีกสถานการณ์หนึ่ง ฉันมี List Item Ids เก็บไว้ในไฟล์ CSV และต้องการดาวน์โหลดไฟล์จาก CSV จากไลบรารีเอกสาร SharePoint
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
#Parameters
$SiteURL = "https://crescent.sharepoint.com"
$CSVFilePath = "C:\Temp\FilesList.csv"
$DownloadPath = "\\fileserver\WFDocs\"
$ListName ="WorkflowAttachments"
#Get the data from CSV file
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists[$ListName]
$CSVFile = Import-CSV $CSVFilePath
$Counter =1
#Read CSV file and download each file
ForEach($Row in $CSVFile)
{
#Get the List Item by ID from CSV
$ListItem = $List.GetItemByID($Row.ID)
#Get the File
$File = $ListItem.File
#Download the File
$Filename = $File.Name
$Data = $File.OpenBinary()
$FilePath= Join-Path $DownloadPath $Filename
[System.IO.File]::WriteAllBytes($FilePath, $Data)
Write-host -f Green "`tDownloaded the File: $FileName ($Counter of $($CSVFile.Count)) at "$File.ServerRelativeURL
$Counter++
}