gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:15:56 25 4
gpt4 key购买 nike

我已经使用以下网站来帮助我做到这一点并进行故障排除。

  • Download file from SharePoint
  • How to download newest file from SharePoint using PowerShell
  • Mike Smith's Tech Training Notes SharePoint, PowerShell and .Net!
  • Upload file to a SharePoint doc library via PowerShell
  • Download latest file from SharePoint Document Library
  • How to iterate each folders in each of the SharePoint websites using PowerShell
  • 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 片段。

    我会为您介绍各种 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 站点/网站的 URL ( $Web ) 和文档库的名称 ( Your Doc Library Name )。

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

    您可以通过定位文档库中的特定子文件夹,按文档的元数据或属性进行过滤,甚至在一个脚本中迭代多个站点、网站和/或库,还可以根据相似的属性选择性地过滤这些内容,从而进一步自定义。

    关于powershell - 如何使用 PowerShell 从 SharePoint 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350575/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com