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

[玩转系统] SharePoint Online:使用 PowerShell 获取子网站大小

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

SharePoint Online:使用 PowerShell 获取子网站大小


要求:获取 SharePoint Online 中的子网站大小。

如何获取 SsharePoint Online 中的子网站大小

您是否正在寻找一种方法来获取 SharePoint Online 中子网站的大小?那么,您可以采取几种方法,我们将逐步介绍每一种方法来获取 SharePoint Online 中任何子网站的大小!在这篇文章中,我将向您展示如何从 Web 用户界面获取 SharePoint Online 中的子网站大小信息。另外,我将分享使用 PowerShell 在 SharePoint Online 中查找子网站大小的脚本。

要了解 SharePoint Online 中子网站占用的存储配额,请转到:

  1. 导航到您的 SharePoint Online 子网站 >> 单击“设置”齿轮 >> 站点信息 >> 查看所有站点设置。
  2. 在“网站设置”页面中,单击“网站集管理”下的“存储指标”链接。

    [玩转系统] SharePoint Online:使用 PowerShell 获取子网站大小

此页面可让您了解特定网站的每个子网站的大小。

请注意,存储指标中显示的数据不是实时的!后台计时器作业需要一段时间才能运行并填充此数据。

使用 PowerShell 获取 SharePoint Online 中的子网站大小

作为 SharePoint Online 管理员,您可以使用 PowerShell 获取网站集或子网站的大小。当尝试确定哪些网站集或子网站使用最多的存储空间时,这非常有用。以下是用于获取 SharePoint Online 中子网站大小的 PowerShell:


#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
     {
        Write-host -f Yellow "Size of the Folder '$($Folder.ServerRelativeUrl)' : " -NoNewline
        
        $FolderSize = 0
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        ForEach($File in $Folder.Files)
        {
            #Get File Size
            $FolderSize += $File.Length
        }
        Write-host $FolderSize

        #Process all Sub Folders
        ForEach($Folder in $Folder.Folders)
        {
            #Call the function recursively
            $FolderSize +=Get-SPOFolderSize $Folder
        }
        Return $FolderSize
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}
 
#parameters
$SiteURL = "https://Crescent.sharepoint.com/unitedstates"
 
#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
$SubSiteSize = Get-SPOFolderSize -Folder $RootFolder

#sharepoint online powershell site size
Write-host "Toal Size of the subsite (MB): " -NoNewline
[Math]::Round($SubSiteSize/1MB, 2)

此 PowerShell 脚本获取给定子网站的大小。如何检索网站集中所有子网站的存储消耗情况?

使用 PowerShell 查找 SharePoint Online 网站集中所有子网站的大小

让我们获取网站集中所有子网站的大小并将其导出到 CSV 文件。


#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
     {
        Write-host -f Cyan "`t Size of the Folder '$($Folder.ServerRelativeUrl)' : " -NoNewline
        
        $FolderSize = 0
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        ForEach($File in $Folder.Files)
        {
            #Get File Size
            $FolderSize += $File.Length
        }
        Write-host $FolderSize

        #Process all Sub Folders
        Foreach($Folder in $Folder.Folders)
        {
            #Call the function recursively
            $FolderSize +=Get-SPOFolderSize $Folder
        }
        Return $FolderSize
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}

Function Get-SPOSubsiteSize([Microsoft.SharePoint.Client.Web]$Web)
{  
    Write-host -f Yellow "Finding the Size of the subsite:"$web.Url 
    $StorageMetrics= @()

    #Get the Root Folder of the Web
    $Ctx = $Web.Context
    $RootFolder = $Web.RootFolder
    $Ctx.Load($RootFolder)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Call the function to get Subsite size
    $SubSiteSize = Get-SPOFolderSize -Folder $RootFolder
    $SizeinMB = [Math]::Round($SubSiteSize/1MB, 2)
    Write-host -f Green "Toal Size of the subsite (MB): $SizeinMB `n"
    
    #Add the result to an Array
    $StorageData = New-Object PSObject
    $StorageData | Add-Member NoteProperty SiteURL($web.Url)
    $StorageData | Add-Member NoteProperty Size-MB([math]::Round($SubSiteSize/1MB,2))
    $StorageMetrics += $StorageData

    #Process all subsites
    ForEach($Subweb in $web.Webs)
    {         
        $StorageMetrics+=Get-SPOSubsiteSize $Subweb
    }
    Return $StorageMetrics
}

#parameters
$SiteURL = "https://Crescent.sharepoint.com"
$CSVPath="C:\Temp\SubsiteStorage.csv"

#Delete the Output Report, if exists
If (Test-Path $CSVPath) { Remove-Item $CSVPath }

#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 Root Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Call the function to get subsite storage metrics
$SubsiteStorageMetrics = Get-SPOSubsiteSize $Web

#Export the results to CSV
$SubsiteStorageMetrics | Export-CSV -LiteralPath $CSVPath -NoTypeInformation

该脚本将返回子站点列表以及当前正在使用的存储量。如果要使用 PowerShell 获取 SharePoint Online 中的网站集大小,请使用:使用 PowerShell 检查 SharePoint Online 网站集存储大小

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

取消回复欢迎 发表评论:

关灯