[玩转系统] SharePoint Online:使用 PowerShell 将列表版本历史记录导出到 Excel
作者:精品下载站 日期:2024-12-14 14:50:11 浏览:14 分类:玩电脑
SharePoint Online:使用 PowerShell 将列表版本历史记录导出到 Excel
要求:将所有版本的 SharePoint Online 列表项导出到 CSV (Excel) 文件。
PowerShell 将列表项的版本历史提取并导出到 CSV 文件
作为管理员,有时您可能需要将 SharePoint Online 列表版本历史记录导出到 Excel 以便进一步分析。我们可以使用 PowerShell 将列表版本历史记录导出到 Excel。本文假设您有一个现有的 SharePoint Online 列表,其中包含下面给出的列,并且您可能需要根据您的要求创建和更改列。本文将引导您完成使用 PowerShell 将 SharePoint Online 列表版本历史记录导出到 Excel 的步骤。
#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 Export-VersionHistory()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $CSVFile
)
Try {
#Delete the Output report file if exists
if (Test-Path $CSVFile) { Remove-Item $CSVFile }
#Get Credentials to connect
$Cred= Get-Credential
#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 List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Define Query to get List Items in batch
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>2000</RowLimit></View>"
#Batch process list items - to mitigate list threshold issue on larger lists
Do {
#Get items from the list in batches
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Array to hold result
$VersionHistoryData = @()
#Iterate throgh each item
Foreach ($Item in $ListItems)
{
write-host "Processing Item:" $item.id -f Yellow
#Get all versions of the list item
$Versions = $Item.versions
$ctx.Load($Versions)
$Ctx.ExecuteQuery()
If($Versions.count -gt 0)
{
#Iterate each version
Foreach($Version in $Versions)
{
#Get the Creator object of the version
$CreatedBy = $Version.createdby
$Ctx.Load($CreatedBy)
$Ctx.ExecuteQuery()
#Send Data to object array
$VersionHistoryData += New-Object PSObject -Property @{
'Item ID' = $Item.ID
'Title' = $Version.FieldValues["Title"]
'Version Label' = $Version.VersionLabel
'Version ID' = ($Version.VersionId/512)
'Created On' = (Get-Date ($Version.Created) -Format "yyyy-MM-dd/HH:mm:ss")
'Created By' = $CreatedBy.Email
}
}
}
}
}While($Query.ListItemCollectionPosition -ne $null)
#Export the data to CSV
$VersionHistoryData | Export-Csv $CSVFile -Append -NoTypeInformation
Write-host -f Green "Version History Exported Successfully to:" $CSVFile
}
Catch {
write-host -f Red "Error Exporting version History to CSV!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CSVFile="C:\Temp\VersionHistory.csv"
#Call the function to generate version History Report
Export-VersionHistory -SiteURL $SiteURL -ListName $ListName -CSVFile $CSVFile
我在脚本中使用了“标题”字段值。您可以使用以下命令添加任何其他列:$version.FieldValues[“ColumnInternalName”]
重要:此脚本仅适用于 CSOM 版本 16.1.6906.1200 或更高版本!从 https://www.microsoft.com/en-us/download/confirmation.aspx?id=42038 下载最新版本的 CSOM SDK该脚本生成一个包含版本数据的 CSV 文件,例如:
- 版本标签,
- 版本号
- 版本创建时
- 谁创建了版本
- 版本的标题字段值(您也可以添加任何其他字段!)
对于本地 SharePoint,请使用以下脚本:如何使用 PowerShell 将 SharePoint 版本历史记录导出到 CSV?
PnP PowerShell 导出列表中所有项目的版本历史记录
让我们使用 PnP PowerShell 将列表中项目的所有早期版本导出到 CSV 文件。
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$CSVPath = "C:\Temp\ProjectsVersionsRpt.csv"
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Fields from List
$ListFields = Get-PnPField -List $ListName | Where {!$_.Hidden}
$VersionHistoryData = @()
#Get All List Items - Exclude Folders
Get-PnPListItem -List $ListName -PageSize 500 | Where {$_.FileSystemObjectType -ne "Folder"} | ForEach-Object {
Write-host "Getting Versioning Data of Item:"$_.ID
$Versions = Get-PnPProperty -ClientObject $_ -Property Versions
ForEach($Version in $Versions)
{
$VersionHistory = New-Object PSObject
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Version ID" -Value ($Version.VersionId/512)
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Version Label" -Value $Version.VersionLabel
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Created On" -Value (Get-Date ($Version.Created) -Format "yyyy-MM-dd/HH:mm:ss")
#Get Value of all other Fields
$ListFields | ForEach-Object {
#Handle Special Fields
$FieldType = $_.TypeAsString
If($FieldType -eq "User" -or $FieldType -eq "UserMulti" -or $FieldType -eq "Lookup" -or $FieldType -eq "LookupMulti")
{
$FieldValue = $Version[$_.InternalName].LookupValue -join "; "
}
ElseIf($FieldType -eq "URL") #Hyperlink
{
$URL = $Version[$_.InternalName].URL
$Description = $Version[$_.InternalName].Description
$FieldValue = "$URL, $Description"
}
ElseIf($FieldType -eq "TaxonomyFieldType" -or $FieldType -eq "TaxonomyFieldTypeMulti") #MMS
{
$FieldValue = $Version[$_.InternalName].Label -join "; "
}
Else
{
#Get Source Field Value
$FieldValue = $Version[$_.InternalName]
}
#Append Version data
$VersionHistory | Add-Member -MemberType NoteProperty -Name $_.InternalName -Value $FieldValue
}
$VersionHistoryData+=$VersionHistory
}
}
$VersionHistoryData | Export-Csv $CSVPath -NoTypeInformation
不要忘记将变量 SiteURL、ListName 和 CSVPath 替换为与您的环境相关的值。
猜你还喜欢
- 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 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][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
[电视剧] [突围] [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