简体   繁体   English

Powershell - Sharepoint 自动文件上传返回“远程服务器返回错误:(403) 禁止。” 错误

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

I am trying to upload a file to a sharepoint folder from C drive of my PC.我正在尝试将文件从 PC 的 C 驱动器上传到共享点文件夹。 But I am getting this error:但我收到此错误:

Exception calling "UploadFile" with "3" argument(s): "The remote server returned an error: (403) Forbidden."使用“3”个参数调用“UploadFile”时出现异常:“远程服务器返回错误:(403) Forbidden。” At C:\\Users\\Projects\\file_upload.ps1:18 char:1 + $webclient.UploadFile($destination +'/'+ $File.Name,'PUT', $File.Full ...在 C:\\Users\\Projects\\file_upload.ps1:18 char:1 + $webclient.UploadFile($destination +'/'+ $File.Name,'PUT', $File.Full ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException + CategoryInfo : NotSpecified: (:) [], MethodInvocationException +fullyQualifiedErrorId : WebException

The code is :代码是:

 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)

If you could help me correct this code or suggest any other code which will work it would be a great help.如果您能帮助我更正此代码或建议任何其他可行的代码,那将是一个很大的帮助。

Try to use the script below:尝试使用以下脚本:

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

在此处输入图片说明 在此处输入图片说明 Reference:参考:

SharePoint Online Automation - O365 - Upload Your Files Remotely Using PowerShell To SPO Document Library SharePoint Online 自动化 - O365 - 使用 PowerShell 远程上传文件到 SPO 文档库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有应用程序权限的 PnP Powershell Sharepoint:远程服务器返回错误:(403)禁止 - PnP Powershell Sharepoint with app permissions: The remote server returned an error: (403) Forbidden Invoke-RestMethod:远程服务器返回错误:(403) Forbidden PowerShell - Invoke-RestMethod : The remote server returned an error: (403) Forbidden PowerShell 远程服务器返回错误:(403) Forbidden。 在 C:\\Program Files\\WindowsPowerShell\\Modules\\CosmosDB\\3.1.0.293\\CosmosDB.psm1 - The remote server returned an error: (403) Forbidden. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.1.0.293\CosmosDB.psm1 Connect-PnPOnline:远程服务器返回错误:(403)禁止 - Connect-PnPOnline : The remote server returned an error: (403) Forbidden Powershell:使用 REST API 从 Sharepoint 列表中删除项目 - 远程服务器返回错误:(400) 错误请求 - Powershell: Using REST API to Delete Items from a Sharepoint List - The remote server returned an error: (400) Bad Request PowerShell 脚本返回“远程服务器返回错误:(401) 未经授权” - PowerShell script return "The remote server returned an error: (401) Unauthorized" Windows Azure Powershell部署错误 - “远程服务器返回了意外响应” - Windows Azure Powershell Deployment Error - “The remote server returned an unexpected response” 远程服务器返回错误:(401) Unauthorized - PowerShell - Microsoft Graph API - The remote server returned an error : (401) Unauthorized - PowerShell - Microsoft Graph API 使用PowerShell 403错误刷新SharePoint Online中的Excel数据集 - Refresh Excel dataset in SharePoint Online with PowerShell 403 error powershell 返回错误:(403) WebException using New-pnpList - powershell returned an error:(403) WebException using New-pnpList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM