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

[玩转系统] SharePoint Online:使用 PowerShell 查看文件夹大小

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

SharePoint Online:使用 PowerShell 查看文件夹大小


要求:获取 SharePoint Online 中的文件夹大小。

如何检查 SharePoint Online 中的文件夹大小?

管理 SharePoint 内容可能是一项艰巨的任务,尤其是当您必须了解文件夹的大小时。您需要知道文件夹的总大小,以确定站点上剩余的空间以及将来的数据存储分配。在这篇博文中,我将向您展示如何从网站设置页面并使用 PowerShell 脚本检查 SharePoint Online 中文件夹的大小。

如何检查 SharePoint Online 中的文件夹大小?要查看 SharePoint Online 中的文件夹大小,请转到:

  1. 导航到站点>>单击“设置”齿轮>>“站点设置”>>单击“站点集管理”下的“存储指标”链接。
  2. 在存储指标页面,单击文档库,导航至目标文件夹的存储位置。

    [玩转系统] SharePoint Online:使用 PowerShell 查看文件夹大小

存储指标页面显示每个文件夹和文件的大小(包括版本历史记录大小)。值得一提的是,存储的metrics页面数据并不是实时数据!后台计时器作业需要一段时间才能运行并填充此数据。

SharePoint Online:使用 PowerShell 获取文件夹大小

随着 SharePoint 库中存储的数据量不断增长,监视和管理文件夹和库的大小非常重要。实现此目的的一种方法是使用 PowerShell,它提供了强大的脚本环境来自动执行任务并检索有关 SharePoint Online 的信息。

如何在 SharePoint Online 中查找文件夹大小?好吧,让我们使用 PowerShell 脚本检查 SharePoint Online 网站中所有文件夹的文件夹大小:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Function to Get the size of a Folder in SharePoint Online
Function Get-SPOFolderSize([Microsoft.SharePoint.Client.Folder]$Folder)
{ 
     Try
     {
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        $FolderSize = 0
        ForEach($File in $Folder.Files | Where {-Not($_.Name.EndsWith(".aspx"))})
        {
            #Get File versions
            $Ctx.Load($File.Versions)
            $Ctx.ExecuteQuery()
            $VersionSize=0
            If($File.Versions.Count -ge 1)
            {
                #Calculate Version Size
                $VersionSize = $File.Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum                                
            }
            $FileSize =  [Math]::Round((($File.Length) + $VersionSize)/1KB, 2)
            If($FileSize -gt 0)
            {
                Write-host "`tSize of the File '$($File.Name)' "$FileSize
            }

            #Get File Size
            $FolderSize += $FileSize
        }

        If($FolderSize -gt 0)
        {
            Write-host -f Yellow "Total Size of the Folder '$($Folder.ServerRelativeUrl)' (KB): " -NoNewline
            $FolderSize= [Math]::Round($FolderSize/1KB, 2)
            Write-host $FolderSize
        }
        #Process all Sub Folders
        ForEach($Folder in $Folder.Folders | Where {-Not($_.Name.StartsWith("_") -or $_.Name -eq "Forms")})
        {
            #Call the function recursively
            Get-SPOFolderSize $Folder
        }
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}
  
#parameters
$SiteURL = "https://Crescent.sharepoint.com"

#Get credentials to connect to SharePoint Online Admin Center
$Cred = Get-Credential
  
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
#Get the Web
$Web = $Ctx.Web
$RootFolder = $Web.RootFolder
$Ctx.Load($RootFolder)
$Ctx.ExecuteQuery()
 
#Call the function to get Subsite size
Get-SPOFolderSize -Folder $RootFolder

如何使用 PnP PowerShell 查看 SharePoint Online 中的文件夹大小?

我们可以直接从文件夹的属性 SMTotalSize 中获取文件夹大小(文件 + 版本),而不是汇总每个文件及其版本的大小!


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
$FolderSiteRelativeURL = "/Branding/Active"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
    
#Get the folder
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL -Includes ListItemAllFields

#Get the total Size of the folder - with versions
Write-host "Size of the Folder:" $([Math]::Round(($Folder.ListItemAllFields.FieldValues.SMTotalSize.LookupId/1KB),2))

查找文档库中所有文件夹的大小怎么样?


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
$ListName = "Branding"
$CSVFile = "C:\Temp\FolderSize.csv"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Get all folders from the document library
    $Folders = Get-PnPListItem -List $ListName -PageSize 2000 | Where { $_.FileSystemObjectType -eq "Folder" }
    
    #Calculate Folder Size from files
    $FolderSizeData = @()
    $Folders | ForEach-Object {
        #Extract Folder Size data
        $FolderSizeData += New-Object PSObject -Property  ([Ordered]@{
            "Folder Name"  = $_.FieldValues.FileLeafRef
            "URL" = $_.FieldValues.FileRef        
            "Size" = $_.FieldValues.SMTotalSize.LookupId
        })
    }
    $FolderSizeData | Format-Table
    $FolderSizeData | Export-csv $CSVFile -NoTypeInformation
    #Calculate the Total Size of Folders
    $FolderSize = [Math]::Round((($FolderSizeData | Measure-Object -Property "Size" -Sum | Select-Object -expand Sum)/1KB),2)
    Write-host -f Green ("Total Size: {0}" -f $FolderSize)
} 
Catch {
    Write-Host -f Red "Error:"$_.Exception.Message
}

结论:

在本教程中,我们讨论了如何使用存储指标页面和 PowerShell 检索 SharePoint Online 中的文件夹大小。通过使用 SharePoint 客户端对象模型 (CSOM) 和 PnP PowerShell 脚本,我们能够检索文件夹及其子文件夹的大小,为管理 SharePoint Online 环境的存储使用情况提供有价值的信息。此方法可以用作更大的自动化流程的一部分,有助于保持 SharePoint 环境的组织和优化。

  • 如何将文件大小列添加到 SharePoint Online 文档库?
  • SharePoint Online:使用 PowerShell 获取文件大小
  • SharePoint Online:使用 PowerShell 获取子网站大小
  • SharePoint Online:使用 PowerShell 获取回收站存储大小

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

取消回复欢迎 发表评论:

关灯