2017-04-11 87 views
1

我使用了以下網站來幫助我解決這個問題並排除故障。如何使用PowerShell從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文件夾中下載隨機文件,並且當它確實知道文件名和擴展名時,我就可以使用它。

用名和擴展名工作代碼:

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

#Get User Information 
$user = Read-Host "Enter your username" 
$username = "[email protected]" 
$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 = "[email protected]" 
$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:[email protected] 

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

回答

1

沒有定義的「輸入參數」,它不完全清楚你需要,所以我會提供的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和文檔庫(Your Doc Library Name)的名稱的本地計算機上的變量。

該腳本隨後遍歷庫中的項目(foreach ($DocLibItem in $DocLibItems) {}),可選地篩選文件擴展名爲.docx的項目,並將每個項目下載到本地計算機。

您可以通過定位文檔庫中的特定子文件夾,按文檔的元數據或屬性進行過濾,甚至可以在一個腳本中迭代多個站點,Web和/或庫,然後根據相似的屬性。

+0

錯誤:Get-SPWeb:術語'Get-SPWeb'不被識別爲cmdlet,函數, 腳本文件或可操作程序的名稱。檢查名稱的拼寫,或者如果包含路徑 ,請驗證路徑是否正確,然後重試。 – Underdog

+0

您是使用本機PowerShell還是SharePoint Management Shell?如果是前者,則在使用任何SharePoint cmdlet – Stevangelista

+0

本機PowerShell ISE之前,需要先運行「Add-PSSnapin Microsoft.SharePoint.PowerShell」。我運行了「Add-PSSnapin Microsoft.SharePoint.PowerShell」,我仍然收到了完全相同的錯誤。 – Underdog

相關問題