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

[玩转系统] SharePoint Online:用于获取文件的 PowerShell

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

SharePoint Online:用于获取文件的 PowerShell


要求:使用 PowerShell 从 SharePoint Online 获取文件。

[玩转系统] SharePoint Online:用于获取文件的 PowerShell

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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$FileRelativeURL ="/sites/marketing/Shared Documents/Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #powershell get file from sharepoint online
    $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)
    $Ctx.ExecuteQuery()
 
    Write-host "File Size:" ($File.Length/1KB)
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
}

从 SharePoint Online 中的绝对 URL 获取文件

以下是在 SharePoint Online 中通过 URL 获取文件的 PowerShell:


#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$FileURL = "https://crescent.sharepoint.com/sites/marketing/Shared Documents/Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #powershell sharepoint online get file by url
    $File = $Ctx.web.GetFileByUrl($FileURL)
    $Ctx.Load($File)
    $Ctx.ExecuteQuery()
    
    Write-host "File Size:" ($File.Length/1KB)    
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
} 

SharePoint Online:PowerShell 从文档库中按名称获取文件

以下是用于检索 SharePoint Online 文档的 PowerShell 脚本:


#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName ="Documents"
$FileName = "Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get List Items
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Get File from by Name
    $Item =  $ListItems | where {$_["FileLeafRef"] -eq $FileName} | Select -First 1

    If($Item -ne $Null)
    {
        #Get the File from List Item
        $File = $Item.File
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()

        Write-host "File Size:" ($File.Length/1KB)
    }
    else
    {
        Write-host -f Yellow "File Not Found!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
}

PowerShell 使用 CAML 查询从 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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName ="Documents"
$FileName = "Compliance Process.xlsx"

#Get Credentials to connect
$Cred = Get-Credential
 
Try { 
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get List
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Frame CamlQuery to retrieve items by File name
    $CAMLQuery= New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$FileName</Value></Eq></Where></Query></View>"

    #Get List Items matching given File Name
    $ListItems = $List.GetItems($CAMLQuery)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Get the First File matching File Name
    If($ListItems -ne $Null)
    {
        #sharepoint online powershell get file
        $File = $ListItems[0].File
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()

        Write-host "File Size:" ($File.Length/1KB)
    }
    else
    {
        Write-host -f Yellow "File Not Found!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -Foregroundcolor Red
}

SharePoint Online:使用 PowerShell 获取文件

以下是通过 PowerShell 访问 SharePoint Online 中的文件的方法:


#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$FileRelativeURL = "/sites/marketing/Shared Documents/Proposal Template.docx"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the File as List Item
$File = Get-PnPFile -Url $FileRelativeURL -AsListItem
 
#Get File Size
Write-host "File Size (KB):"$File["File_x0020_Size"]

获取 SharePoint Online 中存储的文件的内容

读取存储在 SharePoint Online 文档库中的日志文件的内容怎么样?


#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/PMO"
$FileRelativeURL = "/sites/PMO/Shared Documents/Log.txt"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
  
#Get the Contents of the File
$File = Get-PnPFile -Url $FileRelativeURL -AsString

Write-host $File

如果您要下载文件,请参阅 PowerShell 脚本从 SharePoint Online 下载文件

相关帖子:

  • 使用 PowerShell 从 SharePoint Online 文档库获取所有文件
  • PowerShell 从 SharePoint Online 中的文件夹获取文件

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

取消回复欢迎 发表评论:

关灯