我正在尝试从SharePoint门户下载文件。
我能够成功登录并打开文件,但是无法通过脚本下载文件。您能否提一些建议。谢谢。
$username= user_name
$password = password
$url = "https://share.mycompany.com/"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true
$ie.navigate($url)
Start-Sleep -Milliseconds 10000
$ie.Document.getElementByID("Ecom_User_ID").value = $username
$ie.Document.getElementByID("Ecom_Password").value = $password
$ie.document.getElementById("loginBtn").click()
Start-Sleep -Milliseconds 10000
$source = "https://share.mycompany.com/sites/department/SomeExcelFile.xlsx"
$ie.navigate($source)
$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')
$obj.SendKeys('s')
$obj.SendKeys('{Enter}')
答案 0 :(得分:0)
假设您使用的是SharePoint 2013或更高版本,则建议您将SharePoint Pnp PowerShell Framework用于SharePoint中的许多其他操作。
在这种情况下,Get-pnpfile命令
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpfile?view=sharepoint-ps
示例:
Get-PnPFile -Url /sites/project/_catalogs/themes/15/company.spcolor -Path c:\temp -FileName company.spcolor -AsFile
答案 1 :(得分:0)
您可以尝试这个。
Function Download-File([string]$FileUrl,[string]$DownloadPath)
{
$fileName = [System.IO.Path]::GetFileName($FileUrl)
$downloadFilePath = [System.IO.Path]::Combine($DownloadPath,$fileName)
$client = New-Object System.Net.WebClient
$client.Credentials = new-object System.Net.NetworkCredential("lee", "password", "domain")
$client.DownloadFile($FileUrl, $downloadFilePath)
$client.Dispose()
}
Download-File -FileUrl http://sp:12001/MyDoc/test2.docx -DownloadPath "C:\Lee\PSDownLoad"
答案 2 :(得分:0)
在SharePoint 2010上,您可以使用Lee_MSFT引用的.Net方法,但是我发现通过Invoke-WebRequest从SharePoint下载文件更加容易,尤其是当您在具有访问该文件权限的帐户的上下文中运行时
Invoke-WebRequest -Uri 'https://some.site.org/sites/knowledge/documents/myfile.zip' `
-UseDefaultCredentials `
-OutFile c:\temp\myfile.zip
答案 3 :(得分:0)
我之前尝试过类似的代码,但出现401(未经授权)错误。因此,我走了这条路。请指教。谢谢
$source = "https://share.company.com/sites/Department/SomeExcelFile.xlsx"
$destination = "c:\temp\SomeExcelFile.xlsx"
$user = username
$pwd = password
$wc = new-object System.Net.WebClient
$wc.UseDefaultCredentials = $true
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($user,$pwd)
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$wc.DownloadFile($source, $destination)