SharePoint Online: PowerShell'i kullanarak Listedeki Ekler Raporunu Alma


Gereksinim: SharePoint Çevrimiçi Listesindeki tüm Eklerin Envanterini almak için bir Rapor oluşturun.

PowerShell, SharePoint Çevrimiçi Listesindeki Tüm Ekleri Alacak

Bu blog gönderisi, SharePoint Online listesindeki tüm ekleri nasıl alacağınızı gösterecektir. Listeye yüklenen dosyaları görüntülemeniz veya indirmeniz gerekiyorsa bu kullanışlı bir numaradır.

Bu PowerShell, Ek Sayısı, Boyut, Dosya Adı, URL vb. gibi tüm eklerden verileri bir CSV dosyasına çıkarır.


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

Bu komut dosyası şu şekilde bir CSV dosyası oluşturur:

PnP PowerShell, Bir Siteden Tüm Ek Envanterini Almak İçin

PnP PowerShell kullanarak belirli bir sitenin tüm listelerindeki ek verilerini bir CSV raporuna alalım:


#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

Bir listedeki tüm ekleri indirmek istiyorsanız şunu kullanın: SharePoint Online: PowerShell kullanarak Ekleri Listeden İndirin

İlgili makaleler