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

[玩转系统] 使用 PowerShell 的 SharePoint 站点详细存储报告

作者:精品下载站 日期:2024-12-14 13:47:17 浏览:14 分类:玩电脑

使用 PowerShell 的 SharePoint 站点详细存储报告


要求:生成有关 SharePoint 网站的所有列表和库大小的详细报告。

[玩转系统] 使用 PowerShell 的 SharePoint 站点详细存储报告

您是否想知道您的 SharePoint 网站使用了多少存储空间?或者,您可能想知道哪些列表和库占用了最多的空间。无论哪种情况,您都可以使用 PowerShell 为您的站点生成详细的存储报告。该报告将向您显示网站上所有列表、库的总大小。这是查看站点存储的使用情况并确定潜在优化领域的好方法。我们来看看如何生成此报告。

以下是通过列出所有列表和库大小来生成详细存储信息的 PowerShell 脚本:


#Get Size of all lists and libraries in a SharePoint Site

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
 
#Function to Get Storage Metrics for a list/library 
Function GetStorageMetrics($Folder)
{   
    $TotalSize=0 
    #Get the size of each file in the folder
    foreach ($File in $Folder.Files)
    {
        #Get File Size
        $FileSize = $File.TotalLength
        
        $VersionSize=0
        #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
        {
            $VersionSize += $FileVersion.Size
        }
        $TotalSize += $VersionSize+ $FileSize  
    }
    

      #Get Files in Sub Folders
      foreach ($SubFolder in $Folder.SubFolders)
      {   
         if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.
             {
                #Call the function recursively for the subfolders
                $TotalSize += GetStorageMetrics($SubFolder)          
             }
        } 
        
        #Return the object
     return $TotalSize
}

#Set Site URL  variable
$WebURL = "https://sharepoint.crescent.com"
#Get the Web
$Web = Get-SPWeb $WebURL

#Array to hold Storage data for all lists and libraries
$StorageDataCollection = @() 

foreach($List in $Web.Lists)
 {
    #Leave the Hidden Lists and exclude certain Libraries
    if($List.Hidden -eq $false)
    {
        #Write-host "Procesing list:" $list.title
        #Call the function to get library's Size relatedMetrics
        $LibrarySize = GetStorageMetrics($List.RootFolder)
        
        #Create an object to hold storage data
        $StorageDataResult = New-Object PSObject
        
        #Add other details of the list 
        $StorageDataResult | Add-Member -type NoteProperty -name "List/Library Name" -value $List.Title
        $StorageDataResult | Add-Member -type NoteProperty -name "Item/File Count" -value $List.ItemCount
        $StorageDataResult | Add-Member -type NoteProperty -name "Created by" -value $List.Author
        $StorageDataResult | Add-Member -type NoteProperty -name "Created on" -value $List.Created
        $StorageDataResult | Add-Member -type NoteProperty -name "Last Modified" -value $List.LastItemModifiedDate
        $StorageDataResult | Add-Member -type NoteProperty -name "URL" -value $List.DefaultViewUrl
        
        #Add storage related Data
        $StorageDataResult | Add-Member -type NoteProperty -name "Total Size (KB)" -value ([Math]::Round(($LibrarySize/1KB),2))

        #Add object to an array
        $StorageDataCollection += $StorageDataResult        
    }
 }

#Get Recycle bin size
$RecyclebinSize=0
foreach($RecycleBinItem in $Web.RecycleBin)
        {
           $RecyclebinSize += $RecycleBinItem.Size
        }
#Get the Total size of the library
$TotalSiteSize = ($StorageDataCollection | Measure-Object 'Total Size (KB)' -Sum | Select -expand Sum)


Write-host "Total Lists/Libraries Size in MB: "([Math]::Round(($TotalSiteSize/1KB),2))
write-host "Recycle bin Size in MB:" ([Math]::Round(($RecyclebinSize/1MB),2))
#Add recycle bin size
Write-host "Total site Size in MB: "([Math]::Round(($TotalSiteSize/1KB)+($RecyclebinSize/1MB),2))
write-host "Total Number of Lists/Libraries in the Site:" $Web.Lists.Count 

#export the detailed storage Info the CSV file
$StorageDataCollection | sort-object "Total Size" -descending | Export-csv "SiteStroageRpt.csv" -notypeinformation
Write-host "Site Storage Report has been generated!"
      
$Web.dispose()

该脚本生成 CSV 格式的报告。这是导出到 Excel 后的快照:

[玩转系统] 使用 PowerShell 的 SharePoint 站点详细存储报告

它还给出了列表、库的总大小和回收站的大小。

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

取消回复欢迎 发表评论:

关灯