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

[玩转系统] SharePoint Online:使用 PowerShell 获取列表中的附件报告

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

SharePoint Online:使用 PowerShell 获取列表中的附件报告


要求:生成报告以获取 SharePoint Online 列表中所有附件的清单。

PowerShell 获取 SharePoint Online 列表中的所有附件

这篇博文将向您展示如何从 SharePoint Online 列表中获取所有附件。如果您需要查看或下载已上传到列表的文件,这是一个方便的技巧。

此 PowerShell 将所有附件中的数据(例如附件数量、大小、文件名、URL 等)提取到 CSV 文件。


Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName= "Proposals"
$CSVPath = "C:\Temp\AttachmentInventory.csv"
$AttachmentsData = @()
 
#Setup Credentials to connect
$Cred = Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)

#CAML Query to Get List Items with Attachments
$CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$CAMLQuery.ViewXml ="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Attachments' /><Value Type='Boolean'>1</Value></Eq></Where></Query></View>"    
$ListItems=$List.GetItems($CAMLQuery)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

ForEach($Item in $ListItems)
{
        Write-Host "Processing item ID:" $Item.ID
        
        #Get Attachments from List Item
        $Attachments = $Item.AttachmentFiles
        $Ctx.Load($Attachments)
        $Ctx.ExecuteQuery()            

        #Get the attachment details
        foreach($Attachment in $Attachments)
        {
            $File = $Ctx.Web.GetFileByServerRelativeUrl($Attachment.ServerRelativeUrl)
            $Ctx.Load($File)
            $Ctx.Load($File.Author)
            $Ctx.ExecuteQuery()
            
            $AttachmentsData += New-Object PSObject -Property ([ordered]@{
                ItemID  = $Item.Id
                TotalAttachments = $Attachments.Count
                FileName = $File.Name
                URL = $File.ServerRelativeUrl
                AddedBy =  $File.Author.Email
                "FileSize (KB)" = [Math]::Round(($File.Length/1KB),2)
            })
        }
}
#Export Attachments Inventory to CSV
$AttachmentsData | Export-Csv $CSVPath -NoTypeInformation
Write-host "Attachments Inventory has been Exported to CSV File!" -f Green

该脚本生成一个 CSV 文件,如下所示:

[玩转系统] SharePoint Online:使用 PowerShell 获取列表中的附件报告

PnP PowerShell 从站点获取所有附件清单

让我们使用 PnP PowerShell 将给定站点的所有列表中的附件数据获取到 CSV 报告:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$CSVPath = "C:\Temp\Attachments.csv"

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

#Get All Custom Lists from the Web
$Lists = Get-PnPList | Where-Object {$_.BaseTemplate -Eq 100}

$Resultset = @()
#Loop through each list and collect attachment data
ForEach($List in $Lists)
{
    #Get All List Items with Attachments
    $SPQuery = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Attachments' /><Value Type='Boolean'>1</Value></Eq></Where></Query></View>"
    $ListItems = Get-PnPListItem -List $List -PageSize 2000 -Query $SPQuery
    
    #Loop through each item in the list
    ForEach ($ListItem in $ListItems)
    {
         #Get All Attachments of the List Item
        $Attachments = Get-PnPProperty -ClientObject $ListItem -Property "AttachmentFiles"
        
        #Collect Attachment Properties
        ForEach($Attachment in $Attachments)
        {
            $Resultset += New-Object PSObject -Property ([ordered]@{
                AttachmentName = $Attachment.FileName
                ServerRelativeUrl = $Attachment.ServerRelativeUrl
                ListName = $List.Title
                ItemID = $ListItem.ID
                TotalAttachments = $Attachments.Count
                CreatedBy = $ListItem.FieldValues.Author.LookupValue                
            })
        }
    }
}
#Export Report to a CSV File
$Resultset
$Resultset | Export-Csv -Path $CSVPath -NoTypeInformation

如果要从列表中下载所有附件,请使用:SharePoint Online:使用 PowerShell 从列表下载附件

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

取消回复欢迎 发表评论:

关灯