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.
    .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" }
    $colorPaletteFile = $themes[-1]["FileRef"]
    $colorPaletteUrl = $colorPaletteFile -replace '/sites/[^/]*', ""
    Add-PnPFileToSiteTemplate -Path $path -SourceUrl $colorPaletteFile -Connection $srcConn
    return $colorPaletteUrl
}