PowerShell Compaire Property Items

On 26/05/2025

In Powershell


$ret1 = "PO-REQ-..2101.docx"
# $ret2 = "PO-RE..-2101.pdf"

$url1 = "https://test.sharepoint.com/sites/test/procedures";

$url2 = "https://test.sharepoint.com/sites/test/procedures"
Clear-Host
function ToPdf {
    param($fileName)

    $paths = $fileName.Split(".")
    if ($paths.Count -eq 2) {
        return "$($paths[0]).pdf"
    }
    if ($paths.Count -gt 2) {
        $ret = "";
        for ($i = 0 ; $i -lt $paths.Count - 1 ; $i++) {
            $ret += "$($paths[$i])" + "."
        }
        $ret += "pdf"
        return $ret
    }
    throw "Error : $($fileName)"
}



$list = "Work Instructions - Sources"
$list2 = "Work Instructions"
$ColumnsToCompaire = "Csc_Domain"
$ColumnRef = ""
$select = "Title,FileLeafRef,Csc_Domain,FileDirRef"

$buildSelet = ""
foreach ($col in ($select -split ",")) {
    $buildSelet += ""
}
$buildSelet += ""
$con1 = Connect-PnPOnline -Url $url1 -ReturnConnection -UseWebLogin
$con2 = Connect-PnPOnline -Url $url2 -ReturnConnection -UseWebLogin

$list1 = Get-PnPList -Identity $list -Connection $con1
Write-Host "list 1 : $($list1.ItemCount)"

$query = "<View Scope='RecursiveAll'><RowLimit>1000</RowLimit><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy><Query><Where><Neq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Neq></Where></Query>$($select)</View>"
$items1 = Get-PnPListItem -List $list -Connection $con1 -Query $query
$items2 = Get-PnPListItem -List $list2 -Connection $con2 -Query $query 

foreach ($item in $items1) {
    if ("$($item[$ColumnsToCompaire] )".Trim().ToLower() -eq "") {
        continue;
    }
    Write-Host ""
    Write-Host "list 1 : $($list1.ItemCount) item $($item.Id) ------------------"
    foreach ($col in ($select -split ",")) {
        Write-Host "item $($item.Id) '$($col)' : '$($item[$col])'"
        "item 1 $($item.Id) '$($col)' : '$($item[$col])'" | Out-File -LiteralPath ".\log\myLog_1.txt" -Encoding utf8 -Append
    }

    #$found = $items2 | Where-Object {$_.FileLeafRef -eq $item["FileLeafRef"]} #) -and $_.FileDirRef -eq $item["FileDirRef"]
    $founds = @();
    
    $toFound = ToPdf -fileName $item["FileLeafRef"]
    foreach ($found in $items2) {
        if ("$($toFound)".Trim().ToLower() -eq "$($found["FileLeafRef"])".Trim().ToLower()) {
            $founds += $found 
        }
    }

    if ($null -eq $founds) {
        Write-Error "File Error : $($item["FileLeafRef"]))" -ForegroundColor Yellow
    }
    elseif ($founds.Count -eq 0) {
        Write-Host "File ne found $($($item["FileLeafRef"])) -> $($toFound)" -ForegroundColor Yellow
        "File ne found $($($item["FileLeafRef"])) -> $($toFound)" | Out-File -LiteralPath ".\log\myLog_NotFound.txt" -Encoding utf8 -Append
    }
    elseif ($founds.Count -gt 1) {
        Write-Error "More than 1 file"
    }
    elseif ($founds.Count -eq 1) {
        if ($item[$ColumnsToCompaire] -ne $founds[0][$ColumnsToCompaire]) {
            Write-Host "to update"
            "to update  $($item.Id) '$($toFound)' - origine : '$($item[$ColumnsToCompaire])' - pdf : '$($founds[0][$ColumnsToCompaire])'" | Out-File -LiteralPath ".\log\myLog_2.txt" -Encoding utf8 -Append
            Set-PnPListItem -UpdateType SystemUpdate -Identity $founds[0].Id  -Connection $con2 -List $list2 -Values @{$ColumnsToCompaire = $item[$ColumnsToCompaire] }
            Write-Host "updated"
        }
    }
}

 

powershell Sharepoint

No ratings yet - be the first to rate this.