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

[玩转系统] SharePoint Online:使用 PowerShell 上传文件夹

作者:精品下载站 日期:2024-12-14 14:50:44 浏览:17 分类:玩电脑

SharePoint Online:使用 PowerShell 上传文件夹


要求:使用 PowerShell 将文件夹上传到 SharePoint Online。

如何将文件夹上传到 SharePoint Online 文档库?

您可以使用 Web 浏览器 - 工具栏中的上传按钮或通过将文件夹从 Windows 资源管理器拖放到库中来将文件夹及其内容上传到 SharePoint。以下是将文件夹及其子文件夹和文件上传到 SharePoint Online 文档库的方法:

  1. 导航到您的 SharePoint Online 库 >> 单击工具栏中的“上传”,然后选择“文件夹”选项。您将看到一个浏览对话框,用于从计算机中选择一个文件夹。

    [玩转系统] SharePoint Online:使用 PowerShell 上传文件夹

  2. 选择一个文件夹以将整个文件夹复制到 SharePoint Online 库。

这会将文件夹及其内容从本地计算机上传到 SharePoint Online。请注意,上传文件夹选项在 IE 11 中不起作用。(它在 Edge、Google Chrome 和 Firefox 浏览器中起作用!)。您还可以通过拖放将包含文件和子文件夹的文件夹上传到 SharePoint Online 现代库!顺便说一句,用户必须拥有贡献或更多权限!

使用 PowerShell 将文件夹上传到 SharePoint Online(包括文件夹、子文件夹和文件):

我们还可以使用PowerShell将文件夹从本地磁盘复制到SharePoint Online。此脚本对于想要使用 PowerShell 将数据文件夹从个人计算机上传到 Sharepoint Online 的用户很有帮助。该过程可能需要一些时间,具体取决于文件的大小和互联网连接速度。让我们使用 PowerShell 将文件夹上传到 SharePoint Online。这是我的来源:

[玩转系统] SharePoint Online:使用 PowerShell 上传文件夹

先决条件:确保您已安装 SharePoint Online 客户端
您机器中的组件 SDK 可以使用客户端程序集。您可以从 https://www.microsoft.com/en-us/download/details.aspx?id=42038 下载它

对于那些希望在 Microsoft 平台上自动执行繁琐任务的人来说,PowerShell 是一个强大的工具。本文将介绍如何使用 PowerShell 命令将文件夹从本地计算机上传到 Sharepoint Online。虽然可以通过 SharePoint Web 界面执行此操作,但使用 PowerShell 进行自动化场景可能会更方便。以下是如何使用 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 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 Green "Folder Already Exists!"
    }
    Catch {
        #Create New Sub-Folder
        $Folder=$Web.Folders.Add($FolderRelativeURL)
        $Ctx.ExecuteQuery()
        Write-host -f Green "Created Folder at "$FolderRelativeURL
    }
}

#Function to Upload a File to a SharePoint Online
Function Upload-SPOFile()    
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SourceFilePath,
        [Parameter(Mandatory=$true)] [string] $TargetFileURL
    )
    
    #Get the file from disk
    $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
    #Get File Name from source file path - Frame FileNames
    $SourceFileName = Split-path $SourceFilePath -leaf
   
    #Add File to SharePoint Library
    $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()
    Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
}
 
#Main Function to upload a Local Folder to SharePoint Online Document Library Folder
Function Upload-SPOFolder()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SourceFolderPath,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder        
    )

    #Get All Files and Sub-Folders from 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..."
                Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL
            }  
        }
        Else
        {
            $FolderRelativeUrl = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
            $FileRelativeURL = $FolderRelativeUrl+"/"+$_.Name
            Write-host -f Yellow "Uploading File '$_' to URL "$FileRelativeURL
            Upload-SPOFile -SourceFilePath $_.FullName -TargetFileURL $FileRelativeURL
        }
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$LibraryName="Documents" #Document Library Name
$SourceFolderPath="C:\Users\salaudeen\Desktop\Marketing\Documents"
 
#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($LibraryName)
$Ctx.Load($List)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery() 
 
#Call the function to Upload All files & folders from local folder to SharePoint Online Doclibrary
Upload-SPOFolder -SourceFolderPath $SourceFolderPath -TargetFolder $List.RootFolder

此 PowerShell 将给定文件夹、子文件夹和文件上传到 SharePoint Online。

[玩转系统] SharePoint Online:使用 PowerShell 上传文件夹

使用 PnP PowerShell 将文件夹上传到 SharePoint Online

要将文件夹上传到 SharePoint Online 文档库,请使用下面的 PnP PowerShell 脚本!根据您的租户设置站点 URL 和其他参数。


#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$FolderLocalPath = "C:\Temp\Docs"
$TargetFolder = "Shared Documents/Docs"

#Connect with SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive 

#Get All files from the local disk
$Files = Get-ChildItem -Path $FolderLocalPath -File

#Ensure the target folder
Resolve-PnPFolder -SiteRelativePath $TargetFolder | Out-Null

#Upload All files from the local folder to SharePoint Online Folder
ForEach ($File in $Files)
{
    Add-PnPFile -Path "$($File.Directory)$($File.Name)" -Folder $TargetFolder -Values @{"Title" = $($File.Name)} | Out-Null
    Write-host "Uploaded File:"$File.FullName
}

上述 PowerShell 脚本将所有文件从给定本地文件夹上传到 SharePoint Online。如果本地文件夹有子文件夹和文件怎么办?如何将文件和文件夹上传到 SharePoint Online?好吧,这是上传文件夹及其子文件夹和文件的 PnP PowerShell 脚本:


#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$LocalFolderPath = "C:\Temp\Docs"
$TargetFolderURL = "Shared Documents" #Site Relative URL

#Connect with SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive 

#Function to upload all files from a local folder to SharePoint Online Folder
Function Upload-PnPFolder($LocalFolderPath, $TargetFolderURL)
{
    Write-host "Processing Folder:"$LocalFolderPath -f Yellow
    #Get All files and SubFolders from the local disk
    $Files = Get-ChildItem -Path $LocalFolderPath -File

    #Ensure the target folder
    Resolve-PnPFolder -SiteRelativePath $TargetFolderURL | Out-Null

    #Upload All files from the local folder to SharePoint Online Folder
    ForEach ($File in $Files)
    {
        Add-PnPFile -Path "$($File.Directory)$($File.Name)" -Folder $TargetFolderURL -Values @{"Title" = $($File.Name)} | Out-Null
        Write-host "`tUploaded File:"$File.FullName -f Green
    }
}
#Call the function to upload the Root Folder
Upload-PnPFolder -LocalFolderPath $LocalFolderPath -TargetFolderURL $TargetFolderURL

#Get all Folders from given source path 
Get-ChildItem -Path $LocalFolderPath -Recurse -Directory | ForEach-Object {
    $FolderToUpload = ($TargetFolderURL+$_.FullName.Replace($LocalFolderPath,[string]::Empty)).Replace("\","/")
    Upload-PnPFolder -LocalFolderPath $_.FullName -TargetFolderURL $FolderToUpload
}

该脚本也适用于 OneDrive!只需将变量站点 URL 设置为 OneDrive 站点,并将目标文件夹 URL 设置为“文档”。

如果您只想将文件夹结构(不带文件)上传到 SharePoint 文档库,请使用:将文件夹结构上传到 SharePoint Online

经常问的问题:

如何使用 PowerShell 将文件上传到 SharePoint Online?

要将文件上传到 SharePoint Online,请导航到 SharePoint Online 文档库 >> 单击工具栏上的“上传”按钮 >> 选择“文件”菜单项。浏览并选择要上传的文件。您还可以使用 CSOM 脚本或 PnP PowerShell cmdlet Add-PnPFile 将文件上传到 SharePoint Online。
详细信息:将文件上传到 SharePoint Online 的 PowerShell 脚本

如何将多个文件上传到 SharePoint 在线图书馆?

您只需将它们拖放到 SharePoint Online 文档库即可开始上传。文件资源管理器也有帮助。要将文件批量上传到 SharePoint Online,您也可以使用此 PowerShell 脚本:使用 PowerShell 将文件批量上传到 SharePoint Online

如何将大文件上传到SharePoint Online?

虽然SharePoint Online的最大文件上传大小为250 GB,但可能会因网络带宽、超时问题而失败!要解决此问题,请使用 OneDrive 同步客户端或 PowerShell 脚本来拆分大文件并分块上传。如果要使用 PowerShell 上传大文件,请使用如何使用 PowerShell 将较大文件上传到 SharePoint Online?

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

取消回复欢迎 发表评论:

关灯