private/Set-MetadataXmlFile.ps1

function Set-MetadataXmlFile {

    <#
        .SYNOPSIS
        (Re-)Write the given metadata XML file in a normalized way.

        .DESCRIPTION

    #>


    [CmdletBinding()]
    param (
        # metadata type as per metadata API, e.g. 'GlobalValueSet'
        [Parameter(Mandatory, Position=0)]
        [ValidateNotNullOrEmpty()]
        [string] $MetadataType,

        # API name as to be used in the file name
        [Parameter(Mandatory, Position=1)]
        [ValidateNotNullOrEmpty()]
        [string] $MetadataName,

        # file name extension including leading dot, e.g. '.globalValueSet-meta.xml'
        [Parameter(Mandatory, Position=2)]
        [ValidateNotNullOrEmpty()]
        [string] $MetadataFileNameExtension,

        # Name of the target folder/directory WITHOUT the file name.
        # The directory will be created if it does not exist.
        [Parameter(Mandatory, Position=3)]
        [string] $DirectoryPath,

        # A string with the raw xml.
        [Parameter(Mandatory, Position=4)]
        [string] $xmlString,

        # Type of action performed on this file. Just for informational purpose on logging.
        [Parameter()]
        [string] $Action = 'Created',

        [Parameter()]
        [ValidateSet('Information', 'Verbose', 'Debug', 'None')]
        [string] $LogLevel = 'Information',

        [Parameter()]
        [string] $LogMetadataName = $MetadataName
    )

    $ErrorActionPreference = 'Stop'
    [bool] $IsDebug   = $DebugPreference   -ne 'SilentlyContinue'
    [bool] $IsVerbose = $VerbosePreference -ne 'SilentlyContinue'

    # --- (re-)write XML file
    $FilePath = Join-Path $DirectoryPath "$MetadataName$MetadataFileNameExtension"
    Write-Debug "Writing [$FilePath]"
    Set-XmlFile $FilePath $xmlString

    # --- logging
    $LogString = "{0,-25}|{1,-65}|{2,-7}|{3}" -f $MetadataType, $LogMetadataName, $Action, $FilePath
    switch ($LogLevel) {
        'Information'   { Write-Host $LogString }
        'Verbose'       { Write-Verbose $LogString }
        'Debug'         { Write-Debug $LogString }
    }
}