Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xRegistry: Fails to remove reg key when the Key is specified like 'HKEY_LOCAL_MACHINE' #444

Closed
mkht opened this issue Sep 25, 2018 · 2 comments · Fixed by #445
Closed

xRegistry: Fails to remove reg key when the Key is specified like 'HKEY_LOCAL_MACHINE' #444

mkht opened this issue Sep 25, 2018 · 2 comments · Fixed by #445
Labels
bug The issue is a bug. good first issue The issue should be easier to fix and can be taken up by a beginner to learn to contribute on GitHub

Comments

@mkht
Copy link
Contributor

mkht commented Sep 25, 2018

Details of the scenario you tried and the problem that is occurring

When deleting a registry key using the xRegistry resource, if we specify a traditional registry path like HKEY_LOCAL_MACHINE\... instead of a PSDrive style path, Then a ProviderOperationExecutionFailure error will be occured.

Verbose logs showing the problem

VERBOSE: [DSCTestVM]: LCM:  [ Start  Set      ]  [[xRegistry]RemoveRegistryKey]
VERBOSE: [DSCTestVM]:                            [[xRegistry]RemoveRegistryKey] Set-TargetResource is starting for Registry resource with Key HKEY_LOCAL_MACHINE\SOFTWARE\SOMETHING
VERBOSE: [DSCTestVM]:                            [[xRegistry]RemoveRegistryKey] The registry key at path HKEY_LOCAL_MACHINE\SOFTWARE\SOMETHING exists.
VERBOSE: [DSCTestVM]:                            [[xRegistry]RemoveRegistryKey] Removing registry key at path HKEY_LOCAL_MACHINE\SOFTWARE\SOMETHING...
VERBOSE: [DSCTestVM]: LCM:  [ End    Set      ]  [[xRegistry]RemoveRegistryKey]  in 0.0160 seconds.
PowerShell DSC resource MSFT_xRegistryResource  failed to execute Set-TargetResource functionality with error message: The running command stopped because the preference variable "ErrorActionPreference" or common 
parameter is set to Stop: Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SOFTWARE\SOMETHING' because it does not exist. 
    + CategoryInfo          : InvalidOperation: (:) [], CimException
    + FullyQualifiedErrorId : ProviderOperationExecutionFailure
    + PSComputerName        : localhost
 
VERBOSE: [DSCTestVM]: LCM:  [ End    Set      ]
The SendConfigurationApply function did not succeed.
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 1
    + PSComputerName        : localhost
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 0.183 seconds

Suggested solution to the issue

Fix

The DSC configuration that is used to reproduce the issue (as detailed as possible)

# Create empty reg key for test
New-Item -Path 'HKLM:\SOFTWARE\SOMETHING'

Configuration xRegistry_Test
{
    Import-DscResource –ModuleName 'xPSDesiredStateConfiguration'

    xRegistry RemoveRegistryKey
    {
        Ensure = 'Absent'
        ValueName = ''
        Key = 'HKEY_LOCAL_MACHINE\SOFTWARE\SOMETHING'   #Error
      # Key = 'HKLM:\SOFTWARE\SOMETHING'                #It's OK
    }
}

xRegistry_Test -OutputPath C:\DSC
Start-DscConfiguration -Path C:\DSC -Wait -Verbose -Force

The operating system the target node is running

OsName               : Microsoft Windows 10 Pro
OsOperatingSystemSKU : 48
OsArchitecture       : 64-bit
WindowsVersion       : 1803
WindowsBuildLabEx    : 17134.1.amd64fre.rs4_release.180410-1804
OsLanguage           : en-US
OsMuiLanguages       : {en-US}

Version and build of PowerShell the target node is running

PS C:\Users\Administrator> $PSVersionTable

Name                           Value                                                                                                                                                                                 
----                           -----                                                                                                                                                                                 
PSVersion                      5.1.17134.228                                                                                                                                                                         
PSEdition                      Desktop                                                                                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                               
BuildVersion                   10.0.17134.228                                                                                                                                                                        
CLRVersion                     4.0.30319.42000                                                                                                                                                                       
WSManStackVersion              3.0                                                                                                                                                                                   
PSRemotingProtocolVersion      2.3                                                                                                                                                                                   
SerializationVersion           1.1.0.1             

Version of the DSC module that was used ('dev' if using current dev branch)

dev

@johlju
Copy link
Member

johlju commented Sep 26, 2018

It tries to remove the registry by calling Remove-Item

https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/71acfb570ebd387f402379f10c77ba71e90e1c00/DSCResources/MSFT_xRegistryResource/MSFT_xRegistryResource.psm1#L332

But it calls with the passed in Key, all other functionality are using the variable $registryKey which contains the registry key, which in turn has been assigned by a helper function, that converted the Key path to a correct PSDrive path.

https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/71acfb570ebd387f402379f10c77ba71e90e1c00/DSCResources/MSFT_xRegistryResource/MSFT_xRegistryResource.psm1#L215

So instead of using $Key on the Remove-Item call, it should get the correct PSDrive path from the variable $registryKey.

@johlju johlju added bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. good first issue The issue should be easier to fix and can be taken up by a beginner to learn to contribute on GitHub labels Sep 26, 2018
@mkht
Copy link
Contributor Author

mkht commented Sep 26, 2018

Thanks @johlju
Now I'm trying to fix it.

johlju pushed a commit that referenced this issue Sep 27, 2018
…#445)

- Changes to xRegistry
  - Fixed an issue that fails to remove reg key when the `Key` is specified as
    common registry path (issue #444).
@johlju johlju removed the help wanted The issue is up for grabs for anyone in the community. label Sep 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug. good first issue The issue should be easier to fix and can be taken up by a beginner to learn to contribute on GitHub
Projects
None yet
2 participants