Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_regmerge.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2015, Jon Hawkesworth (@jhawkesworth) <[email protected]>
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
10 [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSCustomUseLiteralPath', '',
11 Justification = 'This module has supported wildcard comparison since it was created')]
12 param()
13
14 Function Convert-RegistryPath {
15 Param (
16 [parameter(Mandatory = $True)]
17 [ValidateNotNullOrEmpty()]$Path
18 )
19
20 $output = $Path -replace "HKLM:", "HKLM"
21 $output = $output -replace "HKCU:", "HKCU"
22
23 Return $output
24 }
25
26 $result = @{
27 changed = $false
28 }
29 $params = Parse-Args $args
30
31 $path = Get-AnsibleParam -obj $params -name "path" -type "path" -resultobj $result
32 $content = Get-AnsibleParam -obj $params -name "content" -type "str" -resultobj $result
33 $compare_to = Get-AnsibleParam -obj $params -name "compare_to" -type "str" -resultobj $result
34
35 if ( !$path -and !$content ) {
36 Fail-Json -obj $result -message "Missing required arguments: path or content. At lease one must be provided."
37 }
38
39 if ( $path -and $content ) {
40 Fail-Json -obj $result -message "Extra arguments: path or content. Only one must be provided."
41 }
42
43 if ( $content ) {
44 $path = [System.IO.Path]::GetTempFileName()
45
46 Set-Content -LiteralPath $path -Value $content
47
48 $remove_path = $True
49 }
50
51 # check it looks like a reg key, warn if key not present - will happen first time
52 # only accepting PS-Drive style key names (starting with HKLM etc, not HKEY_LOCAL_MACHINE etc)
53
54 $do_comparison = $False
55
56 If ( $compare_to ) {
57 $compare_to_key = $params.compare_to.ToString()
58 If (Test-Path $compare_to_key -PathType container ) {
59 $do_comparison = $True
60 }
61 Else {
62 $result.compare_to_key_found = $false
63 }
64 }
65
66 If ( $do_comparison -eq $True ) {
67 $guid = [guid]::NewGuid()
68 $exported_path = $env:TEMP + "\" + $guid.ToString() + 'ansible_win_regmerge.reg'
69
70 $expanded_compare_key = Convert-RegistryPath ($compare_to_key)
71
72 # export from the reg key location to a file
73 $reg_args = Argv-ToString -Arguments @("reg.exe", "EXPORT", $expanded_compare_key, $exported_path)
74 $res = Run-Command -command $reg_args
75 if ($res.rc -ne 0) {
76 $result.rc = $res.rc
77 $result.stdout = $res.stdout
78 $result.stderr = $res.stderr
79 Fail-Json -obj $result -message "error exporting registry '$expanded_compare_key' to '$exported_path'"
80 }
81
82 # compare the two files
83 $comparison_result = Compare-Object -ReferenceObject $(Get-Content $path) -DifferenceObject $(Get-Content $exported_path)
84
85 If ($null -ne $comparison_result -and (Get-Member -InputObject $comparison_result -Name "count" -MemberType Properties )) {
86 # Something is different, actually do reg merge
87 $reg_import_args = Argv-ToString -Arguments @("reg.exe", "IMPORT", $path)
88 $res = Run-Command -command $reg_import_args
89 if ($res.rc -ne 0) {
90 $result.rc = $res.rc
91 $result.stdout = $res.stdout
92 $result.stderr = $res.stderr
93 Fail-Json -obj $result -message "error importing registry values from '$path'"
94 }
95 $result.changed = $true
96 $result.difference_count = $comparison_result.count
97 }
98 Else {
99 $result.difference_count = 0
100 }
101
102 Remove-Item $exported_path
103 $result.compared = $true
104
105 }
106 Else {
107 # not comparing, merge and report changed
108 $reg_import_args = Argv-ToString -Arguments @("reg.exe", "IMPORT", $path)
109 $res = Run-Command -command $reg_import_args
110 if ($res.rc -ne 0) {
111 $result.rc = $res.rc
112 $result.stdout = $res.stdout
113 $result.stderr = $res.stderr
114 Fail-Json -obj $result -message "error importing registry value from '$path'"
115 }
116 $result.changed = $true
117 $result.compared = $false
118 }
119
120 if ( $remove_path ) {
121 Remove-Item $path
122 }
123
124 Exit-Json $result