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

[玩转系统] SharePoint Online:使用 PowerShell 获取回收站存储大小

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

SharePoint Online:使用 PowerShell 获取回收站存储大小


要求:获取所有 SharePoint Online 网站集的回收站存储空间消耗情况

[玩转系统] SharePoint Online:使用 PowerShell 获取回收站存储大小

SharePoint Online:查找网站集的回收站存储大小

您是否想知道 SharePoint Online 回收站占用了多少空间? SharePoint Online 中的回收站允许您在指定时间段内恢复已删除的内容,例如列表、库和文档。因此,回收站还会消耗存储空间,这可能会对 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"
  
#Variables for Processing
$SiteUrl = "https://Crescent.sharepoint.com/Sites/Marketing"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
    #Get the Site collection's recycle bin: both 1st stage and 2nd stage
    $RecycleBinItems = $Ctx.Site.RecycleBin
    $Ctx.Load($RecycleBinItems)
    $Ctx.ExecuteQuery()

    #Get Size of the Recycle bin items
    $RecycleBinSize = $RecycleBinItems | Measure-Object Size -Sum

    Write-Host -f Green "Total Recycle Bin Size: $($RecycleBinSize.Sum/1MB) MB"
    
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PowerShell 获取 SharePoint Online 回收站存储大小

如何查找租户中所有 SharePoint Online 网站的回收站大小?


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
 
#Function to Get the recycle bin Size of a SharePoint Online site collection
Function Get-SPOSiteRecycleBinSize($SiteURL, $Cred)
{ 
    #Setup credentials
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
    $Context.Credentials = $Credentials
   
    $Site = $context.Site
    $RecycleBin = $Site.RecycleBin
    $context.Load($Site)
    $context.Load($RecycleBin)
    $context.ExecuteQuery()

    $FirstStageSize=0
    $SecondStageSize=0

    Foreach($Item in $RecycleBin)
    {
        If($Item.itemState -eq "FirstStageRecycleBin")
        {
          $FirstStageSize+=$Item.Size
        }
        Else
        {
          $SecondStageSize+=$Item.Size
        }
    }
    #Output the results
    $Data ="Site Collection URL {0}. First Stage Recycle Bin Size {1}. Second Stage Recycle Bin Size {2}" -f $SiteURL,$FirstStageSize,$SecondStageSize
    Write-host -f Green $Data
}

#Get All Site collections from the Tenant- Including Modern Team sites and communication sites
Function Get-SPOSites($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
 
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()

    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        Write-host -f Yellow "Searching Site Collection:"$Site.URL
        Get-SPOSiteRecycleBinSize -SiteUrl $Site.Url -Cred $Cred
    }
}
 
#Set Parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com/"
$Cred= Get-Credential

Get-SPOSites -AdminSiteURL $AdminSiteUrl -Cred $Cred

该脚本获取租户所有网站集第一阶段和第二阶段回收站占用的存储空间。

PnP PowerShell 获取SharePoint Online 网站集的回收站大小

您可以使用 Get-PnPRecycleBinItem cmdlet 查看网站集回收站中的所有项目:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Sum Recycle bin Items Size
$RecycleBinSize = Get-PnPRecycleBinItem -RowLimit 500000 | Measure-Object -Property Size -Sum

#Get Recycle bin size
Write-host "Recycle Bin Size (MB):" ([Math]::Round($RecycleBinSize.Sum/1MB,2)) 

为所有 SharePoint Online 网站生成回收站大小报告

如何找出租户中所有站点的回收站大小?这是 PowerShell 脚本:


# Parameters
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$OutputPath = "C:\Temp\RecycleBinRpt.csv"

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminCenterURL -Interactive
 
    #Get All Site Collections
    $SiteCollections  = Get-PnPTenantSite -IncludeOneDriveSites:$false

    $RecyclebinData = @()
    $Counter = 1

    #Loop through each site collection
    ForEach($Site in $SiteCollections)
    {
        Try {
            Write-host "Processing Site ($Counter of $($SiteCollections.count):" $Site.URL -f Yellow
            #Connect to the site
            Connect-PnPOnline -Url $Site.URL -Interactive
 
            #Get Recycle bin Item Sizes
            $FirstStageSize = Get-PnPRecycleBinItem -RowLimit 500000 -FirstStage | Measure-Object -Property Size -Sum
            $SecondStageSize = Get-PnPRecycleBinItem -RowLimit 500000 -SecondStage | Measure-Object -Property Size -Sum
            $FirstStageSizeInMB = [Math]::Round($FirstStageSize.Sum/1MB,2)
            $SecondStageSizeInMB = [Math]::Round($SecondStageSize.Sum/1MB,2)
            $TotalSize = $FirstStageSizeInMB+$SecondStageSizeInMB

            #Collect Data
            $RecyclebinData+= [PSCustomObject][ordered]@{
                SiteName         = $Site.Title
                URL              = $Site.URL
                FirstStageSize   = $FirstStageSizeInMB
                SecondStageSize  = $SecondStageSizeInMB
                TotalSize        = $TotalSize
            }
            $Counter++
        }
        Catch {
            write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
        }
    }

    #Export the data to CSV
    $RecyclebinData | Format-Table
    $RecyclebinData | Export-CSV -Path $OutputPath -NoTypeInformation
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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

取消回复欢迎 发表评论:

关灯