当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 从 CSV 文件下载 SharePoint 文档

作者:精品下载站 日期:2024-12-15 00:03:20 浏览:13 分类:玩电脑

使用 PowerShell 从 CSV 文件下载 SharePoint 文档


要求:将 SharePoint 文档库中的文件从 CSV 文件下载到本地磁盘。

从 SharePoint 下载文档的 PowerShell 脚本:

如果您需要快速轻松地从 CSV 文件下载 SharePoint 文档,PowerShell 就是您的最佳工具。本快速指南将向您展示如何从 CSV 文件导入数据,然后使用 PowerShell 从 CSV 文件下载文档。

我的 CSV 中有“URL”和“文件名”列,下面是用于将 SharePoint 文档从 CSV 文件下载到本地磁盘或网络驱动器的 PowerShell。


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
}

该脚本在下载文档时会在屏幕上显示进度并显示消息。

使用 PowerShell 从 CSV 下载文件

在另一种情况下,我将列表项 ID 存储在 CSV 文件中,并希望从 SharePoint 文档库中的 CSV 下载文件。


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++
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯