我正在尝试从PC的C驱动器将文件上传到sharepoint文件夹。但我收到此错误:
“异常使用” 3“参数调用” UploadFile“:”远程服务器返回错误:(403)禁止。“ 在C:\ Users \ Projects \ file_upload.ps1:18 char:1 + $ webclient.UploadFile($ destination +'/'+ $ File.Name,'PUT',$ File.Full ...
+ ~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
+ CategoryInfo:未指定:(:) [],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 = "[email protected]"
$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 Automation-O365-使用PowerShell远程将文件上传到SPO文档库
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句