PSDscResources icon indicating copy to clipboard operation
PSDscResources copied to clipboard

Registry: Set "Absent" with ValueData fails if Key uses "HKEY_LOCAL_MACHINE\..."

Open dbaileyut opened this issue 3 years ago • 1 comments

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

Trying to remove a registry value. I had been successfully creating Registry resources specifying the Keys in the "HKEY_LOCAL_MACHINE\*" format. However, when I switched to Ensure = 'Absent', it failed.

Verbose logs showing the problem

PowerShell DSC resource MSFT_RegistryResource 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\WOW6432Node\Microsoft\Windows\CurrentVersion\Run' because it does not exist. + CategoryInfo : InvalidOperation: (:) [], CimException + FullyQualifiedErrorId : ProviderOperationExecutionFailure + PSComputerName : localhost

Suggested solution to the issue

Account for specifying keys in the HKEY_LOCAL_MACHINE format (which works for 'Present') in the code at: https://github.com/PowerShell/PSDscResources/blob/7064eda52d939a4a3ce40e1f38756cfe6a09acfd/DscResources/MSFT_RegistryResource/MSFT_RegistryResource.psm1#L309-L313

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

Registry RemoveTeamsMachineAutoStart
{
    Key = 'HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run'
    ValueName = 'Teams'
    ValueType = 'String'
    ValueData = '"C:\Program Files (x86)\Microsoft\Teams\current\Teams.exe"'
    Ensure = 'Absent'
    Force =  $true
}

The operating system the target node is running

OsName : Microsoft Windows 10 Enterprise for Virtual Desktops OsOperatingSystemSKU : 175 OsArchitecture : 64-bit WindowsVersion : 2009 WindowsBuildLabEx : 19041.1.amd64fre.vb_release.191206-1406 OsLanguage : en-US OsMuiLanguages : {en-US}

Version and build of PowerShell the target node is running

Name Value


PSVersion 5.1.19041.1023 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1023 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)

2.12.0.0

dbaileyut avatar Jul 13 '21 02:07 dbaileyut

Just ran into this, because I inadvertently had imported PSDscResources. Not importing PSDscResources makes it work, as it then falls back to PSDesiredStateConfiguration.

It also works just writing 'HKLM:\Software...' instead of HKEY_LOCAL_MACHINE, so it utilizes the PSDrive for Cert:

Incase anyone else stumples upon this down the road.

heinejeppesen avatar Mar 31 '23 09:03 heinejeppesen