Private/ConvertFrom-HPSLocalSecurityPolicy.ps1

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"
    }
}