Commands/Join-OpenPackage.ps1

function Join-OpenPackage
{
    <#
    .SYNOPSIS
        Joins Open Packages
    .DESCRIPTION
        Joins multiple open packages into a single open package
    #>

    [Alias('Join-OP','jop','jOpenPackage')]
    [CmdletBinding(PositionalBinding=$false)]
    param(    
    [Parameter(ValueFromPipeline)]
    [PSObject]
    $InputObject,

    <#
    Includes the specified parts.
    
    Enter a wildcard pattern, such as `*.txt`

    Wildcards are permitted.
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $Include,
    
    <#
    Excludes the specified parts.
    
    Enter a wildcard pattern, such as `*.txt`
    
    Wildcards are permitted.
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $Exclude,

    <#
    Includes the specified content types.
    
    Enter a wildcard pattern, such as `text/*`
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $IncludeContentType,
    
    <#
    Excludes the specified content types.
    
    Enter a wildcard pattern, such as `text/*`
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $ExcludeContentType,

    [switch]
    $Force
    )

    begin {
        $memoryStream = [IO.MemoryStream]::new()
        $combinedPackage = [IO.Packaging.Package]::Open($memoryStream, 'OpenOrCreate', 'ReadWrite')
        $copySplat = [Ordered]@{Destination=$combinedPackage}
        $copyOpenPackage = $ExecutionContext.SessionState.InvokeCommand.GetCommand('Copy-OpenPackage','Function')
        foreach ($key in $psBoundParameters.Keys) {
            if ($copyOpenPackage.Parameters[$key]) {
                $copySplat[$key] = $PSBoundParameters[$key]
            }
        }

        $allIdentifiers = @()
    }

    process {
        if ($InputObject -isnot [IO.Packaging.Package]) {
            return $InputObject
        }
        $allIdentifiers += $InputObject.Identifier

        $combinedPackage = $InputObject | Copy-OpenPackage @copySplat
    }   

    end {
        $allUniqueIdentifiers = @($allIdentifiers | Select-Object -Unique)

        $combinedPackage.Identifier = $combinedPackage.Identifier -replace '\.[^\.]+?$'

        $combinedPackage
    }
}