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

[玩转系统] 使用 PowerShell 获取网站集的文档清单

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

使用 PowerShell 获取网站集的文档清单


要求:从 SharePoint 网站集中获取 CSV 格式的所有文档清单。

使用 PowerShell 获取 SharePoint 网站的文档清单:

SharePoint 网站集很快就会因文件和文档而变得混乱。文档清单可以帮助跟踪每个网站集中存储的内容以及特定文件的位置。在这篇博文中,我们将引导您使用 PowerShell 生成 SharePoint 网站中所有文档的清单,以及如何将该列表导出到 CSV 文件。

[玩转系统] 使用 PowerShell 获取网站集的文档清单

下面是 PowerShell 从给定站点获取所有文档详细信息的示例:


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
    }
}

总之,获取 SharePoint 网站集中所有文档的列表是管理和组织组织内文件的有用方法。通过使用 PowerShell,您可以轻松检索多个网站集和子网站中的所有文档的列表。此外,您还可以通过在 Microsoft Excel 中打开生成的 CSV 文件,根据特定条件对结果进行筛选和排序。

要生成有关 SharePoint Online 网站集中所有文档的报告,请使用:SharePoint Online:使用 PowerShell 获取网站中的所有文档清单

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

取消回复欢迎 发表评论:

关灯