powershell - 将 Sharepoint List A 内容与 Sharepoint List B 进行比较,并将差异写入 List B
问题描述
我是Powershell脚本的新手,请问您是否可以帮助我编写脚本。
目的是比较具有 100 条带附件记录的 Sharepoint 列表 A 与具有 50 条记录的另一个 Sharepoint 列表 B。
我需要更新列表 B 的差异,包括列表 A 记录中的附件。
$SiteURL = "projects.crescent.com"
Connect-PnPOnline -url $SiteURL –Credentials (Get-Credential)
$ListOneName = "T1"
$ListTwoName = "T2"
#Get site and List objects
$ColumnToCompare = "Change ID #"
#$Web = Get-SPWeb $SiteURL
$ListOne = $web.lists[$ListOneName]
$ListTwo = $web.lists[$ListTwoName]
#Fetch specific Column Values from each list to compare
$ListOneValues = @()
$ListTwoValues = @()
$ListOne.Items |
foreach { $ListOneValues+= $_[$ColumnToCompare] }
$ListTwo.Items |
foreach { $ListTwoValues+= $_[$ColumnToCompare] }
Compare-Object $ListOneValues $ListTwoValues # -PassThru
解决方案
所以,我没有SP可以做到这一点。但是,如前所述,使用文件系统执行此操作可能大致是这样的。
# Get your lists
($InSourcePath = Get-ChildItem -Path 'D:\Temp\Source')
($InDestinationPath = Get-ChildItem -Path 'D:\Temp\Destination')
# Results
<#
Directory: D:\Temp\Source
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
...
Directory: D:\Temp\Destination
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 14-Mar-20 17:03 here
...
#>
# File count
$InSourcePath.Count
$InDestinationPath.Count
# Results
<#
16
4
#>
# Get the root source and destination paths
($SourcePath = (($InSourcePath = Get-ChildItem -Path 'D:\Temp\Source' -File)[0]).DirectoryName)
($DestinationPath = (($InDestinationPath = Get-ChildItem -Path 'D:\Temp\Destination' -File)[0]).DirectoryName)
# Results
<#
D:\Temp\Source
D:\Temp\Destination
#>
# Intial compare
Compare-Object -ReferenceObject $InDestinationPath.Name -DifferenceObject $InSourcePath.Name -IncludeEqual
# Results '==', in both source/destination :::'=>', only in Source ::: '<=', only in destination
<#
InputObject SideIndicator
----------- -------------
5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url ==
abc.txt =>
...
here <=
...
#>
Compare-Object -ReferenceObject $InDestinationPath.Name -DifferenceObject $InSourcePath.Name -IncludeEqual |
ForEach-Object {
If ($PSItem.SideIndicator -eq '=>')
{
Move-Item -Path "$SourcePath\$($PSitem.InputObject)" -Destination $DestinationPath -WhatIf
}
}
# Results
<#
What if: Performing the operation "Move File" on target "Item: D:\Temp\Source\abc.txt Destination: D:\Temp\Destination\abc.txt".
...
#>
推荐阅读
- c++ - Emscripten 在 macOS 上编译 C++:错误“无线程 API”
- intellij-idea - 菜鸟弄清楚 instal4j 和 intellij
- python - 文件 ”
",第 1 行,在 - java - 删除产品后如何从表中删除行?
- c# - 从一个用户定义的类转换到另一个有返回问题的类。(C#)
- javascript - 如何将函数结果返回给 React 组件?
- javascript - 创建一个虚拟进度条
- r - 避免与 kable 重叠列
- webpack - Webpack 5 Node Polyfill 指令不起作用
- python - Python:从对应于给定列表的字典中选择键、值