2015-10-27 06:13:42 +00:00
|
|
|
#!powershell
|
|
|
|
|
2018-07-17 21:29:05 +00:00
|
|
|
# Copyright: (c) 2015, Jon Hawkesworth (@jhawkesworth) <figs@unity.demon.co.uk>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
2018-10-03 05:28:14 +00:00
|
|
|
#Requires -Module Ansible.ModuleUtils.ArgvParser
|
|
|
|
#Requires -Module Ansible.ModuleUtils.CommandUtil
|
2018-07-17 21:29:05 +00:00
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
2015-10-27 06:13:42 +00:00
|
|
|
|
|
|
|
Function Convert-RegistryPath {
|
|
|
|
Param (
|
|
|
|
[parameter(Mandatory=$True)]
|
|
|
|
[ValidateNotNullOrEmpty()]$Path
|
|
|
|
)
|
|
|
|
|
|
|
|
$output = $Path -replace "HKLM:", "HKLM"
|
|
|
|
$output = $output -replace "HKCU:", "HKCU"
|
|
|
|
|
|
|
|
Return $output
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:41:59 +00:00
|
|
|
$result = @{
|
|
|
|
changed = $false
|
|
|
|
}
|
2015-10-27 06:13:42 +00:00
|
|
|
$params = Parse-Args $args
|
|
|
|
|
2017-02-16 12:41:59 +00:00
|
|
|
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true -resultobj $result
|
|
|
|
$compare_to = Get-AnsibleParam -obj $params -name "compare_to" -type "str" -resultobj $result
|
2015-10-27 06:13:42 +00:00
|
|
|
|
|
|
|
# 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 {
|
2017-02-16 12:41:59 +00:00
|
|
|
$result.compare_to_key_found = $false
|
2015-10-27 06:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-10-03 05:28:14 +00:00
|
|
|
$reg_args = Argv-ToString -Arguments @("reg.exe", "EXPORT", $expanded_compare_key, $exported_path)
|
|
|
|
$res = Run-Command -command $reg_args
|
|
|
|
if ($res.rc -ne 0) {
|
|
|
|
$result.rc = $res.rc
|
|
|
|
$result.stdout = $res.stdout
|
|
|
|
$result.stderr = $res.stderr
|
|
|
|
Fail-Json -obj $result -message "error exporting registry '$expanded_compare_key' to '$exported_path'"
|
|
|
|
}
|
2015-10-27 06:13:42 +00:00
|
|
|
|
|
|
|
# compare the two files
|
|
|
|
$comparison_result = Compare-Object -ReferenceObject $(Get-Content $path) -DifferenceObject $(Get-Content $exported_path)
|
|
|
|
|
2018-10-03 05:28:14 +00:00
|
|
|
If ($null -ne $comparison_result -and (Get-Member -InputObject $comparison_result -Name "count" -MemberType Properties ))
|
2015-10-27 06:13:42 +00:00
|
|
|
{
|
|
|
|
# Something is different, actually do reg merge
|
2018-10-03 05:28:14 +00:00
|
|
|
$reg_import_args = Argv-ToString -Arguments @("reg.exe", "IMPORT", $path)
|
|
|
|
$res = Run-Command -command $reg_import_args
|
|
|
|
if ($res.rc -ne 0) {
|
|
|
|
$result.rc = $res.rc
|
|
|
|
$result.stdout = $res.stdout
|
|
|
|
$result.stderr = $res.stderr
|
|
|
|
Fail-Json -obj $result -message "error importing registry values from '$path'"
|
2016-06-10 17:24:43 +00:00
|
|
|
}
|
2018-10-03 05:28:14 +00:00
|
|
|
$result.changed = $true
|
|
|
|
$result.difference_count = $comparison_result.count
|
2015-10-27 06:13:42 +00:00
|
|
|
} Else {
|
2017-02-16 12:41:59 +00:00
|
|
|
$result.difference_count = 0
|
2015-10-27 06:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Remove-Item $exported_path
|
2017-02-16 12:41:59 +00:00
|
|
|
$result.compared = $true
|
2015-10-27 06:13:42 +00:00
|
|
|
|
|
|
|
} Else {
|
|
|
|
# not comparing, merge and report changed
|
2018-10-03 05:28:14 +00:00
|
|
|
$reg_import_args = Argv-ToString -Arguments @("reg.exe", "IMPORT", $path)
|
|
|
|
$res = Run-Command -command $reg_import_args
|
|
|
|
if ($res.rc -ne 0) {
|
|
|
|
$result.rc = $res.rc
|
|
|
|
$result.stdout = $res.stdout
|
|
|
|
$result.stderr = $res.stderr
|
|
|
|
Fail-Json -obj $result -message "error importing registry value from '$path'"
|
2016-06-10 17:24:43 +00:00
|
|
|
}
|
2018-10-03 05:28:14 +00:00
|
|
|
$result.changed = $true
|
|
|
|
$result.compared = $false
|
2015-10-27 06:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Exit-Json $result
|