1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Further fixes to support binary data. Added boolean return values and return documentation.

This commit is contained in:
jhawkesworth@users.noreply.github.com 2016-04-14 21:22:10 +01:00 committed by Matt Clay
parent c2ca0a9e93
commit aaa9541abd
2 changed files with 48 additions and 2 deletions

View file

@ -28,6 +28,8 @@ New-PSDrive -PSProvider registry -Root HKEY_CURRENT_CONFIG -Name HCCC -ErrorActi
$params = Parse-Args $args;
$result = New-Object PSObject;
Set-Attr $result "changed" $false;
Set-Attr $result "data_changed" $false;
Set-Attr $result "data_type_changed" $false;
$registryKey = Get-Attr -obj $params -name "key" -failifempty $true
$registryValue = Get-Attr -obj $params -name "value" -default $null
@ -56,6 +58,31 @@ Function Test-RegistryValueData {
}
}
# Returns rue if registry data matches.
# Handles binary and string registry data
Function Compare-RegistryData {
Param (
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$ReferenceData,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$DifferenceData
)
$refType = $ReferenceData.GetType().Name
if ($refType -eq "String" ) {
if ($ReferenceData -eq $DifferenceData) {
return $true
} else {
return $false
}
} elseif ($refType -eq "Object[]") {
if (@(Compare-Object $ReferenceData $DifferenceData -SyncWindow 0).Length -eq 0) {
return $true
} else {
return $false
}
}
}
# Simplified version of Convert-HexStringToByteArray from
# https://cyber-defense.sans.org/blog/2010/02/11/powershell-byte-array-hex-convert
@ -64,7 +91,7 @@ Function Test-RegistryValueData {
function Convert-RegExportHexStringToByteArray
{
Param (
[parameter(Mandatory=$true))] [String] $String
[parameter(Mandatory=$true)] [String] $String
)
# remove 'hex:' from the front of the string if present
@ -100,6 +127,9 @@ if($state -eq "present") {
{
if (Test-RegistryValueData -Path $registryKey -Value $registryValue)
{
# handle binary data
$currentRegistryData =(Get-ItemProperty -Path $registryKey | Select-Object -ExpandProperty $registryValue)
if ($registryValue.ToLower() -eq "(default)") {
# Special case handling for the key's default property. Because .GetValueKind() doesn't work for the (default) key property
$oldRegistryDataType = "String"
@ -116,6 +146,8 @@ if($state -eq "present") {
Remove-ItemProperty -Path $registryKey -Name $registryValue
New-ItemProperty -Path $registryKey -Name $registryValue -Value $registryData -PropertyType $registryDataType
$result.changed = $true
$result.data_changed = $true
$result.data_type_changed = $true
}
Catch
{
@ -123,11 +155,12 @@ if($state -eq "present") {
}
}
# Changes Only Data
elseif ((Get-ItemProperty -Path $registryKey | Select-Object -ExpandProperty $registryValue) -ne $registryData)
elseif (-Not (Compare-RegistryData -ReferenceData $currentRegistryData -DifferenceData $registryData))
{
Try {
Set-ItemProperty -Path $registryKey -Name $registryValue -Value $registryData
$result.changed = $true
$result.data_changed = $true
}
Catch
{

View file

@ -116,3 +116,16 @@ EXAMPLES = '''
value: hello
state: absent
'''
RETURN = '''
data_changed:
description: whether this invocation changed the data in the registry value
returned: always
type: boolean
sample: false
data_type_changed:
description: whether this invocation changed the datatype of the registry value
returned: always
type: boolean
sample: true
'''