Parsers/CSV/Registry.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
Function Write-RegistryCSVData
{
    [CmdletBinding()]
    [OutputType([String])]
    param
    (
        [Parameter(Mandatory = $true)]
        $RegistryData
    )

    
    $commentOUT = $false  
    switch ($RegistryData.ValueType)
    {
        "REG_DWORD" 
        {
            $ValueType = "DWORD"            
            $ValueData = -1            
            if (!([int]::TryParse($RegistryData.ExpectedValue, [ref]$ValueData)))
            {
                if (($RegistryData.ExpectedValue -match "(Disabled|Enabled|Not Defined|True|False)" -or $RegistryData.ExpectedValue -eq "''"))
                {
                    # This is supposed to be an INT and it's a String
                    [int]$ValueData = @{"Disabled"=0;"Enabled"=1;"Not Defined"=0;"True"=1;"False"=0;''=0}[$RegistryData.ExpectedValue]
                    $ValueType = "DWORD"            
                }
                else
                {
                    Write-Warning "Could not parse Policy ($($RegistryData.Name)) with ExpectedValue ($($RegistryData.ExpectedValue)) as ($($RegistryData.RegValueType))"
                    $CommentOut = $true
                }
            }
        }

        "REG_SZ"
        {
            $ValueData = $RegistryData.ExpectedValue.ToString()
            $ValueType = "String"
        }

        "REG_MULTI_SZ"
        {
            $ValueData = $RegistryData.ExpectedValue.ToString()
            $ValueType = "MultiString"
        }

        Default 
        {
            $ValueType = $null
        }
    }

    switch ($RegistryData.Hive)
    {
        "LocalMachine" { $RegistryData.Hive = "HKLM:" }
    }

    $policyHash = @{}
    $key, $valuename = [string]$RegistryData.DataSourceKey -split ":"
    if ($valuename -eq $null)
    {
        # Try again without the colon
        $valuename = Split-Path -Path ([string]$RegistryData.DataSourceKey) -Leaf
        $key = Split-Path -Path ([string]$RegistryData.DataSourceKey) -Parent
    }

    $policyHash.Key = $([string]$RegistryData.Hive, [string]$key -join "\" )
    $policyHash.ValueName = $valuename
    if ($ValueType -ne $null)
    {
        $policyHash.ValueType = $ValueType
    }
    
    if ($ValueData)
    {
        $policyHash.ValueData = $ValueData
    }
    
    if ($policyHash.ValueType -eq "None")
    {
        # The REG_NONE is not allowed by the Registry resource.
        $policyHash.Remove("ValueType")
    }

    if ([string]::IsNullOrEmpty($policyHash.ValueName))
    {
        $policyHash.Remove("ValueData")
    }

    if ($RegistryData.ExpectedValue -eq $null)
    {
        $policyHash.Remove("ValueData")
    }
              
    If ([string]::IsNullOrEmpty($PolicyHash.Key))
    {
        $CommentOUT = $true
    }
             
    return Write-DSCString -Resource -Type Registry -Name "$($RegistryData.CCEID): $($RegistryData.Name)" -Parameters $policyHash -CommentOUT:$commentOut -DoubleQuoted
}