private/exceptions.psm1

<#
 .SYNOPSIS
    Convert a hashtable into xml
 
 .DESCRIPTION
    Long description
 
 .PARAMETER StigExceptions
    A hashtable that contains the STIG Id (key) and exception value (value)
 
 .EXAMPLE
 
    ConvertTo-StigXml -StigException @{'V-1090'='1'}
 
 .NOTES
    General notes
#>

function ConvertTo-StigXml
{
    [cmdletbinding()]
    [outputtype([System.XML.XMLDocument])]
    param
    (
        [Parameter(Mandatory)]
        [PsObject]
        $StigExceptions
    )

    # Start the XML doc and add the root element
    [System.XML.XMLDocument] $XmlDocument = New-Object System.XML.XMLDocument
    # Create the root node
    [System.XML.XMLElement] $XmlRoot = $XmlDocument.CreateElement( 'DISASTIG' )
    # Append as child to an existing node. This method will 'leak' an object out of the function
    # so DO NOT remove the [void]
    [void] $XmlDocument.appendChild( $XmlRoot )
    
    foreach ( $StigException in $StigExceptions.GetEnumerator() )
    {
        # Create the rule node
        [System.XML.XMLElement] $XmlRule = $XmlDocument.CreateElement( "Rule" )
        [void] $XmlRoot.appendChild( $XmlRule )
        # Set the base class properties
        $XmlRule.SetAttribute( "Id", $StigException.key )
        $XmlRule.SetAttribute( "Value", $StigException.value )
    }

    $xmlDocument
}

function Merge-StigExceptions
{
    [cmdletbinding()]
    [outputtype([void])]
    param
    (
        [Parameter(Mandatory = $true)]
        [ref] 
        $stigContent,

        [Parameter(Mandatory = $true)]
        [PsObject]
        $StigExceptions,

        [Parameter()]
        [string] 
        $StigTitlePrefix,

        [Parameter()]
        [string] 
        $StigTitleSuffix
    )

    $stigTypeToProperty = Import-PowerShellDataFile -Path $PSScriptRoot\stigTypes.psd1

    Foreach ($exception in $StigExceptions.GetEnumerator())
    {
        # Lookup the STIG Id in the data
        $ruleToOverride = ( $stigContent.value.DISASTIG | 
                        Select-Xml -XPath "//Rule[@Id='$( $exception.Name )']" -ErrorAction Stop ).Node
        
        # If an Id is not found we can continue, but notify the user.
        if ($null -eq $ruleToOverride)
        {
            Write-warning "$($exception.Name) was not found"
            continue
        }
        
        # Get the Parent node which is the STIG type so that we know which property to update.
        $StigType = $ruleToOverride.ParentNode.Name
        # Append [Exception] to the STIG title
        $ruleToOverride.title = "[Exception]" + $ruleToOverride.title
        # seelct and Update the property to override
        $propertyToOverride = $stigTypeToProperty.$StigType
        $ruleToOverride.$propertyToOverride = $Exception.Value.ToString()
    }
}

Export-ModuleMember -Function 'Merge-StigExceptions'