SharePoint Online:使用 PowerShell 生成网站文档清单(库、文件夹、子文件夹和文件)报告
要求:生成一份清单报告,其中包含 SharePoint Online 网站集中的所有文档库、文件夹、子文件夹和文件详细信息。
SharePoint Online:使用 PowerShell 的网站库存报告
您是否需要创建 SharePoint Online 网站中所有文档的清单报告?好吧,您可以使用 PowerShell 来做到这一点!在这篇博文中,我将向您展示如何使用 PowerShell 脚本创建网站上所有文档的清单报告。以及有关每个文档的信息,例如文件名、文件类型、上次修改日期等。
我们想要查找 SharePoint Online 网站中每个文档库中的文件、文件夹和子文件夹的数量。此 PowerShell 脚本将给定 SharePoint Online 网站集中的所有文档库、文件夹、子文件夹和文件的清单提取到 CSV 报告。
#Function to collect site Inventory
Function Get-PnPSiteInventory
{
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Web)
#Skip Apps
If($Web.url -notlike "$SiteURL*") { return }
Write-host "Getting Site Inventory from Site '$($Web.URL)'" -f Yellow
#Exclude certain libraries
$ExcludedLists = @("Form Templates", "Preservation Hold Library")
#Get All Document Libraries from the Web
$Lists= Get-PnPProperty -ClientObject $Web -Property Lists
$Lists | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $ExcludedLists -and $_.ItemCount -gt 0} -PipelineVariable List | ForEach-Object {
#Get Items from List
$global:counter = 0;
$ListItems = Get-PnPListItem -List $_ -PageSize $Pagesize -Fields Author, Created -ScriptBlock `
{ Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($_.ItemCount) * 100) -Activity "Getting Inventory from '$($_.Title)'" -Status "Processing Items $global:Counter to $($_.ItemCount)";}
Write-Progress -Activity "Completed Retrieving Inventory from Library $($List.Title)" -Completed
#Get Root folder of the List
$Folder = Get-PnPProperty -ClientObject $_ -Property RootFolder
$SiteInventory = @()
#Iterate through each Item and collect data
ForEach($ListItem in $ListItems)
{
#Collect item data
$SiteInventory += New-Object PSObject -Property ([ordered]@{
SiteName = $Web.Title
SiteURL = $Web.URL
LibraryName = $List.Title
ParentFolderURL = $Folder.ServerRelativeURL
Name = $ListItem.FieldValues.FileLeafRef
Type = $ListItem.FileSystemObjectType
ItemRelativeURL = $ListItem.FieldValues.FileRef
CreatedBy = $ListItem.FieldValues.Author.Email
CreatedAt = $ListItem.FieldValues.Created
ModifiedBy = $ListItem.FieldValues.Editor.Email
ModifiedAt = $ListItem.FieldValues.Modified
})
}
#Export the result to CSV file
$SiteInventory | Export-CSV $CSVReport -NoTypeInformation -Append
}
}
#Parameters
$SiteURL = "https://crescent.SharePoint.com/sites/Marketing"
$CSVReport = "C:\Temp\SiteInventory.csv"
$Pagesize = 2000
#Connect to Site collection
Connect-PnPOnline -Url $SiteURL -Interactive
#Delete the Output Report, if exists
If (Test-Path $CSVReport) { Remove-Item $CSVReport }
#Call the Function for all Webs
Get-PnPSubWeb -Recurse -IncludeRootWeb | ForEach-Object { Get-PnPSiteInventory $_ }
Write-host "Site Inventory Report has been Exported to '$CSVReport'" -f Green
执行后,该脚本会生成以下格式的 CSV 文件:
如果您需要为单个文档库生成文档清单报告,请使用以下脚本:SharePoint Online:PowerShell 列出文档库中的所有文件清单
总之,在 SharePoint 中创建文档清单可能是一项耗时的任务,尤其是当您拥有多个文档库和数千个文档时。但是,使用 PowerShell,您可以自动化该过程并快速高效地生成文档清单。通过使用本教程中提供的脚本,您可以快速轻松地生成文档清单,其中包含每个文档的标题、URL、库标题、作者和创建日期等重要信息到 CSV 文件。该清单可用于各种目的,例如审计、合规性和报告。
