如何使用PowerShell从SharePoint下载文件? [英] How to use PowerShell to download files from SharePoint?

查看:235
本文介绍了如何使用PowerShell从SharePoint下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下网站来帮助我解决这一问题并进行故障排除。

I've used the following sites to help me get this far and to troubleshoot.


  1. 从SharePoint下载文件

  2. 如何使用PowerShell从SharePoint下载最新文件

  3. Mike Smith的SharePoint,PowerShell和.Net技术培训笔记!

  4. 通过PowerShell将文件上传到SharePoint文档库

  5. 从SharePoint文档中下载最新文件库

  6. 如何使用PowerShell迭代每个SharePoint网站中的每个文件夹

  7. PowerShell的Get-ChildItem位于SharePoint库

  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文件夹中下载随机文件,并且当我真正知道它时,它就可以正常工作文件名和扩展名。

I am trying to download random files from SharePoint folder, and I have it working for when I actually know the file name and extension.

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

Working code with name and extension:

$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)

但是,我似乎无法弄清楚

However, I don't seem to be able to figure out how to do it with multiple files of unknown names or extensions.

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

I have tried mapping a drive, only to end up with "drive mapping failed" & "The network path was not found." errors:

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

我还使用了以下代码,结果相似或完全没有结果。

I've also used the following code with similar results or no results at all.

#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片段。

Without defined "input parameters", it's not exactly clear the full solution you need so I'll provide a few snippets of PowerShell that should be of use based on what you've described.

我将为您提供各种OOTB功能(例如Get-SPWeb等)的基础知识,不过如果需要的话,也可以提供这些详细信息。尽管脚本中的某些行可以通过链接,管道传输等方式缩短和缩短,但我在脚本编写中也过于露骨。

I'll spare you the basics of the various OOTB functions (i.e. Get-SPWeb, etc) though can provide those details as well if needed. I've also been overly explicit in the scripting, though know some of these lines could be chained, piped, etc to be made shorter & more efficient.

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

This example will iterate over the contents of a SharePoint Library and download them to your local machine:

$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网站/网站的URL( $ Web )和文档库的名称(您的文档库名称)。

This is pretty basic; the variables up top are where on your local machine you wish to store the download files ($Destination), the URL of your SharePoint Site/Web ($Web) and the name of the Document Library (Your Doc Library Name).

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

The script then iterates through the items in the Library (foreach ($DocLibItem in $DocLibItems) {}), optionally filters for say items with a .docx file extension and downloads each to your local machine.

您可以通过定位文档中的特定子文件夹来进一步自定义此文件库,可以按文档的元数据或属性进行过滤,甚至可以在一个脚本中遍历多个站点,网站和/或库,还可以根据相似的属性进行过滤。

You could customize this further by targeting a specific sub-folder within the Doc Library, filter by metadata or properties of the Docs or even iterate over multiple Sites, Webs and/or Libraries in one script, optionally filtering those based on similar properties.

这篇关于如何使用PowerShell从SharePoint下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆