簡體   English   中英

Powershell - Sharepoint 自動文件上傳返回“遠程服務器返回錯誤:(403) 禁止。” 錯誤

[英]Powershell - Sharepoint Auto file upload returning "The remote server returned an error: (403) Forbidden." error

我正在嘗試將文件從 PC 的 C 驅動器上傳到共享點文件夾。 但我收到此錯誤:

使用“3”個參數調用“UploadFile”時出現異常:“遠程服務器返回錯誤:(403) Forbidden。” 在 C:\\Users\\Projects\\file_upload.ps1:18 char:1 + $webclient.UploadFile($destination +'/'+ $File.Name,'PUT', $File.Full ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException +fullyQualifiedErrorId : WebException

代碼是:

 Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'



# Set the variables
$destination ='https://link to sharepoint site/foldername/'
$File =get-childitem 'C:\Users\path of the file in C drive'

# Since we’re doing this remotely, we need to authenticate
$securePasssword = ConvertTo-SecureString 'Password' -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ('Username', $securePasssword)

# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination +'/'+ $File.Name,'PUT', $File.FullName)

如果您能幫助我更正此代碼或建議任何其他可行的代碼,那將是一個很大的幫助。

嘗試使用以下腳本:

$User = "user@Tenant.onmicrosoft.com"  
$Password = '*******'  
$SiteURL = "https://Tenant.sharepoint.com"  
$Folder = "C:\Scripts\HpeQuota"  
#Path where you want to Copy  
$DocLibName = "Documents"  
#Docs library  
# Add references to SharePoint client assemblies and authenticate to Office 365 site - required  for CSOM  
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
#Bind to site collection  
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo-SecureString $Password -AsPlainText -Force))  
$Context.Credentials = $Creds  
#Retrieve list  
$List = $Context.Web.Lists.GetByTitle($DocLibName)  
$Context.Load($List)  
$Context.ExecuteQuery()  
# Upload file  
Foreach($File in (dir $Folder  -File))  
{  
    $FileStream = New-Object IO.FileStream($File.FullName, [System.IO.FileMode]::Open)  
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation  
    $FileCreationInfo.Overwrite = $true  
    $FileCreationInfo.ContentStream = $FileStream  
    $FileCreationInfo.URL = $File  
  $Upload = $List.RootFolder.Folders.GetByUrl("/Shared Documents/Cu folder").Files.Add($FileCreationInfo)    
    $Context.Load($Upload)  
    $Context.ExecuteQuery()  
}  

在此處輸入圖片說明 在此處輸入圖片說明 參考:

SharePoint Online 自動化 - O365 - 使用 PowerShell 遠程上傳文件到 SPO 文檔庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM