summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/windows/win_regmerge.ps1
blob: 87e73a69773c01ef21badcd25bbcab986c7c16df (plain)
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
#!powershell
# This file is part of Ansible
#
# Copyright 2015, Jon Hawkesworth (@jhawkesworth) <figs@unity.demon.co.uk>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# WANT_JSON
# POWERSHELL_COMMON

Function Convert-RegistryPath {
    Param (
        [parameter(Mandatory=$True)]
        [ValidateNotNullOrEmpty()]$Path
    )

    $output = $Path -replace "HKLM:", "HKLM"
    $output = $output -replace "HKCU:", "HKCU" 

    Return $output
}

$params = Parse-Args $args
$result = New-Object PSObject
Set-Attr $result "changed" $False

$path = Get-Attr -obj $params -name path -failifempty $True -resultobj $result
$compare_to = Get-Attr -obj $params -name compare_to -failifempty $False -resultobj $result

# check it looks like a reg key, warn if key not present - will happen first time
# only accepting PS-Drive style key names (starting with HKLM etc, not HKEY_LOCAL_MACHINE etc)

$do_comparison = $False

If ($compare_to) {
    $compare_to_key = $params.compare_to.ToString()
    If (Test-Path $compare_to_key -pathType container ) {
        $do_comparison = $True
    } Else {
        Set-Attr $result "compare_to_key_found" $False
    }
}

If ( $do_comparison -eq $True ) {
  $guid = [guid]::NewGuid()
  $exported_path = $env:TEMP + "\" + $guid.ToString() + 'ansible_win_regmerge.reg'

  $expanded_compare_key = Convert-RegistryPath ($compare_to_key) 

  # export from the reg key location to a file
  $reg_args = @("EXPORT", "$expanded_compare_key", $exported_path)
  & reg.exe $reg_args

  # compare the two files
  $comparison_result = Compare-Object -ReferenceObject $(Get-Content $path) -DifferenceObject $(Get-Content $exported_path)

  If (Get-Member -InputObject $comparison_result -Name "count" -MemberType Properties )
  {
     # Something is different, actually do reg merge
     $reg_import_args = @("IMPORT", "$path")
     $ret = & reg.exe $reg_import_args 2>&1
     If ($LASTEXITCODE -eq 0) {
         Set-Attr $result "changed" $True
         Set-Attr $result "difference_count" $comparison_result.count
     } Else {
         Set-Attr $result "rc" $LASTEXITCODE
         Fail-Json $result "$ret"
     }
  } Else {
      Set-Attr $result "difference_count" 0
  }

  Remove-Item $exported_path
  Set-Attr $result "compared" $True

} Else {
     # not comparing, merge and report changed
     $reg_import_args = @("IMPORT", "$path")
     $ret = & reg.exe $reg_import_args 2>&1
     If ( $LASTEXITCODE -eq 0 ) {
         Set-Attr $result "changed" $True
         Set-Attr $result "compared" $False
     } Else {
         Set-Attr $result "rc" $LASTEXITCODE
         Fail-Json $result "$ret"
     }
}

Exit-Json $result