Rad*_*Fox 4 variables powershell sharepoint copy download
I've used the following sites to help me get this far and to troubleshoot.
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)
Run Code Online (Sandbox Code Playgroud)
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++
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
没有定义的“输入参数”,并不能完全清楚您需要的完整解决方案,因此,我将根据您的描述提供一些应使用的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()
}
}
Run Code Online (Sandbox Code Playgroud)
这是非常基本的。顶部的变量是您希望在本地计算机上存储下载文件($Destination),SharePoint网站/网站的URL($Web)和文档库的名称(Your Doc Library Name)的位置。
然后,脚本会遍历库(foreach ($DocLibItem in $DocLibItems) {})中的项目,可以选择过滤带有.docx文件扩展名的项目,并将每个项目下载到本地计算机。
您可以通过针对文档库中的特定子文件夹,按文档的元数据或属性进行过滤,甚至在一个脚本中遍历多个站点,网站和/或库来进行自定义,甚至可以基于相似的属性进行过滤。
| 归档时间: |
|
| 查看次数: |
12166 次 |
| 最近记录: |