ฉันพยายามอัปโหลดไฟล์และโฟลเดอร์ไปยัง SharePoint Online โดยใช้ PowerShell ฉันเกือบจะเสร็จแล้ว แต่มีบางอย่างผิดปกติกับโค้ดในวิธีการเรียกซ้ำ มันอัปโหลดไฟล์ลงในโฟลเดอร์ด้านล่างที่ควรจะเป็น ตัวอย่างเช่น ถ้าฉันมีโครงสร้างโฟลเดอร์และแต่ละโฟลเดอร์มี 3 ไฟล์:
โฟลเดอร์1\โฟลเดอร์2\โฟลเดอร์3
ไฟล์ใน Folder2 และ Folder3 จะถูกอัพโหลดไปยังโฟลเดอร์ 3
ฉันรู้ว่าโฟลเดอร์กำลังได้รับการอัปโหลดก่อน ดังนั้นพาธของ $UploadSubFolder
จึงได้รับการอัปเดตไปที่โฟลเดอร์ด้านล่าง แต่ฉันไม่รู้ว่าจะแก้ไขได้อย่างไร! คุณช่วยเขียนโค้ดเล็กน้อยเพื่อให้ไฟล์ถูกอัพโหลดก่อนได้ไหม?
ใครสามารถช่วยฉันด้วยปัญหานี้? ขอบคุณ!
Function ImportFiles()
{
ForEach($File in Get-ChildItem $CurrentFolder)
{
If($File.PSIsContainer -eq $false)
{
#Upload File to Folder in Sharepoint
$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 = $UploadSubFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
ElseIf($File.PSIsContainer -eq $True)
{
#Upload SubFolder
$CurrentFolder = $CurrentFolder + "\" + $File.Name
$NewFolder1 = Split-Path $File -leaf
$UploadSubFolder = $UploadSubFolder.Folders.Add($NewFolder1)
$Context.Load($UploadSubFolder)
$Context.ExecuteQuery()
ImportFiles
}
Else
{
Write-Host "Upload Complete"
}
}
}
#Get name of Root folder
$NewFolder = Split-Path $Folder -Leaf
#upload Root folder to sharepoint
$UploadFolder = $List.RootFolder.Folders.Add($NewFolder)
$Context.Load($UploadFolder)
$Context.ExecuteQuery()
ForEach($File in (dir $Folder))
{
if($File.PSIsContainer -eq $false)
{
#Upload File to Folder in Sharepoint
$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 = $UploadFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
else
{
$CurrentFolder = $Folder + "\" + $File.Name
#upload folder
$NewFolder = Split-Path $File -leaf
$UploadSubFolder = $UploadFolder.Folders.Add($NewFolder)
$Context.Load($UploadSubFolder)
$Context.ExecuteQuery()
ImportFiles
}
}