Powershell - автоматическая загрузка файла Sharepoint возвращает «Удаленный сервер возвратил ошибку: (403) Запрещено». ошибка - PullRequest
2 голосов
/ 09 января 2020

Я пытаюсь загрузить файл в папку sharepoint с диска C моего P C. Но я получаю эту ошибку:

Исключение, вызывающее «UploadFile» с аргументом «3»: «Удаленный сервер возвратил ошибку: (403) Запрещено». В 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)

Если бы вы могли помочь мне исправить этот код или предложить любой другой код, который будет работать, это было бы очень полезно.

1 Ответ

2 голосов
/ 09 января 2020

Попробуйте использовать следующий сценарий:

$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()  
}  

enter image description here enter image description here Ссылка:

SharePoint Online Автоматизация - O365 - удаленная загрузка файлов с помощью PowerShell в библиотеку документов SPO

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...