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'=@{Parametervalue = '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
    )

    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
        }
        
        # Append [Exception] to the STIG title
        $ruleToOverride.title = "[Exception]" + $ruleToOverride.title
        # select and Update the property to override
        $propertiesToOverride = $exception.Value
        foreach ($property in $propertiesToOverride.GetEnumerator())
        {
            $propertyToOverride = $property.Name
            $ruleToOverride.$propertyToOverride = $property.Value.ToString()
        }
    }
}

Export-ModuleMember -Function 'Merge-StigExceptions'