首页 文章

如何使用PowerShell从SharePoint下载文件?

提问于
浏览
2

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

我正在尝试从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

1 回答

  • 2

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

    我将为您提供各种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 文件扩展名的项目,并将每个项目下载到本地计算机 .

    您可以通过定位文档库中的特定子文件夹,按文档的元数据或属性进行过滤,甚至在一个脚本中迭代多个站点,Web和/或库,可选地根据类似属性过滤这些子文件夹来进一步自定义 .

相关问题