【问题标题】:How to use PowerShell to download files from SharePoint?如何使用 PowerShell 从 SharePoint 下载文件?
【发布时间】:2017-04-12 08:58:03
【问题描述】:

我使用以下网站来帮助我实现这一目标并进行故障排除。

  1. Download file from SharePoint
  2. How to download newest file from SharePoint using PowerShell
  3. Mike Smith's Tech Training Notes SharePoint, PowerShell and .Net!
  4. Upload file to a SharePoint doc library via PowerShell
  5. Download latest file from SharePoint Document Library
  6. How to iterate each folders in each of the SharePoint websites using PowerShell
  7. PowerShell's Get-ChildItem on SharePoint Library

我正在尝试从 SharePoint 文件夹下载随机文件,并且当我真正知道文件名和扩展名时,它就可以正常工作。

带有名称和扩展名的工作代码:

$SharePoint = "https://Share.MyCompany.com/MyCustomer/WorkLoad.docx"
$Path = "$ScriptPath\$($CustomerName)_WorkLoad.docx"

#Get User Information
$user = Read-Host "Enter your username"
$username = "$user@MyCompany"
$password = Read-Host "Enter your password" -AsSecureString

#Download Files
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($UserName, $Password)
$WebClient.DownloadFile($SharePoint, $Path)

但是,我似乎无法弄清楚如何处理多个未知名称或扩展名的文件。

我已尝试映射驱动器,但结果是“驱动器映射失败”和“找不到网络路径”。错误:

$SharePoint  = Read-Host 'Enter the full path to Delivery Site'
$LocalDrive  = 'P:'
$Credentials = Get-Credential


if (!(Test-Path $LocalDrive -PathType Container)) {
    $retrycount = 0; $completed = $false
    while (-not $completed) {
        Try {
            if (!(Test-Path $LocalDrive -PathType Container)) {
                (New-Object -ComObject WScript.Network).MapNetworkDrive($LocalDrive,$SharePoint,$false,$($Credentials.username),$($Credentials.GetNetworkCredential().password))
            }
            $Completed = $true
        }
        Catch {
            if ($retrycount -ge '5') {
                Write-Verbose "Mapping SharePoint drive failed the maximum number of times"
                throw "SharePoint drive mapping failed for '$($SharePoint)': $($Global:Error[0].Exception.Message)"
            } else {
                Write-Verbose "Mapping SharePoint drive failed, retrying in 5 seconds."
                Start-Sleep '5'
                $retrycount++
            }
        }
    }
}

我也使用了以下代码,结果相似或根本没有结果。

#Get User Information
$user = Read-Host "Enter your username"
$username = "$user@MyCompany"
$password = Read-Host "Enter your password" -AsSecureString

#Gathering the location of the Card Formats and Destination folder
$Customer = "$SharePoint\MyCustomer"
$Products = "$Path\$($CustomerName)\Products\"

#Get Documents from SharePoint
$credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-PSDrive -Credential $credential -Name "A" -PSProvider "FileSystem" -Root "$SharePoint"
net use $spPath #$password /USER:$user@corporate

#Get PMDeliverables file objects recursively
Get-ChildItem -Path "$Customer" | Where-Object { $_.name -like 'MS*' } | Copy-Item -Destination $Products  -Force -Verbose

【问题讨论】:

    标签: powershell variables sharepoint download copy


    【解决方案1】:

    如果没有定义“输入参数”,您需要的完整解决方案并不完全清楚,因此我将提供一些 PowerShell 的 sn-ps,根据您所描述的内容应该可以使用。

    我将为您提供各种 OOTB 功能(即 Get-SPWeb 等)的基础知识,但如果需要,我也可以提供这些详细信息。我在脚本中也过于明确了,尽管我知道其中一些行可以被链接、管道等以变得更短、更高效。

    此示例将遍历 SharePoint 库的内容并将其下载到您的本地计算机:

    $Destination = "C:\YourDestinationFolder\ForFilesFromSP"
    $Web = Get-SPWeb "https://YourServerRoot/sites/YourSiteCollection/YourSPWebURL"
    $DocLib = $Web.Lists["Your Doc Library Name"]
    $DocLibItems = $DocLib.Items
    
    foreach ($DocLibItem in $DocLibItems) {
        if($DocLibItem.Url -Like "*.docx") {
            $File = $Web.GetFile($DocLibItem.Url)
            $Binary = $File.OpenBinary()
            $Stream = New-Object System.IO.FileStream($Destination + "\" + $File.Name), Create
            $Writer = New-Object System.IO.BinaryWriter($Stream)
            $Writer.write($Binary)
            $Writer.Close()
        }
    }
    

    这是非常基本的;顶部的变量是您希望在本地计算机上存储下载文件的位置 ($Destination)、您的 SharePoint 站点/Web 的 URL ($Web) 和文档库的名称 (Your Doc Library Name)。

    然后,该脚本会遍历库 (foreach ($DocLibItem in $DocLibItems) {}) 中的项目,可选择过滤带有 .docx 文件扩展名的项目并将每个项目下载到您的本地计算机。

    您可以通过定位文档库中的特定子文件夹来进一步自定义此设置,按文档的元数据或属性进行过滤,甚至在一个脚本中迭代多个站点、网站和/或库,可选择基于类似的过滤器属性。

    【讨论】:

    • 错误:Get-SPWeb:术语“Get-SPWeb”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确并重试。
    • 您使用的是本机 PowerShell 还是 SharePoint 命令行管理程序?如果是前者,您需要在使用任何 SharePoint cmdlet 之前先运行“Add-PSSnapin Microsoft.SharePoint.PowerShell”
    • 本机 PowerShell ISE。我运行了“Add-PSSnapin Microsoft.SharePoint.PowerShell”,但仍然遇到完全相同的错误。
    • 如果没有 SP cmdlet,您将面临挑战,或者至少会遇到更多错误 - 如果您无法加载它们,我会使用 SP Mgmt Shell
    • 请注意,the docs 说:“要使用 SharePoint Online PowerShell 命令,您必须是 SharePoint Online 全局管理员。”
    猜你喜欢
    • 2015-05-10
    • 2013-09-22
    • 2020-12-01
    • 2020-01-15
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    相关资源
    最近更新 更多