首页 > 解决方案 > 将 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 

标签: powershell

解决方案


所以,我没有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".
...
#>

推荐阅读