Private/ConvertFrom-HPSLocalSecurityPolicy.ps1

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Function ConvertFrom-HPSLocalSecurityPolicy {

    <#
        .SYNOPSIS
        n/a

        .DESCRIPTION
        n/a

        .PARAMETER Path
        n/a

        .PARAMETER ConfigData
        n/a

        .EXAMPLE
        ConvertFrom-HPSLocalSecurityPolicy -Path "C:\Windows\Temp\cc20c224-85bb-4008-b3dd-25d732fa7d98.inf"

        .INPUTS
        System.String

        .INPUTS
        System.Object

        .OUTPUTS
        System.Object

        .LINK
        https://hardening.thomas-illiet.fr/Private/ConvertFrom-HPSLocalSecurityPolicy/

        .LINK
        https://github.com/thomas-illiet/Hardening/blob/master/Hardening/Private/ConvertFrom-HPSLocalSecurityPolicy.ps1

        .LINK
        https://msdn.microsoft.com/en-us/library/cc761131.aspx

        .NOTES
        - File Name : ConvertFrom-HPSLocalSecurityPolicy.ps1
        - Author : Thomas ILLIET
    #>


    [CmdletBinding( HelpUri = "https://hardening.thomas-illiet.fr/Private/ConvertFrom-HPSLocalSecurityPolicy/" )]
    [OutputType( [System.Object] )]
    Param(
        [Parameter(Mandatory = $True)]
        [ValidateNotNullOrEmpty()]
        [ValidateScript({(Test-Path $_)})]
        [System.String]
        $Path,

        [Parameter(Mandatory = $True)]
        [ValidateNotNullOrEmpty()]
        [System.Object]
        $ConfigData
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    process {
        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        # + Gets the content of an INI file and returns it as a hashtable
        $PolicyConfiguration = ConvertFrom-HPSSetupInformation -Path $Path

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        # + Create security policy template object
        $SecurityPolicyTpl = New-Object -TypeName PSObject
        $SecurityPolicyTpl | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Null
        $SecurityPolicyTpl | Add-Member -MemberType NoteProperty -Name 'Key' -Value $Null
        $SecurityPolicyTpl | Add-Member -MemberType NoteProperty -Name 'Value' -Value $Null
        $SecurityPolicyTpl | Add-Member -MemberType NoteProperty -Name 'InputType' -Value $Null
        foreach ( $Preset in $ConfigData.Preset.GetEnumerator() ) {
            $SecurityPolicyTpl | Add-Member -MemberType NoteProperty -Name $Preset.Name -Value $Null
        }
        $SecurityPolicyTpl | Add-Member -MemberType NoteProperty -Name 'Score' -Value $Null

        # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        # +
        foreach ( $Configuration in $ConfigData.Data.GetEnumerator() ) {

            # Create a new instance of SecurityPolicy PsObject
            $SecurityPolicy = $SecurityPolicyTpl.PsObject.Copy()

            # Add current configuration
            $SecurityPolicy.Name = [System.String]$Configuration.Name
            $SecurityPolicy.Key = [System.String]$Configuration.Value.Key
            $SecurityPolicy.InputType = [System.String]$Configuration.Value.InputType
            $SecurityPolicy.Score = [System.Int32]$Configuration.Value.Score

            # Add Data is really defined
            if ( $PolicyConfiguration.ContainsKey( $Configuration.Value.Section ) ) {
                if( ( [string]::IsNullOrEmpty($PolicyConfiguration[$Configuration.Value.Section][$Configuration.Value.Key].Data) ) -eq $False ) {
                    $SecurityPolicy.Value = $PolicyConfiguration[$Configuration.Value.Section][$Configuration.Value.Key].Data -as $Configuration.Value.InputType
                }
            }

            # Add the value of the preset configuration
            foreach ( $Preset in $ConfigData.Preset.GetEnumerator() ) {
                $SecurityPolicy.($Preset.Name) = $Preset.Value.($Configuration.Name)
            }

            # Return current object
            $SecurityPolicy
        }
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }
}