0
votes

I am making a PowerShell script to copy content from a document library to a different document library. So far I have the following code

{
    # Define final variables
    $SourceUrlComplete = $SourceUrl + $SourceSite + "/" + $SourceSubSite
    $DestinationUrlComplete = $SourceUrl + "Contracts"

    # Set connections
    Write-host "Start copy process..." -f Yellow
    Write-host "1. Connect to $sourceUrlComplete" -f Yellow
    $SourceConnection = Connect-PnPOnline -Url $SourceUrlComplete -ClientId $ClientId -ClientSecret $ClientSecret -WarningAction Ignore -ReturnConnection
    Write-host "   Connected" -f DarkGreen
    Write-host "2. Connect to $TargetUrlComplete" -f Yellow
    $DestinationConnection = Connect-PnPOnline -Url $DestinationUrlComplete -ClientId $ClientId -ClientSecret $ClientSecret -WarningAction Ignore -ReturnConnection
    Write-host "   Connected" -f DarkGreen

    $SourceList = Get-PnPList -Identity $SourceLibrary -Includes RootFolder -Connection $SourceConnection
    $SourceListUrl = $SourceList.RootFolder.ServerRelativeUrl
    $DestinationList = Get-PnPList -Identity $DesitinationLibrary -Includes RootFolder -Connection $DestinationConnection
    $DestinationListUrl = $DestinationList.RootFolder.ServerRelativeUrl
}

This works, I have connections, I have the list too. I can also use Get-PnpListItem and will get all list items. What I'm now left with is the following:

  1. I need to look through the items and based on a custom column check a folder exists on the target
  2. Copy the file to the target
  3. Unfortunately, the column 'title' and 'name' are not always populated in the source item

Note: the lists are on two different sites. But if the solution is to create a temp list on-site A then copy that to site B please let me know that too.

Can someone advise on the next step? I get stuck with either coping one on one of the whole list or messages saying to use spFileCollection.add().

1

1 Answers

0
votes

So what I've done now is make this function

Function ProcessFiles {
    $SourceUrlComplete = $SourceUrl + $Site + "/" + $SubSite
    $DestinationUrlComplete = $SourceUrl + "Contracts"

    Write-host "Start copy process..." -f Yellow
    Write-host "  1. Connect to $sourceUrlComplete" -f Yellow
    $SourceConnection = Connect-PnPOnline -Url $SourceUrlComplete -ClientId $ClientId -ClientSecret $ClientSecret -WarningAction Ignore -ReturnConnection
    Write-host "     Connected" -f DarkGreen
    
    Write-host "  2. Connect to $DestinationUrlComplete" -f Yellow
    $DestinationConnection = Connect-PnPOnline -Url $DestinationUrlComplete -ClientId $ClientId -ClientSecret $ClientSecret -WarningAction Ignore -ReturnConnection
    Write-host "     Connected" -f DarkGreen

    Write-host "  3. Process files" -f Yellow 
        $SourceFiles = Get-PnPFolderItem -Connection $SourceConnection -FolderSiteRelativeUrl $SourceList -ItemType File
        $SourceListItems = Get-PnPListItem -List $SourceList -Connection $SourceConnection
        foreach ($SourceFile in $SourceFiles) {
            $process = "yes"
            foreach ($SourceListItem in $SourceListItems)
            {
                if ($SourceListItem["FileRef"] -eq $SourceFile.ServerRelativeUrl -and $process -eq "yes")
                {
                    $process = "no"
                    Write-host "     - $($SourceFile.ServerRelativeUrl)" -f DarkYellow
                
                    Get-PnPFile -Url $SourceFile.ServerRelativeUrl -Path $ProcessFolder -FileName $SourceFile.Name -AsFile -Connection $SourceConnection -Force
                        
                    $SourcePath = $ProcessFolder + "\" + $SourceFile.Name
                    $DestinationFolder = $SourceListItem["CustomerName"]
                    $DestinationItemValues = @{
                        "CustomerName" = $SourceListItem["CustomerName"]
                        "Title" = $SourceListItem["Title"]
                        "CustomerNo" = $SourceListItem["CustomerNo"]
                        "DocType" = $SourceListItem["DocType"]
                        "DocDate" = $SourceListItem["DocDate"]
                        "DocCode" = $SourceListItem["DocCode"]
                        "Modified" = $SourceListItem["Modified"]
                        "Created" = $SourceListItem["Created"]
                        "Visibility" = $SourceListItem["Visibility"]
                        "CompanyID" = $SourceListItem["CompanyID"]
                    }

                    $DestinationFolder = "Contracts"
                    if ($SourceSubSite -eq "BE")
                    {
                        $DestinationFolder = $DestinationFolder + "\SOME_FOLDER\" + $SourceListItem["CustomerName"]

                        Add-PnPFile -Path $SourcePath -Connection $DestinationConnection -Folder "$DestinationFolder" -Values $DestinationItemValues | Out-Null
                        Remove-Item $SourcePath -Recurse
                        Write-host "     - DONE" -f DarkGreen
                    }
                    else
                    {
                        Write-host "     - FAILED" -f Red
                    }
                }
            }
        }
    Write-host "     Done" -f DarkGreen
}

Right now it works, but I download the file temporarily. I need to add something to replace special characters where the new folder is determined. I'm not sure this is the best way currently.