Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何将文件从SharePoint团队网站导入到本地计算机?_Powershell_Sharepoint - Fatal编程技术网

Powershell 如何将文件从SharePoint团队网站导入到本地计算机?

Powershell 如何将文件从SharePoint团队网站导入到本地计算机?,powershell,sharepoint,Powershell,Sharepoint,我的客户每月生成报告(excel文件),并将文件上载到SharePoint文档库中。同样,一些报告是通过邮件(outlook)发送的 要求是我们需要将excel报告(在文档库中以及通过邮件发送的文件)复制到登录区域(FTP服务器),并且我们需要每月自动执行此过程 实现这一目标的最佳方式是什么?请帮我个忙。谢谢 $uri = "https://intranet.company.com/departments/SampleSite/_api/web/getfilebyserverrelativeur

我的客户每月生成报告(excel文件),并将文件上载到SharePoint文档库中。同样,一些报告是通过邮件(outlook)发送的

要求是我们需要将excel报告(在文档库中以及通过邮件发送的文件)复制到登录区域(FTP服务器),并且我们需要每月自动执行此过程

实现这一目标的最佳方式是什么?请帮我个忙。谢谢

$uri = "https://intranet.company.com/departments/SampleSite/_api/web/getfilebyserverrelativeurl('/departments/SampleSite/Shared Documents/test.docx')/`$Value"
$FileBinary = $(Invoke-WebRequest -Uri $uri -Credential $cred -ContentType 'application/json;odata=verbose').Content
[System.IO.File]::WriteAllBytes('C:\tempfolder\test.docx',$FileBinary)
使用此示例,您可以下载并保存文件(它使用SP restapi)。此简单示例使用当前登录用户的凭据

编辑:根据请求,我为SharePoint Online添加了一个示例(由于登录流,您需要SP SDK)。但代码没有优化,因为这是我的第一个示例之一。问题是您必须“硬编码”凭证。在我的公司中,我的解决方案是使用证书加密凭据:我使用运行脚本的服务帐户的公钥加密文件中的凭据;服务帐户使用私钥对其进行解密。不理想,但对我们来说足够安全

$cred = Get-Credential # Get credentials

## Load the Assemblies
add-type -AssemblyName system.net.http
Add-type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll"

# Put credentials in SPO credential object
$SPCred = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($cred.UserName,$cred.Password)
# Set the uri's
$uri = [uri]::new('https://YourTentant.sharepoint.com')
$fullUri = "https://YourTentant.sharepoint.com/sites/ApiTest/_api/web/getfilebyserverrelativeurl('/sites/ApiTest/Shared Documents/Document.docx')/`$Value"
# Create HTTPClientHandler object and set the properties
$handler = [System.Net.Http.HttpClientHandler]::new()
$handler.Credentials = $SPCred
$handler.CookieContainer.SetCookies($uri,$SPCred.GetAuthenticationCookie($uri))

# Create the client and set the options
$Client = [System.Net.Http.HttpClient]::new($handler)
$Client.DefaultRequestHeaders.Accept.Clear()
$Client.DefaultRequestHeaders.Accept.Add([System.Net.Http.Headers.MediaTypeWithQualityHeaderValue]::new('application/json'))
# Call the URL (async) and get the response
$Response = $Client.GetAsync($fullUri)
$data = $Response.Result

## now download the data as string (in the final version I'm going to call the ReadAsStream() method instead to get the binary file)
$a = $data.Content.ReadAsStringAsync() 
$a.Result

您可以将webclient服务与powershell一起使用,从Sharepoint文档库下载文档。谢谢,但是可以通过powershell commaands吗???是的。稍后将共享代码确定谢谢。等待您宝贵的代码…这在O365中不起作用(我必须为此编写一个新脚本,因为我无法使用委派凭据,我必须使用SharePoint SDK使登录流正常工作)。我将更新答案,以包括SPO的工作样本)