SharePoint Online:使用 PowerShell 在文档库之间复制文件夹结构
要求:在 SharePoint Online 中的文档库之间复制文件夹结构。
PowerShell 在 SharePoint Online 文档库之间复制文件夹结构:
是否曾经想要在两个 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 Copy All Folders from Source to Destination
Function Copy-SPOFolder
{
param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
$Ctx = $TargetFolder.Context
#Get all Folders from the Source
$SubFolders = $SourceFolder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
If($SubFolder.Name -ne "Forms")
{
#Prepare Target Folder
$TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl
Try {
$Folder=$Ctx.web.GetFolderByServerRelativeUrl($TargetFolderURL)
$Ctx.load($Folder)
$Ctx.ExecuteQuery()
Write-host "Folder Already Exists:"$TargetFolderURL -f Yellow
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$Folder=$Ctx.Web.Folders.Add($TargetFolderURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
Write-host "Folder Copied:"$TargetFolderURL -f Green
}
}
#Call the function recursively
Copy-SPOFolder -SourceFolder $SubFolder -TargetFolder $Folder
}
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
}
#Function to Copy Folder Structure between document libraries
Function Copy-SPOFolderStructure
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceLibraryName,
[Parameter(Mandatory=$true)] [string] $TargetLibraryName
)
Try {
#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 source library and Target Libraries
$SourceLibrary = $Ctx.Web.Lists.GetByTitle($SourceLibraryName)
$Ctx.Load($SourceLibrary)
$Ctx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $Ctx.Web.Lists.GetByTitle($TargetLibraryName)
$Ctx.Load($TargetLibrary)
$Ctx.Load($TargetLibrary.RootFolder)
$Ctx.ExecuteQuery()
#Call the function to copy folders from source library to the target
Copy-SPOFolder -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
}
Catch {
write-host -f Red "Error Copying File!" $_.Exception.Message
}
}
#Set Parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SourceLibraryName="Team Documents"
$TargetLibraryName="Project Documents"
#Call the function
Copy-SPOFolderStructure -SiteURL $SiteURL -SourceLibraryName $SourceLibraryName -TargetLibraryName $TargetLibraryName
此脚本将所有文件夹和子文件夹从源库复制到目标。如果要复制文件夹和文件,请使用:SharePoint Online:使用 PowerShell 在文档库之间复制文件
