当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 将网络文件共享迁移到 SharePoint

作者:精品下载站 日期:2024-12-14 15:46:06 浏览:14 分类:玩电脑

使用 PowerShell 将网络文件共享迁移到 SharePoint


要求: 使用 PowerShell 将网络文件共享迁移到 SharePoint。

用于将网络文件共享导入 SharePoint 的 PowerShell 脚本

将网络文件共享迁移到 SharePoint 并不复杂。使用 PowerShell,您可以快速轻松地将文件迁移到 SharePoint 文件夹。本文将向您展示如何使用 PowerShell 将网络文件共享迁移到 SharePoint。

使用此 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

总之,使用 PowerShell 将网络文件共享迁移到 SharePoint 是将大量数据从一个平台移动到另一个平台的一种强大且高效的方法。通过使用 PowerShell,您可以自动将数据(包括文件和文件夹)从网络文件共享迁移到 SharePoint。这可以节省时间和资源,同时确保数据在 SharePoint 中正确传输和组织。

要将网络文件共享迁移到 SharePoint Online,请使用:如何使用 PowerShell 将文件共享迁移到 SharePoint Online?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯