From e98e757ec42bea25a8e7b195a47edaf4e114ea00 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Mon, 29 Jan 2018 13:57:03 -0800 Subject: [PATCH] Fix win_regedit module and tests. (#35478) --- lib/ansible/modules/windows/win_regedit.ps1 | 21 ++++++++-- .../targets/win_regedit/tasks/tests.yml | 40 +++++++++++-------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/ansible/modules/windows/win_regedit.ps1 b/lib/ansible/modules/windows/win_regedit.ps1 index 76654b13e0c..0e03908bdf2 100644 --- a/lib/ansible/modules/windows/win_regedit.ps1 +++ b/lib/ansible/modules/windows/win_regedit.ps1 @@ -448,7 +448,12 @@ try { if (-not $check_mode) { $reg_key = Get-Item -Path $path try { - $reg_key.OpenSubKey($null, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree).SetValue($name, $data, $type) + $sub_key = $reg_key.OpenSubKey($null, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree) + try { + $sub_key.SetValue($name, $data, $type) + } finally { + $sub_key.Close() + } } catch { Fail-Json $result "failed to change registry property '$name' at $($path): $($_.Exception.Message)" } finally { @@ -476,7 +481,12 @@ $key_prefix[$path] if (-not $check_mode) { $reg_key = Get-Item -Path $path try { - $reg_key.OpenSubKey($null, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree).SetValue($name, $data, $type) + $sub_key = $reg_key.OpenSubKey($null, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree) + try { + $sub_key.SetValue($name, $data, $type) + } finally { + $sub_key.Close() + } } catch { Fail-Json $result "failed to change registry property '$name' at $($path): $($_.Exception.Message)" } finally { @@ -525,7 +535,12 @@ $key_prefix[$path] if (-not $check_mode) { $reg_key = Get-Item -Path $path try { - $reg_key.OpenSubKey($null, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree).DeleteValue($name) + $sub_key = $reg_key.OpenSubKey($null, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree) + try { + $sub_key.DeleteValue($name) + } finally { + $sub_key.Close() + } } catch { Fail-Json $result "failed to delete registry property '$name' at $($path): $($_.Exception.Message)" } finally { diff --git a/test/integration/targets/win_regedit/tasks/tests.yml b/test/integration/targets/win_regedit/tasks/tests.yml index 1694cbbf8fc..f42ccfcfe30 100644 --- a/test/integration/targets/win_regedit/tasks/tests.yml +++ b/test/integration/targets/win_regedit/tasks/tests.yml @@ -469,13 +469,15 @@ - name: get result of create new key in loaded hive (check mode) win_shell: | + $ErrorActionPreference = "Stop" ®.exe load HKLM\ANSIBLE C:\Users\Default\NTUSER.dat > $null Test-Path -Path HKLM:\ANSIBLE\NewKey + [GC]::Collect() + [GC]::WaitForPendingFinalizers() + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: new_key_in_hive_result_check -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of create new key in loaded hive (check mode) assert: that: @@ -491,13 +493,15 @@ - name: get result of create new key in loaded hive win_shell: | + $ErrorActionPreference = "Stop" ®.exe load HKLM\ANSIBLE C:\Users\Default\NTUSER.dat > $null Test-Path -Path HKLM:\ANSIBLE\NewKey + [GC]::Collect() + [GC]::WaitForPendingFinalizers() + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: new_key_in_hive_result -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of create new key in loaded hive assert: that: @@ -533,12 +537,12 @@ $prop = Get-ItemProperty -Path HKLM:\ANSIBLE\NewKey -Name TestProp -ErrorAction SilentlyContinue if ($prop) { $prop.TestProp + $prop.Close() } + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: new_prop_in_hive_result_check -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of set hive key property (check mode) assert: that: @@ -561,12 +565,12 @@ $prop = Get-ItemProperty -Path HKLM:\ANSIBLE\NewKey -Name TestProp -ErrorAction SilentlyContinue if ($prop) { $prop.TestProp + $prop.Close() } + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: new_prop_in_hive_result -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of set hive key property assert: that: @@ -603,12 +607,12 @@ $prop = Get-ItemProperty -Path HKLM:\ANSIBLE\NewKey -Name TestProp -ErrorAction SilentlyContinue if ($prop) { $prop.TestProp + $prop.Close() } + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: remove_prop_in_hive_result_check -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of remove hive key property (check mode) assert: that: @@ -629,12 +633,12 @@ $prop = Get-ItemProperty -Path HKLM:\ANSIBLE\NewKey -Name TestProp -ErrorAction SilentlyContinue if ($prop) { $prop.TestProp + $prop.Close() } + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: remove_prop_in_hive_result -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of remove hive key property assert: that: @@ -665,13 +669,15 @@ - name: get result of remove key in loaded hive (check mode) win_shell: | + $ErrorActionPreference = "Stop" ®.exe load HKLM\ANSIBLE C:\Users\Default\NTUSER.dat > $null Test-Path -Path HKLM:\ANSIBLE\NewKey + [GC]::Collect() + [GC]::WaitForPendingFinalizers() + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: remove_key_in_hive_result_check -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of removekey in loaded hive (check mode) assert: that: @@ -688,13 +694,15 @@ - name: get result of remove key in loaded hive win_shell: | + $ErrorActionPreference = "Stop" ®.exe load HKLM\ANSIBLE C:\Users\Default\NTUSER.dat > $null Test-Path -Path HKLM:\ANSIBLE\NewKey + [GC]::Collect() + [GC]::WaitForPendingFinalizers() + ®.exe unload HKLM\ANSIBLE > $null exit 0 register: remove_key_in_hive_result -- win_command: reg.exe unload HKLM\ANSIBLE - - name: assert result of remove key in loaded hive assert: that: