[英]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.