1 #!powershell
2
3 # Copyright: (c) 2015, Jon Hawkesworth (@jhawkesworth) <figs@unity.demon.co.uk>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #Requires -Module Ansible.ModuleUtils.ArgvParser
7 #Requires -Module Ansible.ModuleUtils.CommandUtil
8 #Requires -Module Ansible.ModuleUtils.Legacy
9
Convert-RegistryPathnull10 Function Convert-RegistryPath {
11 Param (
12 [parameter(Mandatory=$True)]
13 [ValidateNotNullOrEmpty()]$Path
14 )
15
16 $output = $Path -replace "HKLM:", "HKLM"
17 $output = $output -replace "HKCU:", "HKCU"
18
19 Return $output
20 }
21
22 $result = @{
23 changed = $false
24 }
25 $params = Parse-Args $args
26
27 $path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -resultobj $result
28 $compare_to = Get-AnsibleParam -obj $params -name "compare_to" -type "str" -resultobj $result
29
30 # check it looks like a reg key, warn if key not present - will happen first time
31 # only accepting PS-Drive style key names (starting with HKLM etc, not HKEY_LOCAL_MACHINE etc)
32
33 $do_comparison = $False
34
35 If ($compare_to) {
36 $compare_to_key = $params.compare_to.ToString()
37 If (Test-Path $compare_to_key -pathType container ) {
38 $do_comparison = $True
39 } Else {
40 $result.compare_to_key_found = $false
41 }
42 }
43
44 If ( $do_comparison -eq $True ) {
45 $guid = [guid]::NewGuid()
46 $exported_path = $env:TEMP + "\" + $guid.ToString() + 'ansible_win_regmerge.reg'
47
48 $expanded_compare_key = Convert-RegistryPath ($compare_to_key)
49
50 # export from the reg key location to a file
51 $reg_args = Argv-ToString -Arguments @("reg.exe", "EXPORT", $expanded_compare_key, $exported_path)
52 $res = Run-Command -command $reg_args
53 if ($res.rc -ne 0) {
54 $result.rc = $res.rc
55 $result.stdout = $res.stdout
56 $result.stderr = $res.stderr
57 Fail-Json -obj $result -message "error exporting registry '$expanded_compare_key' to '$exported_path'"
58 }
59
60 # compare the two files
61 $comparison_result = Compare-Object -ReferenceObject $(Get-Content $path) -DifferenceObject $(Get-Content $exported_path)
62
63 If ($null -ne $comparison_result -and (Get-Member -InputObject $comparison_result -Name "count" -MemberType Properties ))
64 {
65 # Something is different, actually do reg merge
66 $reg_import_args = Argv-ToString -Arguments @("reg.exe", "IMPORT", $path)
67 $res = Run-Command -command $reg_import_args
68 if ($res.rc -ne 0) {
69 $result.rc = $res.rc
70 $result.stdout = $res.stdout
71 $result.stderr = $res.stderr
72 Fail-Json -obj $result -message "error importing registry values from '$path'"
73 }
74 $result.changed = $true
75 $result.difference_count = $comparison_result.count
76 } Else {
77 $result.difference_count = 0
78 }
79
80 Remove-Item $exported_path
81 $result.compared = $true
82
83 } Else {
84 # not comparing, merge and report changed
85 $reg_import_args = Argv-ToString -Arguments @("reg.exe", "IMPORT", $path)
86 $res = Run-Command -command $reg_import_args
87 if ($res.rc -ne 0) {
88 $result.rc = $res.rc
89 $result.stdout = $res.stdout
90 $result.stderr = $res.stderr
91 Fail-Json -obj $result -message "error importing registry value from '$path'"
92 }
93 $result.changed = $true
94 $result.compared = $false
95 }
96
97 Exit-Json $result
98