[玩转系统] SharePoint Online:使用 PowerShell 的版本历史记录报告
作者:精品下载站 日期:2024-12-14 21:04:47 浏览:14 分类:玩电脑
SharePoint Online:使用 PowerShell 的版本历史记录报告
要求:为 SharePoint 网站集中的所有网站生成版本历史记录报告。
SharePoint Online PowerShell 用于获取文档库的版本历史记录详细信息
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"
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$LibraryName="Branding"
$ReportOutput = "C:\Temp\VersionHistoryRpt.csv"
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the web & Library
$Web=$Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($LibraryName)
$Ctx.ExecuteQuery()
#Query to Batch process Items from the document library
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query><RowLimit>2000</RowLimit></View>"
Do {
$ListItems=$List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
$VersionHistoryData = @()
#Iterate throgh each file - Excluding Folder Objects
Foreach ($Item in $ListItems | Where { $_.FileSystemObjectType -eq "File"})
{
$File = $Web.GetFileByServerRelativeUrl($Item["FileRef"])
$Ctx.Load($File)
$Ctx.Load($File.ListItemAllFields)
$Ctx.Load($File.Versions)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Processing File:"$File.Name
If($File.Versions.Count -ge 1)
{
#Calculate Version Size
$VersionSize = $File.Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
If($Web.ServerRelativeUrl -eq "/")
{
$FileURL = $("{0}{1}" -f $Web.Url, $File.ServerRelativeUrl)
}
Else
{
$FileURL = $("{0}{1}" -f $Web.Url.Replace($Web.ServerRelativeUrl,''), $File.ServerRelativeUrl)
}
#Send Data to object array
$VersionHistoryData += New-Object PSObject -Property @{
'File Name' = $File.Name
'Versions Count' = $File.Versions.count
'File Size' = ($File.Length/1KB)
'Version Size' = ($VersionSize/1KB)
'URL' = $FileURL
}
}
}
} While ($Query.ListItemCollectionPosition -ne $null)
#Export the data to CSV
$VersionHistoryData | Export-Csv $ReportOutput -NoTypeInformation
Write-host -f Green "Versioning History Report has been Generated Successfully!"
}
Catch {
write-host -f Red "Error Generating Version History Report!" $_.Exception.Message
}
PowerShell 为 SharePoint Online 网站集生成版本历史记录报告:
下面是用于获取 SharePoint Online 网站集中文档库中所有文件的版本历史记录的 PowerShell。
#Import SharePoint Online module
Import-Module Microsoft.Online.SharePoint.Powershell
Function Generate-VersionHistoryReport()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ReportOutput
)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get all subsites and Lists from the site
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($Web.Webs)
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Processing Site: "$SiteURL
#Exclude system lists
$ExcludedLists = @("Access Requests","App Packages","appdata","appfiles","Apps in Testing","Cache Profiles","Composed Looks","Content and Structure Reports","Content type publishing error log","Converted Forms",
"Device Channels","Form Templates","fpdatasources","Get started with Apps for Office and SharePoint","List Template Gallery", "Long Running Operation Status","Maintenance Log Library", ,"Master Docs","Master Page Gallery"
"MicroFeed","NintexFormXml","Quick Deploy Items","Relationships List","Reusable Content","Reporting Metadata", "Reporting Templates", "Search Config List","Site Assets", "Site Pages", "Solution Gallery",
"Style Library","Suggested Content Browser Locations","Theme Gallery", "TaxonomyHiddenList","User Information List","Web Part Gallery","wfpub","wfsvc","Workflow History","Workflow Tasks")
#Iterate through each list in a site and get versioning configuration
ForEach($List in $Lists)
{
if(($ExcludedLists -NotContains $List.Title) -and ($List.EnableVersioning) -and ($List.BaseType -eq "DocumentLibrary"))
{
Write-Host "`tProcessing Library:"$List.Title
#Query to Batch process Items from the document library
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query><RowLimit>2000</RowLimit></View>"
$VersionHistoryData = @()
Do {
$ListItems=$List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Iterate throgh each version of file
$VersionHistoryData = @()
#Iterate throgh each file - Excluding Folder Objects
Foreach ($Item in $ListItems | Where { $_.FileSystemObjectType -eq "File"})
{
$File = $web.GetFileByServerRelativeUrl($Item["FileRef"])
$Ctx.Load($File)
$Ctx.Load($File.Versions)
$Ctx.ExecuteQuery()
If($File.Versions.Count -ge 1)
{
$VersionSize=0
#Calculate Version Size
Foreach ($Version in $File.Versions)
{
$VersionSize = $VersionSize + $Version.Size
}
#Send Data to object array
$VersionHistoryData += New-Object PSObject -Property @{
'Site' = $SiteURL
'Library' = $List.Title
'File Name' = $File.Name
'Version Count' = $File.Versions.count
'Version Size-KB' = ($VersionSize/1024)
'URL' = $SiteURL+$File.ServerRelativeUrl
}
}
}
} While ($Query.ListItemCollectionPosition -ne $null)
#Export the data to CSV
$VersionHistoryData | Export-Csv $ReportOutput -Append -NoTypeInformation
}
}
#Iterate through each subsite in the current web
Foreach ($Subweb in $Web.Webs)
{
#Call the function recursively to process all subsites underneaththe current web
Generate-VersionHistoryReport -SiteURL $Subweb.URL -ReportOutput $ReportOutput
}
}
Catch {
write-host -f Red "Error Generating version History Report!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$ReportOutput="C:\Temp\VersionHistoryRpt.csv"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Delete the Output report file if exists
If (Test-Path $ReportOutput) { Remove-Item $ReportOutput }
#Call the function to generate version History Report
Generate-VersionHistoryReport -SiteURL $SiteURL -ReportOutput $ReportOutput
该脚本生成一个 CSV 文件,其中包含以下数据:
- 地点
- 图书馆
- 文件名
- 版本数量
- 版本历史 大小
- 文档的网址
如果要导出单个版本详细信息,例如版本标签、创建者、注释等,请使用:SharePoint Online:使用 PowerShell 将列表版本历史记录导出到 Excel
PnP PowerShell 获取文档库的版本历史报告
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$LibraryName = "Documents"
$CSVPath = "C:\Temp\VersionHistoryRpt.csv"
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
$VersionHistoryData = @()
#Iterate through all files
Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_.FieldValues.FileLeafRef -like "*.*"} | ForEach-Object {
Write-host "Getting Versioning Data of the File:"$_.FieldValues.FileRef
#Get FileSize & version Size
$FileSizeinKB = [Math]::Round(($_.FieldValues.File_x0020_Size/1KB),2)
$File = Get-PnPProperty -ClientObject $_ -Property File
$Versions = Get-PnPProperty -ClientObject $File -Property Versions
$VersionSize = $Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
$VersionSizeinKB = [Math]::Round(($VersionSize/1KB),2)
$TotalFileSizeKB = [Math]::Round(($FileSizeinKB + $VersionSizeinKB),2)
#Extract Version History data
$VersionHistoryData+=New-Object PSObject -Property ([Ordered]@{
"File Name" = $_.FieldValues.FileLeafRef
"File URL" = $_.FieldValues.FileRef
"Versions" = $Versions.Count
"File Size (KB)" = $FileSizeinKB
"Version Size (KB)" = $VersionSizeinKB
"Total File Size (KB)" = $TotalFileSizeKB
})
}
$VersionHistoryData | Format-table -AutoSize
$VersionHistoryData | Export-Csv -Path $CSVPath -NoTypeInformation
脚本输出:
同样,要生成站点上所有库的版本历史记录分析,请使用以下 PowerShell 脚本:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/vendors"
$CSVFile = "C:\Temp\VersionHistoryRpt.csv"
#Delete the Output report file if exists
If (Test-Path $CSVFile) { Remove-Item $CSVFile }
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Document Libraries from the Web - Exclude Hidden and certain lists
$ExcludedLists = @("Form Templates", "Preservation Hold Library","Site Assets", "Pages", "Site Pages", "Images",
"Site Collection Documents", "Site Collection Images","Style Library")
$Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists -and $_.BaseType -eq "DocumentLibrary"}
#Iterate through all files from all document libraries
ForEach($List in $Lists)
{
$global:counter = 0
$Files = Get-PnPListItem -List $List -PageSize 2000 -Fields File_x0020_Size, FileRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Files of '$($List.Title)'" -Status "Processing Files $global:Counter to $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
$VersionHistoryData = @()
$Files | ForEach-Object {
Write-host "Getting Versioning Data of the File:"$_.FieldValues.FileRef
#Get File Size and version Size
$FileSizeinKB = [Math]::Round(($_.FieldValues.File_x0020_Size/1KB),2)
$File = Get-PnPProperty -ClientObject $_ -Property File
$Versions = Get-PnPProperty -ClientObject $File -Property Versions
$VersionSize = $Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
$VersionSizeinKB = [Math]::Round(($VersionSize/1KB),2)
$TotalFileSizeKB = [Math]::Round(($FileSizeinKB + $VersionSizeinKB),2)
#Extract Version History data
$VersionHistoryData+=New-Object PSObject -Property ([Ordered]@{
"Library Name" = $List.Title
"File Name" = $_.FieldValues.FileLeafRef
"File URL" = $_.FieldValues.FileRef
"Versions" = $Versions.Count
"File Size (KB)" = $FileSizeinKB
"Version Size (KB)" = $VersionSizeinKB
"Total File Size (KB)" = $TotalFileSizeKB
})
}
$VersionHistoryData | Export-Csv -Path $CSVFile -NoTypeInformation -Append
}
使用 PowerShell 生成 SharePoint Online 版本历史记录报告可以提供有关网站内容的宝贵见解。按照本文提供的脚本,您可以轻松生成 SharePoint Online 中的列表或库的版本历史记录报告,并将其导出到 CSV 文件以进行分析。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[软件合集] 25年5月28日 精选软件37个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag