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

[玩转系统] SharePoint Online:使用 PowerShell 获取文件大小

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

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


要求:使用 PowerShell 获取 SharePoint Online 中的文件大小。

如何获取 SharePoint Online 中的文件大小?

您是否曾经需要获取 SharePoint Online 中文件的大小?也许您需要知道 SharePoint Online 文档库中的文件占用了多少存储空间。在这篇博文中,我们将向您展示如何使用 PowerShell 获取文件或文件夹的大小。此外,我们将探索通过几个简单的步骤获取文件大小信息。让我们开始吧!

要获取文件的大小,您可以按照以下方法操作:

  • 选项 1:编辑文档库的列表视图并在其中包含“文件大小”列
  • 选项2:进入“存储指标”页面,您可以获取文件的大小。
  • 选项 3:您可以在资源管理器视图中打开库并从文件属性中获取文件大小。
  • 选项 4:您可以在 SharePoint Designer 中打开网站以获取文件的大小。

    [玩转系统] SharePoint Online:使用 PowerShell 获取文件大小

PowerShell 获取 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"
 
Function Get-SPOFileSize($SiteURL,$FileRelativeURL)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
 
        #Get the File
        $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()
        $FileSize = [Math]::Round($File.Length/1KB,2)

        #Get File Size
        Write-host "File Size (KB):" $FileSize
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Call the Function to get file size with Site URL and File URL
Get-SPOFileSize -SiteURL "https://crescent.sharepoint.com/sites/marketing" -FileRelativeURL "/sites/marketing/Branding/Technical Design.docx"

要获取文件的总大小及其版本,请使用:


#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"

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
$FileRelativeURL =  "/sites/marketing/Branding/Technical Design.docx"
 
#Setup Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)
    $Ctx.Load($File.ListItemAllFields)
    $Ctx.ExecuteQuery()
    $TotalFileSize = [Math]::Round($File.ListItemAllFields.FieldValues.SMTotalSize.LookupId/1KB,2)

    #Get File Size
    Write-host "Total File Size (KB):" $TotalFileSize
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

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

要使用 PnP PowerShell 获取文件大小,


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
$FileRelativeURL =  "/sites/marketing/Branding/Technical Design.docx"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the File as List Item
$File = Get-PnPFile -Url $FileRelativeURL -AsListItem
 
#Get File Size
Write-host "File Size:"$File["File_x0020_Size"]

如果您想分析所有文档的文件大小,请使用以下 PowerShell 脚本。

获取 SharePoint Online 文档库中所有文档的文件大小
让我们获取文档库中每个文件的大小,并使用 PowerShell 将数据导出到 CSV 文件:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName= "Branding"
$ReportOutput = "C:\Temp\FileSizeRpt.csv"
  
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive

#Array to store results
$Results = @()
  
#Get all Items from the document library
$List  = Get-PnPList -Identity $ListName
$ListItems = Get-PnPListItem -List $ListName -PageSize 500 | Where {$_.FileSystemObjectType -eq "File"}
Write-host "Total Number of Items in the List:"$List.ItemCount

$ItemCounter = 0 
#Iterate through each item
Foreach ($Item in $ListItems)
{
        $Results += New-Object PSObject -Property ([ordered]@{
            FileName          = $Item.FieldValues.FileLeafRef
            RelativeURL       = $Item.FieldValues.FileRef
            FileSize          = $Item.FieldValues.File_x0020_Size
            TotalFileSize     = $Item.FieldValues.SMTotalSize.LookupId
        })
    $ItemCounter++
    Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting data from Item '$($Item['FileLeafRef'])"
}
 
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
Write-host "File Size Report Exported to CSV Successfully!"

这是关于将文件大小列添加到 SharePoint Online 文档库列表视图的另一篇文章:如何在 SharePoint Online 中添加文件大小列?

总之,使用 PowerShell 可以轻松获取 SharePoint Online 中特定文件或多个文件的文件大小。此外,您还可以将此信息导出到 CSV 文件以进行进一步分析和报告。

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

取消回复欢迎 发表评论:

关灯