Obtener el inventario de documentos para una colección de sitios mediante PowerShell
Requisito: Obtener todo el inventario de documentos en formato CSV de una colección de sitios de SharePoint.
PowerShell para obtener el inventario de documentos de un sitio de SharePoint:
Las colecciones de sitios de SharePoint pueden llenarse rápidamente de archivos y documentos. El inventario de documentos puede ayudar a realizar un seguimiento de lo que se almacena en cada colección de sitios y dónde se encuentran archivos específicos. En esta publicación de blog, lo guiaremos en la generación de un inventario de todos los documentos en su sitio de SharePoint usando PowerShell y cómo exportar la lista a un archivo CSV.
Aquí está PowerShell que obtiene todos los detalles del documento de un sitio determinado:
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ReportFile = "C:\Temp\SiteDocsInventoryRpt.csv"
# Function to retrieve Documents Inventory Report
Function Export-DocsInventory($List)
{
#Array to hold Storage data for all files
$DocInventoryCollection = @()
#Query to batch process
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.ViewAttributes = "Scope='Recursive'"
$Query.RowLimit = 2000
$Counter = 1
Do {
#Get List Items defined by the Query
$ListItems = $List.GetItems($Query)
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each list Item
ForEach($Item in $ListItems)
{
Write-Host "Processing File: $($File.Name) ($Counter of $($List.ItemCount))"
#Get File Inventory
$File = $Item.File
$FileSize = $File.TotalLength
$DocInventory = New-Object PSObject
$DocInventory | Add-Member -type NoteProperty -name "File Name" -value $File.Name
$DocInventory | Add-Member -type NoteProperty -name "File Size(KB)" -value $($FileSize/1KB)
$DocInventory | Add-Member -type NoteProperty -name "Created on" -value $File.TimeCreated
$DocInventory | Add-Member -type NoteProperty -name "Last Modified" -value $File.TimeLastModified
$DocInventory | Add-Member -type NoteProperty -name "Created by" -value $File.Author.Name
$DocInventory | Add-Member -type NoteProperty -name "Parent Folder" -value $File.ParentFolder.URL
$DocInventory | Add-Member -type NoteProperty -name "URL" -value $File.URL
$DocInventoryCollection += $DocInventory
$Counter++
}
} While ($Query.ListItemCollectionPosition -ne $Null)
#Export the data to CSV File
$DocInventoryCollection | Export-csv $ReportFile -notypeinformation -Append
}
#Get the site collection
$Site = Get-SPSite $SiteURL
$ResultData = @()
If(Test-Path $ReportFile) { Remove-Item $ReportFile }
#Ge All Sites of the Site collection
Foreach($web in $Site.AllWebs)
{
Write-host -f Yellow "Processing Site: "$Web.URL
#Get all Document Libraries - Exclude Hidden and System libraries
$DocumentLibraries = $web.lists | Where-Object { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.BaseType -eq "DocumentLibrary")}
#Iterate through All lists and Libraries
ForEach ($Library in $DocumentLibraries)
{
#Call the function to get data
Export-DocsInventory $Library
}
}
En conclusión, obtener una lista de todos los documentos de una colección de sitios de SharePoint es una forma útil de administrar y organizar archivos dentro de una organización. Al utilizar PowerShell, puede recuperar fácilmente una lista de todos los documentos en múltiples colecciones de sitios y subsitios. Además, puede filtrar y ordenar los resultados según criterios específicos abriendo el archivo CSV generado en Microsoft Excel.
Para generar un informe sobre todos los documentos en una colección de sitios de SharePoint Online, use: SharePoint Online: obtener el inventario de todos los documentos en un sitio usando PowerShell
