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 เป็น:
PnP PowerShell เพื่อรับรายการเอกสารแนบทั้งหมดจากไซต์
รับข้อมูลไฟล์แนบจากรายการทั้งหมดของไซต์ที่กำหนดไปยังรายงาน CSV โดยใช้ PnP PowerShell:
#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
