[玩转系统] 如何使用 PowerShell 将文件共享迁移到 SharePoint Online?
作者:精品下载站 日期:2024-12-14 14:50:58 浏览:13 分类:玩电脑
如何使用 PowerShell 将文件共享迁移到 SharePoint Online?
要求:将所有文件和文件夹从网络文件共享迁移到 SharePoint Online。
PowerShell 将网络文件共享中的所有文件批量上传到 SharePoint Online
将文件共享迁移到 SharePoint Online 是利用 SharePoint 带来的许多功能和优势的好方法。但是,迁移文件可能是一个复杂的过程,尤其是当您有大量数据需要移动时。幸运的是,有一些工具和脚本可以使这个过程变得更加容易。本博文将向您展示如何使用 PowerShell 将文件共享迁移到 SharePoint Online。
让我们使用 PowerShell 将数据从文件服务器迁移到 SharePoint Online。我们还可以使用此 PowerShell 脚本将本地文件复制或移动到 SharePoint Online。这是我的来源:
让我们使用 PowerShell 将包含元数据的文件共享复制到 SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Function to migrate all Files and Folders from FileShare to SharePoint Online
Function Migrate-FileShareToSPO()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $TargetLibraryName,
[Parameter(Mandatory=$true)] [string] $SourceFolderPath
)
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Target Folder to Upload
$Web = $Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($TargetLibraryName)
$Ctx.Load($List)
$TargetFolder = $List.RootFolder
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
#Get All Files and Folders from the Source
Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
If ($_.PSIsContainer -eq $True) #If its a Folder!
{
$TargetFolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
Write-host -f Yellow "Ensuring Folder '$TargetFolderRelativeURL' Exists..."
#Check Folder Exists
Try {
#Get Source Folder's metadata
$CreatedDate= [DateTime]$_.CreationTime
$ModifiedDate = [DateTime]$_.LastWriteTime
$Folder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
#Write-host -f Green "Folder Already Exists!"
}
Catch {
#Create New Sub-Folder
$Folder=$Web.Folders.Add($TargetFolderRelativeURL)
$Ctx.ExecuteQuery()
Write-host -f Green "Created Folder at "$TargetFolderRelativeURL
#Set Metadata of the Folder
$FolderProperties = $Folder.ListItemAllFields
$FolderProperties["Created"] = $CreatedDate
$FolderProperties["Modified"] = $ModifiedDate
$FolderProperties.Update()
$Ctx.ExecuteQuery()
}
}
Else #If its a File
{
$TargetFileURL = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/") +"/"+$_.Name
$SourceFilePath = $_.FullName
Write-host -f Yellow "Uploading File '$_' to URL "$TargetFileURL
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
#Upload the File to SharePoint Library's Folder
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $TargetFileURL
$FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)
$Ctx.ExecuteQuery()
#Close file stream
$FileStream.Close()
#Update Metadata of the File
$FileProperties = $FileUploaded.ListItemAllFields
$FileProperties["Created"] = [datetime]$_.CreationTime
$FileProperties["Modified"] =[datetime]$_.LastWriteTime
$FileProperties.Update()
$Ctx.ExecuteQuery()
Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
}
}
}
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$TargetLibraryName="Documents"
$SourceFolderPath="\Cre-LT575\Salaudeen\Project Documents"
#Call the function to Upload All files & folders from network Fileshare to SharePoint Online
Migrate-FileShareToSPO -SiteURL $SiteURL -SourceFolderPath $SourceFolderPath -TargetLibraryName $TargetLibraryName
此 PowerShell 脚本将给定网络文件共享中的所有文件和文件夹上传到现有 SharePoint Online 文档库。
您可以使用此 PowerShell 脚本将文件和文件夹从本地驱动器批量上传到 SharePoint Online 或 OneDrive!只需使用相关参数调用函数“Migrate-FileShareToSPO”即可使用 PnP PowerShell 将文件共享导入到 SharePoint Online 文档库
这次,我们使用 PnP PowerShell 将文件从网络文件共享上传到 SharePoint Online 库。
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/london/"
$SourceFolderPath = "\FileServer\Upload"
$ListName = "Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Target Folder to Upload
$Web = Get-PnPWeb
$List = Get-PnPList $ListName -Includes RootFolder
$TargetFolder = $List.RootFolder
#Get All Files from the Source Folder
$Files = Get-ChildItem -Path $SourceFolderPath -Recurse | Where {!$_.PSIsContainer }
#Iterate through Each file and Upload to SharePoint Online Library
$Counter = 0
$Files | ForEach-Object {
#Calculate Target Folder URL for the file
$TargetFolderURL = $TargetFolder.ServerRelativeURL.Replace($Web.ServerRelativeUrl,[string]::Empty) + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
#Remove the Leading '/' characters
If($TargetFolderURL.StartsWith("/")) { $TargetFolderURL = $TargetFolderURL.Remove(0,1)}
#Display Progress bar
$Status = "Uploading File '" + $_.Name + "' to " + $TargetFolderURL +" ($($Counter) of $($Files.Count))"
Write-Progress -Activity "Uploading Files..." -Status $Status -PercentComplete (($Counter / $Files.Count) * 100)
#Check if File exists Already
$FileURL = $TargetFolderURL+"/"+$_.Name
$FileExists = Get-PnPFile -Url $FileURL -ErrorAction SilentlyContinue
If(!$FileExists)
{
#Upload File to Folder
$File = Add-PnPFile -Path $_.FullName -Folder $TargetFolderURL
Write-host "Uploaded File $($_.FullName) to Folder $TargetFolderURL"
}
$Counter++
}
比较源和目标并同步源中的新文件
上面的脚本工作正常,但是当我们有大量文件时,它需要花费大量时间,因为它必须将源中的每个文件与目标进行比较。所以,让我们稍微改进一下。
#Function to Ensure Files from Fileshare to SharePoint Online
Function Ensure-FileShareInSPO
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceFolderPath,
[Parameter(Mandatory=$true)] [string] $LibraryName,
[Parameter(Mandatory=$true)] [string] $LogFile,
[Parameter(Mandatory=$false)] [Int] $SyncInterval = -30 #Number of Days to check
)
Try {
Add-content $Logfile -value "`n---------------------- Import Files Script Started: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-------------------"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -ClientId "3c3ca329-f1b9-41xa-316-c3281aab82" -ClientSecret "vKLew0aJabhs/C3mmhSci61/ej2WE6G1eWasq3u4="
#Get Number of Source Items in the source
$SourceItemsCount = (Get-ChildItem -Path $SourceFolderPath -Recurse | Where {$_.LastWriteTime -gt (Get-Date).AddDays($SyncInterval)}).count
#Get the Target Library to Upload
$Web = Get-PnPWeb
$Library = Get-PnPList $LibraryName -Includes RootFolder
$TargetFolder = $Library.RootFolder
$TargetFolderSiteRelativeURL = $TargetFolder.ServerRelativeURL.Replace($Web.ServerRelativeUrl,[string]::Empty)
#Get All Items from the Source
$SourceItems = Get-ChildItem -Path $SourceFolderPath -Recurse | Where {$_.LastWriteTime -gt (Get-Date).AddDays($SyncInterval)}
$Source = @($SourceItems | Select @{Label='TargetItemURL';Expression={$_.FullName.Replace($SourceFolderPath,$TargetFolderSiteRelativeURL).Replace("\","/")}}, @{Label='Timestamp';Expression={$_.LastWriteTime.ToString("MM/dd/yyyy hh:mm:ss tt")}}, FullName, PSIsContainer)
#Get All Files from the target document library - In batches of 2000
$TargetFiles = Get-PnPListItem -List $LibraryName -PageSize 2000
$Target = @($TargetFiles | Select @{Label='TargetItemURL';Expression={$_.FieldValues.FileRef.Replace($Web.ServerRelativeUrl,[string]::Empty)}},
@{Label='FullName';Expression={$_.FieldValues.FileRef.Replace($TargetFolder.ServerRelativeURL,$SourceFolderPath).Replace("/","\")}},
@{Label='PSIsContainer';Expression={$_.FileSystemObjectType -eq "Folder"}}, @{Label='Timestamp';Expression={$_.FieldValues.Modified.ToString("MM/dd/yyyy hh:mm:ss tt")}})
#Compare Source and Target and upload files which are not in the target
$Counter = 1
$FilesDifferent = Compare-Object -ReferenceObject $Source -DifferenceObject $Target -Property FullName, TargetItemURL, PSIsContainer, Timestamp
$FilesDiff = @($FilesDifferent | Where {$_.SideIndicator -eq "<="})
$FilesDiffCount = $FilesDiff.Count
#Check if Source Folder Items count and Target Folder Items count are different
If($FilesDiffCount -gt 0)
{
Write-host "Found $FilesDiffCount new differences in the Source!"
Add-content $Logfile -value "Found $FilesDiffCount new differences in the Source!"
$FilesDiff | ForEach-Object {
#Calculate Target Folder URL for the file
$TargetFolderURL = (Split-Path $_.TargetItemURL -Parent).Replace("\","/")
#Remove the Leading '/' characters
If($TargetFolderURL.StartsWith("/")) { $TargetFolderURL = $TargetFolderURL.Remove(0,1)}
$ItemName = Split-Path $_.FullName -leaf
#Replace Invalid Characters
$ItemName = [RegEx]::Replace($ItemName, "[{0}]" -f ([RegEx]::Escape([String]'\"*:<>?/\|')), '_')
#Display Progress bar
$Status = "Importing '" + $ItemName + "' to " + $TargetFolderURL +" ($($Counter) of $($FilesDiffCount))"
Write-Progress -Activity "Uploading ..." -Status $Status -PercentComplete (($Counter / $FilesDiffCount) * 100)
If($_.PSIsContainer)
{
#Ensure Folder
$Folder = Resolve-PnPFolder -SiteRelativePath ($TargetFolderURL+"/"+$ItemName)
Get-PnPProperty -ClientObject $Folder -Property ListItemAllFields | Out-Null
Set-PnPListItem -List $LibraryName -Identity $Folder.ListItemAllFields.Id -Values @{"Modified"=$_.Timestamp;} | Out-Null
Write-host "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL"
Add-content $Logfile -value "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL"
}
Else
{
#Upload File
$File = Add-PnPFile -Path $_.FullName -Folder $TargetFolderURL -Values @{"Modified"=$_.Timestamp;}
Write-host "Ensured File '$($_.FullName)' to Folder $TargetFolderURL"
Add-content $Logfile -value "Ensured File '$($_.FullName)' to Folder $TargetFolderURL"
}
$Counter++
}
}
Else
{
Write-host "Found no new Items in the Source! Total Items in Source: $SourceItemsCount , Number Items to Sync: $FilesDiffCount, Target: $($Library.Itemcount)"
Add-content $Logfile -value "Found no new Items in the Source! Items in Source: $SourceItemsCount , Number Items to Sync: $FilesDiffCount, Target: $($Library.Itemcount)"
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
Add-content $Logfile -value "Error:$($_.Exception.Message)"
}
Finally {
Add-content $Logfile -value "---------------------- Import Files Script Completed: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-----------------"
}
}
#Call the function
Import-FileShareToSPO -SiteURL "https://crescent.sharepoint.com/sites/Funds" -SourceFolderPath "\fileserver\Reports" `
-ListName "Reports" -LogFile "C:\FileShareImportScript\Reports-LOG.log"
使用 PowerShell 将文件共享迁移到 SharePoint Online
如果您想要将文件从文件共享增量迁移到 SharePoint Online,该怎么办?此脚本将所有文件从网络文件共享导入到 SharePoint Online 文档库。
#Function to Import FileShare to SharePoint Online Document Library
Function Import-FileSharetoSPO()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceFolderPath,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $LogFile,
[Parameter(Mandatory=$true)] [string] $ConfigFilePath
)
Try {
Add-content $Logfile -value "`n---------------------- File Import Script Started: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-------------------"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Initialize Config File
Try {
$ConfigFile = [XML] (Get-Content $ConfigFilePath -ErrorAction Stop)
}
Catch [System.Management.Automation.ItemNotFoundException] {
$Config = "<?xml version='1.0' encoding='UTF-8'?><Config><Timestamp LastRun='01/01/2000 12:00:00 AM'></Timestamp></Config>"
New-Item -ItemType "File" -Path $ConfigFilePath | Out-Null
Set-Content -Path $ConfigFilePath -Value $Config
}
#Get the Last Run time of the script from Config file
$ConfigFile = [XML] (Get-Content $ConfigFilePath)
$ScriptLastRunTime = [DateTime]::ParseExact($ConfigFile.Config.Timestamp.GetAttribute("LastRun"), "dd/MM/yyyy hh:mm:ss tt", $null)
#Update LastRun Timestamp in config file
$ConfigFile.Config.Timestamp.SetAttribute("LastRun", (Get-date -format "dd/MM/yyy hh:mm:ss tt"))
$ConfigFile.Save($ConfigFilePath)
#Get the Most Recent Item Created Timestamp
$LastItemTimestamp = Get-ChildItem -Path $SourceFolderPath -Recurse | Measure-Object -Maximum -Property CreationTime | Select -ExpandProperty Maximum
#Check if there are new items since last script executed
If($LastItemTimestamp -ge $ScriptLastRunTime)
{
Write-host -f green "Found Changes to the Source since the last execution of script..."
Add-content $Logfile -value "Found Changes to the Source since the last execution of script!"
#Get the Target Folder to Upload
$Web = Get-PnPWeb
$List = Get-PnPList $ListName -Includes RootFolder
$TargetFolder = $List.RootFolder
$TargetFolderSiteRelativeURL = $TargetFolder.ServerRelativeURL.Replace($Web.ServerRelativeUrl,[string]::Empty)
#Get All New Items from the Source
$SourceItems = Get-ChildItem -Path $SourceFolderPath -Recurse | Where-Object {$_.CreationTime -gt $ScriptLastRunTime}
$Source = $SourceItems | Select FullName, PSIsContainer, @{Label='TargetItemURL';Expression={$_.FullName.Replace($SourceFolderPath,$TargetFolderSiteRelativeURL).Replace("\","/")}}
Add-content $Logfile -value "Number of New Items Found in the Source: $($SourceItems.Count)"
#Upload Source Items from Fileshare to Target SharePoint Online document library
$Counter = 1
$Source | ForEach-Object {
#Calculate Target Folder URL
$TargetFolderURL = (Split-Path $_.TargetItemURL -Parent).Replace("\","/")
#Remove the Leading '/' characters
If($TargetFolderURL.StartsWith("/")) { $TargetFolderURL = $TargetFolderURL.Remove(0,1)}
$ItemName = Split-Path $_.FullName -leaf
#Replace Invalid Characters
$ItemName = [RegEx]::Replace($ItemName, "[{0}]" -f ([RegEx]::Escape([String]'\*:<>?/\|')), '_')
#Display Progress bar
$Status = "Importing '" + $ItemName + "' to " + $TargetFolderURL +" ($($Counter) of $($SourceItems.Count))"
Write-Progress -Activity "Uploading ..." -Status $Status -PercentComplete (($Counter / $SourceItems.Count) * 100)
If($_.PSIsContainer)
{
#Ensure Folder
$Folder = Resolve-PnPFolder -SiteRelativePath ($TargetFolderURL+"/"+$ItemName)
Write-host "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL"
Add-content $Logfile -value "Ensured Folder '$($ItemName)' to Folder $TargetFolderURL"
}
Else
{
#Upload File
$File = Add-PnPFile -Path $_.FullName -Folder $TargetFolderURL
Write-host "Uploaded File '$($_.FullName)' to Folder $TargetFolderURL"
Add-content $Logfile -value "Uploaded File '$($_.FullName)' to Folder $TargetFolderURL"
}
$Counter++
}
}
Else
{
Write-host -f Yellow "No Changes detected in the Source!"
Add-content $Logfile -value "No Changes detected in the Source!"
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
Add-content $Logfile -value "Error:$($_.Exception.Message)"
}
Finally {
Add-content $Logfile -value "---------------------- File Import Script Completed: $(Get-date -format 'dd/MM/yyy hh:mm:ss tt')-----------------"
}
}
#Call the Function with different parameters
Import-FileSharetoSPO -SiteURL "https://crescent.sharepoint.com/sites/Funds" -SourceFolderPath "\fileserver\Notices" `
-ListName "Notices" -LogFile "E:\FileShareImportScript\Notices-LOG.log" -ConfigFilePath "E:\FileShareImportScript\Notices-CONFIG.config"
Import-FileSharetoSPO -SiteURL "https://crescent.sharepoint.com/sites/Funds" -SourceFolderPath "\fileserver\Reports" `
-ListName "Reports" -LogFile "E:\FileShareImportScript\Reports-LOG.log" -ConfigFilePath "E:\FileShareImportScript\Reports-CONFIG.config"
第一次,当您执行脚本时,它会创建一个配置文件并更新其中的“LastRun”时间戳。下次,它会获取上次运行脚本后创建的文件和文件夹并上传它们。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag