public/Save-SkylineTermSetSchema.ps1

Function Save-SkylineTermSetSchema {
    <#
    .SYNOPSIS
    Extracts the PnP Provisioning Schema XML for the specific term group and term set
     
    .DESCRIPTION
    Extracts the PnP Provisioning Schema XML for the specific term group and term set
     
    Reference: https://github.com/SharePoint/PnP-Provisioning-Schema
 
    .EXAMPLE
    Save-SkylineListProvisioningSchema -TermGroup "Skyline" -TermSet "Departments" -Path "C:\Schemas\Terms.xml"
 
    .PARAMETER TermGroup
    Name of the parent term group
 
    .PARAMETER TermSet
    Name of the term set
 
    .PARAMETER Path
    Path to export the schema to. Example: C:\Schemas\Terms.xml
 
    .PARAMETER Web
    The web to apply the command to. Omit this parameter to use the current web.
    #>


    [cmdletbinding()]
    param(
        [parameter(Mandatory = $True)]
        [string]$TermGroup,
        [parameter(Mandatory = $True)]
        [string]$TermSet, 
        [parameter(Mandatory = $True)]
        [string]$Path,
        [Microsoft.SharePoint.Client.Web]$Web
    )

    Process
    {
        Write-Debug ( "Running $($MyInvocation.MyCommand).`n" + "PSBoundParameters:`n$($PSBoundParameters | Format-List | Out-String)")

        Try
        {     
            $PSBoundParameters.Remove("TermGroup") | Out-Null
            $PSBoundParameters.Remove("TermSet") | Out-Null
            $PSBoundParameters.Remove("Path") | Out-Null

            $Directory = Ensure-SkylineDirectory $Path
            $FileName = Get-SkylineFileName $Path
            $Path = "$Directory\$FileName"
            $TemporaryPath = "$Directory\$FileName.temp.xml"

            Get-PnPProvisioningTemplate -IncludeAllTermGroups -Handlers TermGroups -Out $TemporaryPath -Force @PSBoundParameters

            [XML]$SchemaXml = Get-Content -Path $TemporaryPath

            $TermGroupNodesToRemove = @($SchemaXml.Provisioning.Templates.ProvisioningTemplate.TermGroups.TermGroup | Where-Object {$_.Name -ne $TermGroup})
            $TermGroupNodesToRemove | ForEach-Object {$SchemaXml.Provisioning.Templates.ProvisioningTemplate.TermGroups.RemoveChild($_) | Out-Null}

            if (![string]::IsNullOrWhiteSpace($TermSet)){
                $TermSetNodesToRemove = @($SchemaXml.Provisioning.Templates.ProvisioningTemplate.TermGroups.TermGroup.TermSets.TermSet | Where-Object {$_.Name -ne $TermSet})
                $TermSetNodesToRemove | ForEach-Object {$SchemaXml.Provisioning.Templates.ProvisioningTemplate.TermGroups.TermGroup.TermSets.RemoveChild($_) | Out-Null}
            }

            #Save the remaining XML nodes of the passed -List to -Path
            $SchemaXml.Save($Path)

            Remove-Item -Path $TemporaryPath -Force
        }
        Catch
        {
            Throw $_
        }
    }
}