Last active
August 19, 2024 10:31
-
-
Save reshmee011/c7c6d2efb14f6a622774e1c737585e53 to your computer and use it in GitHub Desktop.
StorageMetrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $SiteUrl = Read-Host "Enter site collection URL" | |
| $dateTime = (Get-Date).toString("dd-MM-yyyy-hh-ss") | |
| $invocation = (Get-Variable MyInvocation).Value | |
| $directorypath = Split-Path $invocation.MyCommand.Path | |
| $fileName = "storageMetrics" + $dateTime + ".csv" | |
| $outputPath = $directorypath + "\"+ $fileName | |
| # Create storageMetrics.csv if not present | |
| if (-not (Test-Path $outputPath)) { | |
| New-Item -ItemType File -Path $outputPath | |
| } | |
| #Exclude certain libraries | |
| $ExcludedLibraries = @("Form Templates", "Preservation Hold Library", "Site Assets", "Site Pages", "Images", "Pages", "Settings", "Videos", | |
| "Site Collection Documents", "Site Collection Images", "Style Library", "AppPages", "Apps for SharePoint", "Apps for Office") | |
| function ReportStorage($siteUrl) { | |
| try { | |
| $DocLibraries = Get-PnPList -Includes BaseType, Hidden, Title -Connection $siteconn | Where-Object { $_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLibraries } | |
| $DocLibraries | ForEach-Object { | |
| Write-host "Processing Document Library:" $_.Title -f Yellow | |
| $library = $_ | |
| $relativePath = ( $_.rootfolder.ServerRelativeUrl -split "/" | Select-Object -Skip 3) -join "/" | |
| $LibraryMetric= Get-PnPFolderStorageMetric -FolderSiteRelativeUrl $relativePath | |
| # $FolderSize = [Math]::Round($FolderSize/1MB, 2) | |
| $item = [PSCustomObject]@{ | |
| Title = $library.Title | |
| Type = "Library" | |
| ServerRelativePath = $_.rootfolder.ServerRelativeUrl | |
| FileSizeMB = $([Math]::Round(($libraryMetric.TotalSize/1MB),2)) | |
| PercentOfSite = "$(([Math]::Round(($libraryMetric.TotalSize/1MB),2)/$siteStorage) * 100) %" | |
| VersionCount = "N/A" | |
| TotalFileCount = $libraryMetric.TotalFileCount | |
| LastModified = $libraryMetric.LastModified | |
| } | |
| $item | Export-Csv -Path $outputPath -NoTypeInformation -Append | |
| $listItems = Get-PnPListItem -List $library.Title -Fields "ID" -PageSize 1000 -Connection $siteconn | |
| #Get file zize | |
| $listItems | ForEach-Object { | |
| $fileSize = 0 | |
| $totalVersionSize = 0 | |
| $listitem = $_ | |
| $type= $listitem.FileSystemObjectType; | |
| if($type -eq "File"){ | |
| $file = Get-PnPFile -Url $listitem["FileRef"] -AsFileObject -ErrorAction SilentlyContinue -Connection $siteconn | |
| $versionCount=0 | |
| if ($file) { | |
| $fileSize += $file.Length | |
| $fileversions = Get-PnPFileVersion -Url $listitem["FileRef"] -Connection $siteconn | |
| if ($fileversions) { | |
| if ($fileversions.Count) { | |
| $versionCount = $fileversions.Count | |
| #$VersionList = ($fileversions[0..$($fileversions.Count - $versionsToKeep)]) | |
| # Calculate the total version size | |
| $totalVersionSize +=$fileversions | Measure-Object -Property Size -Sum | Select-Object -ExpandProperty Sum | |
| } | |
| $FileSize+=$totalVersionSize | |
| } | |
| } | |
| $item = [PSCustomObject]@{ | |
| Title = $listitem.FieldValues["FileLeafRef"] | |
| Type = $type | |
| ServerRelativePath = $listitem.FieldValues["FileRef"] | |
| FileSizeMB = $([Math]::Round(($FileSize/1MB),2)) | |
| PercentOfSite = "$(([Math]::Round(($FileSize/1MB),2)/$siteStorage) * 100) %" | |
| VersionCount = $VersionCount | |
| TotalFileCount = 1 | |
| LastModified = $listitem["Last_x0020_Modified"] | |
| } | |
| $item | Export-Csv -Path $outputPath -NoTypeInformation -Append | |
| } | |
| If($type -eq "Folder"){ | |
| $relativePath = ($listitem["FileRef"] -split "/" | Select-Object -Skip 3) -join "/" | |
| $FolderMetric= Get-PnPFolderStorageMetric -FolderSiteRelativeUrl $relativePath | |
| # $FolderSize = [Math]::Round($FolderSize/1MB, 2) | |
| #$FileSize =$FolderSize | |
| $item = [PSCustomObject]@{ | |
| Title = $listitem.FieldValues["FileLeafRef"] | |
| Type = $type | |
| ServerRelativePath = $listitem["FileRef"] | |
| FileSizeMB = $([Math]::Round(($FolderMetric.TotalSize/1MB),2)) | |
| VersionCount = $VersionCount | |
| PercentOfSite = "$(([Math]::Round(($FolderMetric.TotalSize/1MB),2)/$siteStorage) * 100) %" | |
| TotalFileCount = $FolderMetric.TotalFileCount | |
| LastModified = $FolderMetric.LastModified | |
| } | |
| $item | Export-Csv -Path $outputPath -NoTypeInformation -Append | |
| } | |
| } | |
| } | |
| } | |
| catch { | |
| Write-Output " An exception was thrown: $($_.Exception.Message)" -ForegroundColor Red | |
| } | |
| } | |
| # Get total storage use for this site collection | |
| <#WARNING: Connecting with -Interactive uses the PnP Management Shell multi-tenant App Id for authentication. It is strongly recommended to register your own EntraID App for authentication. See the documentation for Register-PnPEntraIDApp.#> | |
| Connect-PnPOnline -url $SiteUrl -Interactive | |
| $site = Get-PnPTenantSite -Identity $SiteUrl | |
| $siteStorage = $site.StorageUsageCurrent | |
| $metrics = Get-PnPFolderStorageMetric | |
| Write-Host "Site storage: $siteStorage MB" | |
| $item = [PSCustomObject]@{ | |
| Title = $site.Title | |
| Type = "Site" | |
| ServerRelativePath = $site.Url | |
| FileSizeMB = $site.StorageUsageCurrent | |
| PercentOfSite = " 100 %" | |
| VersionCount = "n/a" | |
| TotalFileCount = $metrics.TotalFileCount | |
| LastModified = $site.LastContentModifiedDate | |
| } | |
| $item | Export-Csv -Path $outputPath -NoTypeInformation -Append | |
| ReportStorage -siteUrl $site.Url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment