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

[玩转系统] SharePoint Online:使用 PowerShell 下载文件

作者:精品下载站 日期:2024-12-14 14:19:57 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 下载文件


要求:使用 PowerShell 从 SharePoint Online 下载文件。

如何从 SharePoint Online 库下载文件?

从 SharePoint Online 下载文件非常简单。如果您需要从 SharePoint Online 下载文件,只需浏览到包含该文档的网站,右键单击它,然后在菜单栏中选择“下载” - 就这么简单。如果您需要访问与您共享的文档(无需与本地计算机建立任何连接),这会很有用。如何从 SharePoint Online 下载文档?让我们看看如何下载文件的分步说明。

要从库下载文件,请按照下列步骤操作:

  1. 登录到 SharePoint Online 网站 >> 导航到所需文件所在的库。
  2. 右键单击该文件,然后从上下文菜单中选择“下载”选项。

    [玩转系统] SharePoint Online:使用 PowerShell 下载文件

  3. 这会出现“保存”提示,并且文件将保存在客户端计算机的下载目录中。
  4. 在现代库中,您可以选择文件或文件夹,然后单击工具栏中的“下载”按钮。您还可以一次下载多个文件。

    [玩转系统] SharePoint Online:使用 PowerShell 下载文件

将它们全部选中,然后单击工具栏中的“下载”按钮。这会将所有选定的文件下载为 .zip 文件,然后您可以将其解压到计算机上。

您可以在“文件资源管理器”视图中打开任何文档库或文件夹,然后只需将文件复制粘贴到本地计算机即可下载!您还可以使用“同步”选项将所有文件/文件夹从库下载到您的计算机。

现在,如何使用 PowerShell 从 SharePoint Online 下载文件?好吧,这是从 SharePoint Online 下载文件的 PowerShell 脚本!

SharePoint Online PowerShell 从库下载文件

让我们使用 PowerShell 从 SharePoint Online 文档库下载文档。此 CSOM PowerShell 脚本从 SharePoint Online 文档库(或任何库!)下载文件。它需要以下参数:站点的 URL、文件的服务器相对 URL 和本地路径。需要注意的是,只有当用户有足够的权限访问站点和库时,该脚本才会起作用。


#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 copy a given sourcefile to downloadpath
Function Download-FileFromLibrary()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $SourceFileURL,
        [Parameter(Mandatory=$true)] [string] $TargetFilePath
    )

    Try {
        #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
    
        #sharepoint online powershell download file from library
        $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$SourceFileURL)
        $WriteStream = [System.IO.File]::Open($TargetFilePath,[System.IO.FileMode]::Create)
        $FileInfo.Stream.CopyTo($WriteStream)
        $WriteStream.Close()

        Write-host -f Green "File '$SourceFileURL' Downloaded to '$TargetFilePath' Successfully!" $_.Exception.Message
  }
    Catch {
        write-host -f Red "Error Downloading File!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/sales"
$SourceFileURL="/sites/Sales/Shared Documents/Crescent Legal App Requirements.docx"  #Server Relative URL 
$TargetFilePath="C:\Temp\LegalDoc.docx"

#Call the function to download file
Download-FileFromLibrary -SiteURL $SiteURL -SourceFileURL $SourceFileURL -TargetFilePath $TargetFilePath

若要从 SharePoint Online 文档库下载所有文件,请使用以下脚本:SharePoint Online 使用 PowerShell 下载多个文件

使用 PnP PowerShell 从 SharePoint Online 下载文件

我们可以使用 PowerShell 将文件从 SharePoint Online 复制到本地磁盘。以下是从 SharePoint Online 下载文件的 PnP PowerShell:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$FileServerRelativeURL = "/sites/Retail/Shared%20Documents/DocumentsInventory.csv"
$DestinationFolder ="C:\Temp"

Try {
     #Check if the Destination Folder exists. If not, create the folder for targetfile
     If(!(Test-Path -Path $DestinationFolder))
     {
        New-Item -ItemType Directory -Path $DestinationFolder | Out-Null
        Write-host -f Yellow "Created a New Folder '$DestinationFolder'"
     }

    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Check if File exists
    $File = Get-PnPFile -Url $FileServerRelativeURL -ErrorAction SilentlyContinue
    If($File -ne $Null)
    {
        #Download file from sharepoint online
        Get-PnPFile -Url $FileServerRelativeURL -Path $DestinationFolder -Filename $File.Name -AsFile -Force
        Write-host "File Downloaded Successfully!" -f Green
    }
    Else
    {
        Write-host "Could not Find File at "$FileServerRelativeURL -f Red
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

此 PowerShell 在没有提示的情况下从 SharePoint Online 本地复制文件。 Get-PnPFile cmdlet 还具有其他参数,如 FileName 等。以下是如何使用 PowerShell 从 SharePoint Online 文档库下载和读取 CSV 文件的示例:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$FileServerRelativeURL = "/sites/retail/Invoices/MonthlyRpt.csv"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

$DownloadPath = $env:TEMP
$FileName = Split-Path $FileServerRelativeURL -leaf

#Download file from sharepoint online to Temp Folder
Get-PnPFile -Url $FileServerRelativeURL -Path $DownloadPath -Filename $FileName -AsFile -Force

#Import the CSV file
$CSVFile = Import-Csv -Path $env:TEMP$FileName

如何使用 PowerShell 从 SharePoint 下载文件夹?要使用 PowerShell 从 SharePoint Online 下载文件夹,请使用:PowerShell 从 SharePoint Online 下载文件夹

经常问的问题:

如何从 SharePoint 网站下载所有文件?

要从 SharePoint 站点上的所有库下载所有文件,请使用“文件资源管理器”视图。首先,导航到要从中下载的文档库或文件夹。然后,单击“库”选项卡并选择“使用资源管理器打开”。这将打开一个 Windows 资源管理器窗口,您可以在其中选择所有文件并将它们拖到计算机上的本地文件夹。
或者,您可以使用 PowerShell 脚本:如何从 SharePoint 网站下载所有文件?

如何从 SharePoint Online 库下载所有文件?

要从 SharePoint Online 文档库下载所有文件,请浏览到您的 SharePoint 文档库 >> 选择所有文件和文件夹。单击工具栏中的“下载”按钮。您可以使用“文件资源管理器视图”或 PowerShell 作为下载文档库的替代选项。
更多信息:如何在 SharePoint Online 中下载文档库?

如何从 SharePoint Classic 下载多个文件?

使用“文件资源管理器”视图或 OneDrive 同步客户端从 SharePoint Online 网站下载文件。您还可以使用 PowerShell 下载多个文件。

如何关闭 SharePoint 中的下载权限?

要禁用 SharePoint 中的下载权限,您可以向用户授予“受限查看”(或“仅查看”)权限,这将阻止用户下载文件。您还可以使用信息权限管理 (IRM) 来限制对某些文件的访问并防止下载或打印它们。详细信息:如何阻止 SharePoint 文档库中的下载?

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

取消回复欢迎 发表评论:

关灯