[英]Upload multiple files from multiple folders to SharePoint online using Powershell
对于SharePoint Online和Powershell来说都是新手,并认为这将是一个非常简单的任务,但我正在寻求帮助。
我有一个客户端,该客户端的照片存储在文件共享的多个文件夹中,他们希望将其移动到SharePoint。 他们希望使用文件退出所在的文件夹名称作为元数据,以使搜索更加容易。
这是我正在使用的脚本,运气不佳。
$connection = Connect-PnPOnline https://somecompany.sharepoint.com -Credentials $me -ReturnConnection
$files = Get-ChildItem "F:\some data" -Recurse
foreach ($file in $files)
{Add-PnPFile -Path $file.FullName -Folder Photos -Values @{"Title" = $file.Name;} -Connection $connection}
我遇到的问题是,这不会递归文件夹,并返回“找不到本地文件”
如果可以正常工作,则可以将当前文件夹名称作为变量添加到元数据中。
我很确定这对专家来说将是一个简单的任务,但是可惜我不是。任何帮助将不胜感激。
谢谢贾森
这似乎对我有用,因此将回答此问题。 如果有更简单的方法或更清洁的方法,并且有人知道如何更深入一点,则很乐意发表评论。
$connection = Connect-PnPOnline https://somecompany.sharepoint.com -Credentials $me -ReturnConnection
$LocalFolders = get-childitem -path "c:\test" | where-object {$_.Psiscontainer} | select-object FullName
foreach ($folder in $localfolders) {
$files = get-childitem -Path $folder.FullName -Recurse
foreach ($file in $files) {
$value1 = $file.Directory.Name
Add-PnPFile -Path $file.FullName -Folder Photos -Values @{"Title" = $file.Name;"SubCat" = $value1;} -Connection $connection
}
}
您可以尝试以下脚本,需要安装pnp powershell 。
function UploadDocuments(){
Param(
[ValidateScript({If(Test-Path $_){$true}else{Throw "Invalid path given: $_"}})]
$LocalFolderLocation,
[String]
$siteUrl,
[String]
$documentLibraryName
)
Process{
$path = $LocalFolderLocation.TrimEnd('\')
Write-Host "Provided Site :"$siteUrl -ForegroundColor Green
Write-Host "Provided Path :"$path -ForegroundColor Green
Write-Host "Provided Document Library name :"$documentLibraryName -ForegroundColor Green
try{
$credentials = Get-Credential
Connect-PnPOnline -Url $siteUrl -CreateDrive -Credentials $credentials
$file = Get-ChildItem -Path $LocalFolderLocation -Recurse
$i = 0;
Write-Host "Uploading documents to Site.." -ForegroundColor Cyan
(dir $path -Recurse) | %{
try{
$i++
if($_.GetType().Name -eq "FileInfo"){
$SPFolderName = $documentLibraryName + $_.DirectoryName.Substring($path.Length);
$status = "Uploading Files :'" + $_.Name + "' to Location :" + $SPFolderName
Write-Progress -activity "Uploading Documents.." -status $status -PercentComplete (($i / $file.length) * 100)
$te = Add-PnPFile -Path $_.FullName -Folder $SPFolderName
}
}
catch{
}
}
}
catch{
Write-Host $_.Exception.Message -ForegroundColor Red
}
}
}
UploadDocuments -LocalFolderLocation C:\Lee\Share -siteUrl https://tenant.sharepoint.com/sites/Developer -documentLibraryName MyDOc4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.