ย้ายการแชร์ไฟล์เครือข่ายไปยัง SharePoint โดยใช้ PowerShell
ข้อกำหนด: ย้าย Network File Share ไปยัง SharePoint โดยใช้ PowerShell
สคริปต์ PowerShell เพื่อนำเข้าไฟล์เครือข่ายที่แชร์ไปยัง SharePoint
การย้ายไฟล์ที่แชร์บนเครือข่ายของคุณไปยัง SharePoint ไม่จำเป็นต้องยุ่งยาก ด้วย PowerShell คุณสามารถย้ายไฟล์ของคุณไปยังโฟลเดอร์ SharePoint ได้อย่างรวดเร็วและง่ายดาย โพสต์นี้จะแสดงวิธีโยกย้ายการแชร์ไฟล์เครือข่ายไปยัง SharePoint โดยใช้ PowerShell
ใช้สคริปต์ PowerShell นี้เพื่อนำเข้าไฟล์และโฟลเดอร์ทั้งหมดจากเส้นทางเครือข่ายไปยังไลบรารีเอกสาร SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to migrate all Files and Folders from FileShare to SharePoint
Function Migrate-FileShareToSharePoint()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $TargetLibraryName,
[Parameter(Mandatory=$true)] [string] $SourceFolderPath,
[Parameter(Mandatory=$false)] [bool] $Overwrite = $False
)
#Get the Target Folder to Upload
$Web = Get-SPWeb $SiteURL
$TargetLibrary = $Web.Lists[$TargetLibraryName]
$TargetFolder= $TargetLibrary.RootFolder
#Get All Files and Folders from the Source
Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
#Get Source Item's metadata
$CreatedDate= [DateTime]$_.CreationTime
$ModifiedDate = [DateTime]$_.LastWriteTime
If ($_.PSIsContainer -eq $True) #If its a Folder!
{
#Frame Target Folder URL
$TargetFolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
#$Subfolder = $Web.GetFolder($TargetFolder.URL + "/" + $_.Name)
$FolderToCreate = $Web.GetFolder($TargetFolderRelativeURL)
Write-host -f Yellow "Ensuring Folder '$TargetFolderRelativeURL'" -NoNewline
#Ensure Target Folder Exists
If ($FolderToCreate.Exists -eq $false)
{
#Create New Sub-Folder
$FolderToCreate=$Web.Folders.Add($TargetFolderRelativeURL)
$FolderToCreate.Item["Created"] = $CreatedDate
$FolderToCreate.Item["Modified"] = $ModifiedDate
$FolderToCreate.Item.Update()
Write-host -f Green "`tCreated Folder!"
}
Else
{
Write-host -f Green "`tFolder Already Exists!"
}
}
Else #If its a File
{
$FolderToUpload = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
$TargetFileURL = $FolderToUpload+"/"+$_.Name
$SourceFilePath = $_.FullName
Write-host -f Yellow "Ensuring File '$TargetFileURL'" -NoNewline
#Check if file exists
$FileToUpload = $Web.GetFile($TargetFileURL)
If($FileToUpload.Exists -and $Overwrite -eq $False)
{
Write-host -f Green "`tFile Already Exists!"
}
Else
{
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
#Upload the File to SharePoint Library's Folder
$ParentFolder = $Web.GetFolder($FolderToUpload)
$FileToUpload = $ParentFolder.Files.Add($_.Name, $FileStream, $TRUE)
#Set Metadata
$FileToUpload.Item["Created"]=$CreatedDate
$FileToUpload.Item["Modified"]=$ModifiedDate
$FileToUpload.Item.Update()
#Close file stream
$FileStream.Close()
Write-host "`tFile Uploaded Successfully!" -ForegroundColor Green
}
}
#Read-Host
}
}
#Set parameter values
$SiteURL="https://intranet.crescent.com/sites/marketing/us"
$TargetLibraryName="Documents"
$SourceFolderPath= "\\Cre-LT575\Salaudeen\Project Documents" #Or any local path: "C:\Temp\Docs"
#Call the function to Upload All files & folders from network Fileshare to SharePoint library
Migrate-FileShareToSharePoint -SiteURL $SiteURL -SourceFolderPath $SourceFolderPath -TargetLibraryName $TargetLibraryName
โดยสรุป การย้ายการแชร์ไฟล์บนเครือข่ายไปยัง SharePoint โดยใช้ PowerShell เป็นวิธีที่มีประสิทธิภาพและมีประสิทธิภาพในการย้ายข้อมูลจำนวนมากจากแพลตฟอร์มหนึ่งไปยังอีกแพลตฟอร์มหนึ่ง โดยใช้ PowerShell คุณสามารถย้ายข้อมูล รวมถึงไฟล์และโฟลเดอร์จากการใช้ไฟล์เครือข่ายไปยัง SharePoint ได้โดยอัตโนมัติ วิธีนี้สามารถประหยัดเวลาและทรัพยากรในขณะเดียวกันก็รับประกันว่าข้อมูลจะได้รับการถ่ายโอนและจัดระเบียบอย่างเหมาะสมใน SharePoint
หากต้องการย้ายการแชร์ไฟล์เครือข่ายไปยัง SharePoint Online ให้ใช้: วิธีโยกย้ายการแชร์ไฟล์ไปยัง SharePoint Online โดยใช้ PowerShell