比较Sharepoint List A和Sharepoint List B的内容,并将差异写到List B中

  • 本文关键字:List Sharepoint 比较 powershell
  • 更新时间 :
  • 英文 :


我是一个新手在Powershell脚本,我想问你是否可以帮我做一个脚本。

目标是比较有100条带附件的Sharepoint List A和有50条记录的Sharepoint List 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:TempSource')
($InDestinationPath = Get-ChildItem -Path 'D:TempDestination')
# Results
<#
Directory: D:TempSource

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:TempDestination

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:TempSource' -File)[0]).DirectoryName)
($DestinationPath = (($InDestinationPath = Get-ChildItem -Path 'D:TempDestination' -File)[0]).DirectoryName)
# Results
<#
D:TempSource
D:TempDestination
#>

# 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:TempSourceabc.txt Destination: D:TempDestinationabc.txt".
...
#>

最新更新