Module/Rule.FileContent/Convert/FileContentType/FileContentType.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Header

<#
    .SYNOPSIS
        Singleton class to support variable filter and parse methods in FileContentRule
    .DESCRIPTION
        The FileContentType is used to extend filter and parse logic for diiferent
        FileContentRules without modifing existing filtering and parsing logic
    .PARAMETER Instance
        Maintains a single instance of the class object
#>

class FileContentType
{
    static [FileContentType] $Instance
    #region Constructor
    hidden FileContentType () {}

     #region Methods

     <#
        .SYNOPSIS
            Returns an instance of the class
        .DESCRIPTION
            Gets or sets a single instance of the FileContentType
            for use in the FileContentRule
    #>


     static [FileContentType] GetInstance()
     {
         if ([FileContentType]::Instance -eq $null)
         {
             [FileContentType]::Instance = [FileContentType]::new()
         }
         return [FileContentType]::Instance
     }

    <#
        .SYNOPSIS
            Loads and applies specific filtering and parsing rules
        .DESCRIPTION
            When Key-Value settings are located in a rule, the format
            of Key-Value pairs differ between technologies, this method
            supports a unique filter and parsing strategy for the rule
        .PARAMETER matchResult
            The key-value settings from the check-content element in the xccdf
    #>


    [pscustomobject] ProcessMatches ( [psobject] $matchResult )
    {
        $exclude = @($MyInvocation.MyCommand.Name,'Template.*.txt')
        $supportFileList = Get-ChildItem -Path $PSScriptRoot -Exclude $exclude -Recurse -Include "*.$($global:stigXccdfName).*"
        foreach ($supportFile in $supportFileList)
        {
            Write-Verbose "Loading $($supportFile.FullName)"
            . $supportFile.FullName
        }

        $filtered = Get-FilteredItem -MatchResult $matchResult
        if ($filtered)
        {
            return $filtered;
        }
        else
        {
            return $null
        }
    }
}