2017-02-09 22:47:00 +00:00
|
|
|
#!powershell
|
|
|
|
|
2018-07-17 21:29:05 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
2017-02-09 22:47:00 +00:00
|
|
|
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
2017-03-23 22:15:28 +00:00
|
|
|
$params = Parse-Args -arguments $args -supports_check_mode $true
|
2017-02-20 11:49:36 +00:00
|
|
|
|
2017-03-23 22:15:28 +00:00
|
|
|
$path = Get-AnsibleParam -obj $params -name "path" -type "str" -failifempty $true -aliases "key"
|
|
|
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -aliases "entry","value"
|
2017-02-09 22:47:00 +00:00
|
|
|
|
|
|
|
$result = @{
|
|
|
|
changed = $false
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Get-NetHiveName($hive) {
|
|
|
|
# Will also check that the hive passed in the path is a known hive
|
|
|
|
switch ($hive.ToUpper()) {
|
|
|
|
"HKCR" {"ClassesRoot"}
|
|
|
|
"HKCC" {"CurrentConfig"}
|
|
|
|
"HKCU" {"CurrentUser"}
|
|
|
|
"HKLM" {"LocalMachine"}
|
2017-02-20 11:49:36 +00:00
|
|
|
"HKU" {"Users"}
|
2017-02-09 22:47:00 +00:00
|
|
|
default {"unsupported"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Get-PropertyType($hive, $path, $property) {
|
|
|
|
$type = (Get-Item REGISTRY::$hive\$path).GetValueKind($property)
|
|
|
|
switch ($type) {
|
|
|
|
"Binary" {"REG_BINARY"}
|
|
|
|
"String" {"REG_SZ"}
|
|
|
|
"DWord" {"REG_DWORD"}
|
|
|
|
"QWord" {"REG_QWORD"}
|
|
|
|
"MultiString" {"REG_MULTI_SZ"}
|
|
|
|
"ExpandString" {"REG_EXPAND_SZ"}
|
|
|
|
"None" {"REG_NONE"}
|
|
|
|
default {"Unknown"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Get-PropertyObject($hive, $net_hive, $path, $property) {
|
|
|
|
$value = (Get-ItemProperty REGISTRY::$hive\$path).$property
|
|
|
|
$type = Get-PropertyType -hive $hive -path $path -property $property
|
|
|
|
If ($type -eq 'REG_EXPAND_SZ') {
|
|
|
|
$raw_value = [Microsoft.Win32.Registry]::$net_hive.OpenSubKey($path).GetValue($property, $false, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
|
|
|
|
} ElseIf ($type -eq 'REG_BINARY' -or $type -eq 'REG_NONE') {
|
|
|
|
$raw_value = @()
|
|
|
|
foreach ($byte in $value) {
|
|
|
|
$hex_value = ('{0:x}' -f $byte).PadLeft(2, '0')
|
|
|
|
$raw_value += "0x$hex_value"
|
|
|
|
}
|
|
|
|
} Else {
|
|
|
|
$raw_value = $value
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:51:16 +00:00
|
|
|
$object = @{
|
2017-02-09 22:47:00 +00:00
|
|
|
raw_value = $raw_value
|
|
|
|
value = $value
|
|
|
|
type = $type
|
|
|
|
}
|
|
|
|
|
|
|
|
$object
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Test-RegistryProperty($hive, $path, $property) {
|
|
|
|
Try {
|
|
|
|
$type = (Get-Item REGISTRY::$hive\$path).GetValueKind($property)
|
|
|
|
} Catch {
|
|
|
|
$type = $null
|
|
|
|
}
|
|
|
|
|
|
|
|
If ($type -eq $null) {
|
|
|
|
$false
|
|
|
|
} Else {
|
|
|
|
$true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Will validate the key parameter to make sure it matches known format
|
2017-03-23 22:15:28 +00:00
|
|
|
if ($path -match "^([a-zA-Z_]*):\\(.*)$") {
|
2017-02-09 22:47:00 +00:00
|
|
|
$hive = $matches[1]
|
2017-03-23 22:15:28 +00:00
|
|
|
$reg_path = $matches[2]
|
2017-02-09 22:47:00 +00:00
|
|
|
} else {
|
2017-03-23 22:15:28 +00:00
|
|
|
Fail-Json $result "path does not match format 'HIVE:\KEY_PATH'"
|
2017-02-09 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Used when getting the actual REG_EXPAND_SZ value as well as checking the hive is a known value
|
|
|
|
$net_hive = Get-NetHiveName -hive $hive
|
|
|
|
if ($net_hive -eq 'unsupported') {
|
2017-03-23 22:15:28 +00:00
|
|
|
Fail-Json $result "the hive in path is '$hive'; must be 'HKCR', 'HKCC', 'HKCU', 'HKLM' or 'HKU'"
|
2017-02-09 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:15:28 +00:00
|
|
|
if (Test-Path REGISTRY::$hive\$reg_path) {
|
|
|
|
if ($name -eq $null) {
|
2017-02-09 22:47:00 +00:00
|
|
|
$property_info = @{}
|
2017-03-23 22:15:28 +00:00
|
|
|
$properties = Get-ItemProperty REGISTRY::$hive\$reg_path
|
2017-02-09 22:47:00 +00:00
|
|
|
|
|
|
|
foreach ($property in $properties.PSObject.Properties) {
|
|
|
|
# Powershell adds in some metadata we need to filter out
|
2017-03-23 22:15:28 +00:00
|
|
|
$real_property = Test-RegistryProperty -hive $hive -path $reg_path -property $property.Name
|
2017-02-09 22:47:00 +00:00
|
|
|
if ($real_property -eq $true) {
|
2017-03-23 22:15:28 +00:00
|
|
|
$property_object = Get-PropertyObject -hive $hive -net_hive $net_hive -path $reg_path -property $property.Name
|
2017-02-09 22:47:00 +00:00
|
|
|
$property_info.Add($property.Name, $property_object)
|
2017-02-20 11:49:36 +00:00
|
|
|
}
|
2017-02-09 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$sub_keys = @()
|
2017-03-23 22:15:28 +00:00
|
|
|
$sub_keys_raw = Get-ChildItem REGISTRY::$hive\$reg_path -ErrorAction SilentlyContinue
|
2017-02-09 22:47:00 +00:00
|
|
|
|
|
|
|
foreach ($sub_key in $sub_keys_raw) {
|
|
|
|
$sub_keys += $sub_key.PSChildName
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:15:28 +00:00
|
|
|
$result.exists = $true
|
|
|
|
$result.sub_keys = $sub_keys
|
|
|
|
$result.properties = $property_info
|
2017-02-09 22:47:00 +00:00
|
|
|
} else {
|
2017-03-23 22:15:28 +00:00
|
|
|
$exists = Test-RegistryProperty -hive $hive -path $reg_path -property $name
|
2017-02-09 22:47:00 +00:00
|
|
|
if ($exists -eq $true) {
|
2017-03-23 22:15:28 +00:00
|
|
|
$propertyObject = Get-PropertyObject -hive $hive -net_hive $net_hive -path $reg_path -property $name
|
|
|
|
$result.exists = $true
|
|
|
|
$result.raw_value = $propertyObject.raw_value
|
|
|
|
$result.value = $propertyObject.value
|
|
|
|
$result.type = $propertyObject.type
|
2017-02-09 22:47:00 +00:00
|
|
|
} else {
|
2017-03-23 22:15:28 +00:00
|
|
|
$result.exists = $false
|
2017-02-09 22:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-23 22:15:28 +00:00
|
|
|
$result.exists = $false
|
2017-02-09 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Exit-Json $result
|