diff --git a/lib/ansible/modules/windows/win_regedit.ps1 b/lib/ansible/modules/windows/win_regedit.ps1 index 80db38e12e..6c9997a436 100644 --- a/lib/ansible/modules/windows/win_regedit.ps1 +++ b/lib/ansible/modules/windows/win_regedit.ps1 @@ -51,11 +51,6 @@ if (-not ($path -match "^HK(CC|CR|CU|LM|U):\\")) { Fail-Json $result "path: $path is not a valid powershell path, see module documentation for examples." } -# Allow empty values as the "(default)" value -if ($name -eq "") { - $name = "(default)" -} - Function Test-ValueData { Param ( [parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] $Path, @@ -145,21 +140,24 @@ if (-not (Test-Path HKCC:\)) { New-PSDrive -Name HKCC -PSProvider Registry -Root HKEY_CURRENT_CONFIG } + # Convert HEX string to binary if type binary if ($type -eq "binary" -and $data -ne $null -and $data -is [String]) { $data = Convert-RegExportHexStringToByteArray($data) } # Special case handling for the path's default property. -if ($name.ToLower() -eq "(default)") { +if ($name -eq "" -or ($name -ne $null -and $name.ToLower() -eq "(default)")) { + # Apparently, "(default)" cannot be of type expandstring, do it ourselves if ($type -eq "expandstring" -and $data -ne $null -and $data -is [String]) { $data = Expand-Environment($data) } + $name = "(default)" $type = "string" } # Support REG_NONE with empty value -# FIXME: REG_NONE support is not idempotent +# FIXME: REG_NONE support is not idempotent yet if ($type -eq "none" -or $data -eq $null) { $data = New-Object byte[] 0 # $data = ([byte[]] @()) @@ -174,7 +172,7 @@ if ($state -eq "present") { # Handle binary data $old_data = (Get-ItemProperty -Path $path | Select-Object -ExpandProperty $name) - if ($name.ToLower() -eq "(default)") { + if ($name -eq "(default)") { # Special case handling for the path's default property. # Because .GetValueKind() doesn't work for the (default) path property $old_type = "String".ToLower() @@ -291,7 +289,7 @@ if ($state -eq "present") { -"$name" = "$type`:$data" "@ - } elseif (Test-ValueData -Path $path -Value $name) { + } elseif (Test-ValueData -Path $path -Name $name) { if (-not $check_mode) { try { diff --git a/test/integration/targets/win_regedit/tasks/main.yml b/test/integration/targets/win_regedit/tasks/main.yml index 6948055dd1..72080e034f 100644 --- a/test/integration/targets/win_regedit/tasks/main.yml +++ b/test/integration/targets/win_regedit/tasks/main.yml @@ -32,7 +32,7 @@ - assert: that: - "check00_result.failed == true" - - "check00_result.msg == 'key: HKCU\\Software is not a valid powershell path, see module documentation for examples.'" + - "check00_result.msg == 'path: HKCU\\Software is not a valid powershell path, see module documentation for examples.'" - name: remove setting win_regedit: