Add-SSPThemeToTemplate.ps1

<#
    .Synopsis
    Adds the current color palette to a PnP Template file.
    .Description
    This CmdLet adds the color palette to a PnP Template File.

    NOTE: This call only seems to be successful for a Template File that is for TeamSiteWithoutMicrosoft365Group.
    .Parameter Path
    This parameter contains path of the PnP Template File.
    .Parameter Connection
    This parameter provides the context for the call. The default is the current connection
    returned by "Get-PnPConnection".
    .Example
        PS> $colorPaletteUrl = Add-SSPThemeToTemplate -Path "/tmp/template.pnp"
    .Example
        PS> $colorPaletteUrl = Add-SSPThemeToTemplate -Path "/tmp/template.pnp" -Connection $srcConn
#>


function Add-SSPThemeToTemplate
{
    param(
        [string] $path,
        $connection = (Get-PnPConnection)
    )
    $themes = Get-PnPListItem -List "Theme Gallery" -Connection $connection | Where-Object { $_["FileRef"] -like "*_catalogs/theme/Themed/*/*.spcolor" }
    $themes = $themes | Sort-Object -Property {$_["Modified"]} -Descending
    $colorPaletteFile = $themes[0]["FileRef"]
    $colorPaletteUrl = $colorPaletteFile -replace '/sites/[^/]*', ""
    Write-Host "Add-SSPThemeToTemplate $colorPaletteFile placed at $colorPaletteUrl"
    Add-PnPFileToSiteTemplate -Path $path -SourceUrl $colorPaletteFile -Connection $srcConn
    return $colorPaletteUrl
}