如何通过Powershell在Sharepoint联机目录中列出文件

时间:2019-05-30 01:00:43

标签: powershell sharepoint

您好,我正在尝试通过PowerShell将文件上传到我的Sharepoint网站(根目录)。 我可以连接到网址,但是在尝试上传文件时遇到问题 我已经安装了Install-Module SharePointPnPPowerShellOnline

当我运行命令时出现此错误

Add-PnPFile:远程服务器返回错误:(403)禁止。 在第1行:char:1 + Add-PnPFile-路径D:\ delme \ test.xlsx-文件夹/ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo:WriteError :( :) [Add-PnPFile],WebException     + FullyQualifiedErrorId:EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFile

Connect-PnPOnline –Url https://ABC123.sharepoint.com/sites/XXX/teamsites/os/Directory%20and%20Operating%20Systems/Forms/AllItems.aspx?viewpath=%2Fsites%2Fisd%2Fteamsites%2Fos%2FDirectory%20and%20Operating%20Systems%2FForms%2FAllItems.aspx

Add-PnPFile -D:\ delme \ test.xlsx-文件夹/

我知道我已经接近了。

2 个答案:

答案 0 :(得分:0)

使用下面的PowerShell。

$siteurl="https://ABC123.sharepoint.com/sites/XXX/teamsites/os" 
Connect-PnPOnline -Url $siteurl
Add-PnPFile -Path D:\delme\test.xlsx -Folder "Directory and Operating Systems"

答案 1 :(得分:0)

嗨,您可以使用此代码上传文件并检查文件是否成功更新

代码 -

Import-Module SharePointPnPPowerShellOnline 

#Get Connection to the url via Connect-PnPOnline $URL

Connect-PnPOnline "Some SharePoint Url" -UseWebLogin

#store all the files in the folder(get files using get-ChildItem)
#for single file you can use something like : $file = LocalPath\fileName.txt
$Files = Get-ChildItem "Local Folder Path which contains the files"

#Looping through each item 
foreach($File in $Files){
    
#storing the add-pnpFile in a variable so that i will be able to know the status, if my file have been successfully uploaded or not.
#"Shared Documents\If inside shared document you want to give some folder it will come here" else only "Shared Document"
    
$upload = Add-PnPFile -Folder "Shared Documents\FolderToUpload" -Path $File.FullName
    
#now checking, if file successfully uploaded or not and printing the required message. (You can use Write-Host $message if you want to print the message)
    
    if($upload.UniqueId){
      $message = "Successfully Uploaded" 
    } 
    else {
        $message = "ERROR - Unable to Upload"
    }
 }