SharePoint Online:使用 PowerShell 上传文件夹结构
要求: 使用 PowerShell 将文件夹结构上传到 SharePoint Online。
PowerShell 将文件夹结构上传到 SharePoint Online:
我必须将文件夹结构从本地驱动器克隆到 SharePoint Online。将文件夹结构上传到 SharePoint Online 可能是一个乏味的过程,尤其是当您有大量文件和文件夹需要上传时。
通过 Web 浏览器或文件资源管理器上传文件夹树也会上传这些文件夹中的所有文件。因此,以下是用于将文件夹结构从本地驱动器上传到 SharePoint Online 文档库而不复制任何文件的 PowerShell:
#Load SharePoint CSOM Assemblies
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"
#Function to Check if Folder Exists. If not, Create the Folder
Function Ensure-SPOFolder()
{
param
(
[Parameter(Mandatory=$true)] [string] $FolderRelativeURL
)
#Check Folder Exists
Try {
$Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
Write-host -f Yellow "`tFolder Already Exists! Skipped...!"
}
Catch {
#Create New Sub-Folder
$Folder=$Web.Folders.Add($FolderRelativeURL)
$Ctx.ExecuteQuery()
Write-host -f Green "`tCreated Folder at "$FolderRelativeURL
}
}
#Function to upload a Local Folder structure to SharePoint Online Documnet Library
Function Upload-SPOFolderStructure()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $SourceFolderPath
)
#Setup Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the Target Folder to Upload
$Web = $Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$TargetFolder = $List.RootFolder
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
#Get All Folders and Sub-Folders from the Local Source
Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
If($_.PSIsContainer -eq $True)
{
$FolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
If($FolderRelativeURL)
{
Write-host -f Yellow "Ensuring Folder '$FolderRelativeURL' Exists..."
#Call function to ensure folder
Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL
}
}
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
$SourceFolderPath="C:\Users\salaudeen\Desktop\Upload"
#Call the function to Upload All folders and sub-folders from local folder to SharePoint Online library
Upload-SPOFolderStructure -SiteURL $SiteURL -ListName $ListName -SourceFolderPath $SourceFolderPath
此脚本将所有文件夹和子文件夹从给定源复制到 SharePoint Online 文档库。
如果您需要将文件夹结构与文件一起上传,请使用:使用 PowerShell 将文件夹上传到 SharePoint Online
