mirror of
https://github.com/PowerShell/PSDscResources
synced 2026-06-21 13:45:29 +00:00
porting registry
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
[ClassVersion("1.0.0"), FriendlyName("Registry")]
|
||||
class MSFT_xRegistryResource : OMI_BaseResource
|
||||
{
|
||||
[Key, Description("The path of the registry key to add, modify, or remove. This path must include the registry hive/drive.")] String Key;
|
||||
[Key, Description("The name of the registry value. To add or remove a registry key, specify this property as an empty string without specifying ValueType or ValueData. To modify or remove the default value of a registry key, specify this property as an empty string while also specifying ValueType or ValueData.")] String ValueName;
|
||||
[Write, Description("The data the specified registry key value should have as a string or an array of strings (MultiString only).")] String ValueData[];
|
||||
[Write, Description("The type the specified registry key value should have."), ValueMap{"String", "Binary", "DWord", "QWord", "MultiString", "ExpandString"},Values{"String", "Binary", "DWord", "QWord", "MultiString", "ExpandString"}] String ValueType;
|
||||
[Write, Description("Specifies whether or not the registry key or value should exist. To add or modify a registry key or value, set this property to Present. To remove a registry key or value, set the property to Absent."), ValueMap{"Present", "Absent"},Values{"Present", "Absent"}] String Ensure;
|
||||
[Write, Description("Specifies whether or not the specified DWord or QWord registry key data is provided in a hexadecimal format. Not valid for types other than DWord and QWord. The default value is $false.")] Boolean Hex;
|
||||
[Write, Description("Specifies whether or not to overwrite the specified registry key value if it already has a value or whether or not to delete a registry key that has subkeys. The default value is $false.")] Boolean Force;
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
[Description("Provides a mechanism to manage registry keys and values on a target node.") : Amended,AMENDMENT, LOCALE("MS_409")]
|
||||
class MSFT_RegistryResource : OMI_BaseResource
|
||||
{
|
||||
[Key, Description("The path of the registry key to add, modify, or remove. This path must include the registry hive/drive.") : Amended] String Key;
|
||||
[Key, Description("The name of the registry value. To add or remove a registry key, specify this property as an empty string without specifying ValueType or ValueData. To modify or remove the default value of a registry key, specify this property as an empty string while also specifying ValueType or ValueData.") : Amended] String ValueName;
|
||||
[Description("The data the specified registry key value should have as a string or an array of strings (MultiString only).") : Amended] String ValueData[];
|
||||
[Description("The type the specified registry key value should have.") : Amended] String ValueType;
|
||||
[Description("Specifies whether or not the registry key or value should exist. To add or modify a registry key or value, set this property to Present. To remove a registry key or value, set the property to Absent.") : Amended] String Ensure;
|
||||
[Description("Specifies whether or not the specified DWord or QWord registry key data is provided in a hexadecimal format. Not valid for types other than DWord and QWord. The default value is $false.") : Amended] Boolean Hex;
|
||||
[Description("Specifies whether or not to overwrite the specified registry key value if it already has a value or whether or not to delete a registry key that has subkeys. The default value is $false.") : Amended] Boolean Force;
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
# Localized resources for MSFT_RegistryResource
|
||||
|
||||
ConvertFrom-StringData @'
|
||||
DefaultValueDisplayName = (Default)
|
||||
|
||||
GetTargetResourceStartMessage = Get-TargetResource is starting for Registry resource with Key {0}
|
||||
GetTargetResourceEndMessage = Get-TargetResource has finished for Registry resource with Key {0}
|
||||
RegistryKeyDoesNotExist = The registry key at path {0} does not exist.
|
||||
RegistryKeyExists = The registry key at path {0} exists.
|
||||
RegistryKeyValueDoesNotExist = The registry key at path {0} does not have a value named {1}.
|
||||
RegistryKeyValueExists = The registry key at path {0} has a value named {1}.
|
||||
|
||||
SetTargetResourceStartMessage = Set-TargetResource is starting for Registry resource with Key {0}
|
||||
SetTargetResourceEndMessage = Set-TargetResource has finished for Registry resource with Key {0}
|
||||
CreatingRegistryKey = Creating registry key at path {0}...
|
||||
SettingRegistryKeyValue = Setting the value {0} under the registry key at path {1}...
|
||||
OverwritingRegistryKeyValue = Overwriting the value {0} under the registry key at path {1}...
|
||||
RemovingRegistryKey = Removing registry key at path {0}...
|
||||
RegistryKeyValueAlreadySet = The value {0} under the registry key at path {1} has already been set to the specified value.
|
||||
RemovingRegistryKeyValue = Removeing the value {0} from the registry key at path {1}...
|
||||
|
||||
TestTargetResourceStartMessage = Test-TargetResource is starting for Registry resource with Key {0}
|
||||
TestTargetResourceEndMessage = Test-TargetResource has finished for Registry resource with Key {0}
|
||||
RegistryKeyValueTypeDoesNotMatch = The type of the value {0} under the registry key at path {1} does not match the expected type. Expected {2} but was {3}.
|
||||
RegistryKeyValueDoesNotMatch = The value {0} under the registry key at path {1} does not match the expected value. Expected {2} but was {3}.
|
||||
|
||||
CannotRemoveExistingRegistryKeyWithSubKeysWithoutForce = The registry key at path {0} has subkeys. To remove this registry key please specifiy the Force parameter as $true.
|
||||
CannotOverwriteExistingRegistryKeyValueWithoutForce = The registry key at path {0} already has a value with the name {1}. To overwrite this registry key value please specifiy the Force parameter as $true.
|
||||
CannotRemoveExistingRegistryKeyValueWithoutForce = The registry key at path {0} already has a value with the name {1}. To remove this registry key value please specifiy the Force parameter as $true.
|
||||
RegistryDriveInvalid = The registry drive specified in the registry key path {0} is missing or invalid.
|
||||
ArrayNotAllowedForExpectedType = The specified value data has been declared as a string array, but the registry key type {0} cannot be converted from an array. Please declare the value data as only one string or use the registry type MultiString.
|
||||
DWordDataNotInHexFormat = The specified registry key value data {0} is not in the correct hex format to parse as an Int32 (dword).
|
||||
QWordDataNotInHexFormat = The specified registry key value data {0} is not in the correct hex format to parse as an Int64 (qword).
|
||||
BinaryDataNotInHexFormat = The specified registry key value data {0} is not in the correct hex format to parse as a Byte array (Binary).
|
||||
InvalidRegistryDrive = The registry drive {0} is invalid. Please update the Key parameter to include a valid registry drive.
|
||||
InvalidRegistryDriveAbbreviation = The registry drive abbreviation {0} is invalid. Please update the Key parameter to include a valid registry drive.
|
||||
RegistryDriveCouldNotBeMounted = The registry drive with the abbreviation {0} could not be mounted.
|
||||
'@
|
||||
@@ -0,0 +1,19 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Create a new registry key called MyNewKey as a subkey under the key
|
||||
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'.
|
||||
#>
|
||||
Configuration Sample_RegistryResource_AddKey
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Registry Registry1
|
||||
{
|
||||
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey'
|
||||
Ensure = 'Present'
|
||||
ValueName = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
If the registry key value MyValue under the key
|
||||
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' does not exist,
|
||||
creates it with the Binary value 0.
|
||||
|
||||
If the registry key value MyValue under the key
|
||||
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' already exists,
|
||||
overwrites it with the Binary value 0.
|
||||
#>
|
||||
Configuration Sample_RegistryResource_AddOrModifyValue
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Registry Registry1
|
||||
{
|
||||
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
Ensure = 'Present'
|
||||
ValueName = 'MyValue'
|
||||
ValueType = 'Binary'
|
||||
ValueData = '0x00'
|
||||
Force = $true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Removes the registry key called MyNewKey under the parent key
|
||||
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'.
|
||||
#>
|
||||
Configuration Sample_RegistryResource_RemoveKey
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Registry Registry1
|
||||
{
|
||||
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey'
|
||||
Ensure = 'Absent'
|
||||
ValueName = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Removes the registry key value MyValue from the key
|
||||
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'.
|
||||
#>
|
||||
Configuration Sample_RegistryResource_RemoveValue
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Registry Registry1
|
||||
{
|
||||
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
Ensure = 'Absent'
|
||||
ValueName = 'MyValue'
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -73,7 +73,7 @@ VariablesToExport = '*'
|
||||
AliasesToExport = @()
|
||||
|
||||
# DSC resources to export from this module
|
||||
DscResourcesToExport = 'Group', 'GroupSet', 'Script', 'Service', 'ServiceSet', 'User', 'WindowsFeature', 'WindowsFeatureSet', 'WindowsOptionalFeature', 'WindowsOptionalFeature', 'WindowsPackageCab', 'WindowsProcess', 'ProcessSet'
|
||||
DscResourcesToExport = 'Group', 'GroupSet', 'Registry', 'Script', 'Service', 'ServiceSet', 'User', 'WindowsFeature', 'WindowsFeatureSet', 'WindowsOptionalFeature', 'WindowsOptionalFeature', 'WindowsPackageCab', 'WindowsProcess', 'ProcessSet'
|
||||
|
||||
# List of all modules packaged with this module
|
||||
# ModuleList = @()
|
||||
|
||||
@@ -36,6 +36,7 @@ Please check out the common DSC Resources [contributing guidelines](https://gith
|
||||
|
||||
* [Group](#group): Provides a mechanism to manage local groups on a target node.
|
||||
* [GroupSet](#groupset): Provides a mechanism to configure and manage multiple Group resources with common settings but different names.
|
||||
* [Registry](#registry): Provides a mechanism to manage registry keys and values on a target node.
|
||||
* [Script](#script): Provides a mechanism to run PowerShell script blocks on a target node.
|
||||
* [Service](#service): Provides a mechanism to configure and manage Windows services on a target node.
|
||||
* [ServiceSet](#serviceset): Provides a mechanism to configure and manage multiple Service resources with common settings but different names.
|
||||
@@ -113,6 +114,35 @@ None
|
||||
|
||||
* [Add members to multiple groups](https://github.com/PowerShell/PSDscResources/blob/master/Examples/Sample_GroupSet_AddMembers.ps1)
|
||||
|
||||
### Registry
|
||||
|
||||
Provides a mechanism to manage registry keys and values on a target node.
|
||||
|
||||
#### Requirements
|
||||
|
||||
None
|
||||
|
||||
#### Parameters
|
||||
|
||||
* **[String] Key** _(Key)_: The path of the registry key to add, modify, or remove. This path must include the registry hive/drive (e.g. HKEY_LOCAL_MACHINE, HKLM:).
|
||||
* **[String] ValueName** _(Key)_: The name of the registry value. To add or remove a registry key, specify this property as an empty string without specifying ValueType or ValueData. To modify or remove the default value of a registry key, specify this property as an empty string while also specifying ValueType or ValueData.
|
||||
* **[String] Ensure** _(Write)_: Specifies whether or not the registry key or value should exist. To add or modify a registry key or value, set this property to Present. To remove a registry key or value, set this property to Absent. { *Present* | Absent }.
|
||||
* **[String] ValueData** _(Write)_: The data the specified registry key value should have as a string or an array of strings (MultiString only).
|
||||
* **[String] ValueType** _(Write)_: The type the specified registry key value should have. { *String* | Binary | DWord | QWord | MultiString | ExpandString }
|
||||
* **[Boolean] Hex** _(Write)_: Specifies whether or not the specified DWord or QWord registry key data is provided in a hexadecimal format. Not valid for types other than DWord and QWord. The default value is $false.
|
||||
* **[Boolean] Force** _(Write)_: Specifies whether or not to overwrite the specified registry key value if it already has a value or whether or not to delete a registry key that has subkeys. The default value is $false.
|
||||
|
||||
#### Read-Only Properties from Get-TargetResource
|
||||
|
||||
None
|
||||
|
||||
#### Examples
|
||||
|
||||
* [Add a registry key](https://github.com/PowerShell/PSDscResources/blob/master/Examples/Sample_RegistryResource_AddKey.ps1)
|
||||
* [Add or modify a registry key value](https://github.com/PowerShell/PSDscResources/blob/master/Examples/Sample_RegistryResource_AddOrModifyValue.ps1)
|
||||
* [Remove a registry key value](https://github.com/PowerShell/PSDscResources/blob/master/Examples/Sample_RegistryResource_RemoveValue.ps1)
|
||||
* [Remove a registry key](https://github.com/PowerShell/PSDscResources/blob/master/Examples/Sample_RegistryResource_RemoveKey.ps1)
|
||||
|
||||
### Script
|
||||
|
||||
Provides a mechanism to run PowerShell script blocks on a target node.
|
||||
@@ -453,11 +483,12 @@ The following parameters will be the same for each process in the set:
|
||||
* Updated resource module, unit tests, integration tests, and examples to reflect the changes made in xPSDesiredStateConfiguration.
|
||||
* Group:
|
||||
* Updated resource module, examples, and integration tests to reflect the changes made in xPSDesiredStateConfiguration.
|
||||
* Added Script
|
||||
* Added Script.
|
||||
* Added GroupSet, ServiceSet, WindowsFeatureSet, WindowsOptionalFeatureSet, and ProcessSet.
|
||||
* Added Set-StrictMode -'Latest' and $errorActionPreference -'Stop' to Group, Service, User, WindowsFeature, WindowsOptionalFeature, WindowsPackageCab.
|
||||
* Fixed bug in WindowsFeature in which is was checking the 'Count' property of an object that was not always an array.
|
||||
* Cleaned Group and Service resources and tests.
|
||||
* Added Registry.
|
||||
|
||||
### 2.1.0.0
|
||||
|
||||
|
||||
@@ -0,0 +1,318 @@
|
||||
<#
|
||||
WARNING: DO NOT RUN THESE TESTS ON A VALUABLE MACHINE!
|
||||
Running on a disposable VM or AppVeyor is strongly recommended.
|
||||
If these tests go awry, your machine's registry could be corrupted which will brick your machine!
|
||||
If this happens to you, it is fixable, but the fix is difficult and time-consuming.
|
||||
#>
|
||||
|
||||
$errorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version 'Latest'
|
||||
|
||||
# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
|
||||
$script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
|
||||
$script:commonTestHelperFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'CommonTestHelper.psm1'
|
||||
Import-Module -Name $commonTestHelperFilePath
|
||||
|
||||
$script:testEnvironment = Enter-DscResourceTestEnvironment `
|
||||
-DscResourceModuleName 'PSDscResources' `
|
||||
-DscResourceName 'MSFT_RegistryResource' `
|
||||
-TestType 'Integration'
|
||||
|
||||
try
|
||||
{
|
||||
Describe 'Registry End to End Tests' {
|
||||
BeforeAll {
|
||||
# Import Registry resource module for Get-TargetResource, Test-TargetResource, Set-TargetResource
|
||||
$moduleRootFilePath = Split-Path -Path $script:testsFolderFilePath -Parent
|
||||
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
|
||||
$registryResourceFolderFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'MSFT_RegistryResource'
|
||||
$registryResourceModuleFilePath = Join-Path -Path $registryResourceFolderFilePath -ChildPath 'MSFT_RegistryResource.psm1'
|
||||
Import-Module -Name $registryResourceModuleFilePath -Force
|
||||
|
||||
$script:registryKeyValueTypes = @( 'String', 'Binary', 'DWord', 'QWord', 'MultiString', 'ExpandString' )
|
||||
$script:testRegistryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\TestKey2'
|
||||
|
||||
# Force is specified as true for both of these configurations
|
||||
$script:confgurationFilePathKeyAndNameOnly = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_RegistryResource_KeyAndNameOnly.config.ps1'
|
||||
$script:confgurationFilePathWithDataAndType = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_RegistryResource_WithDataAndType.config.ps1'
|
||||
}
|
||||
|
||||
Context 'Create a new registry key' {
|
||||
$configurationName = 'CreateRegistryKey'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Present'
|
||||
ValueName = ''
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathKeyAndNameOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKey = Get-Item -Path $registryParameters.Key -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should have created the registry key' {
|
||||
$registryKey | Should Not Be $null
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Create a registry key value with no data or type' {
|
||||
$configurationName = 'CreateRegistryKeyValueNoDataOrType'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Present'
|
||||
ValueName = 'TestValue'
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathKeyAndNameOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKeyValue = Get-ItemProperty -Path $registryParameters.Key -Name $registryParameters.ValueName -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should have created the registry key value' {
|
||||
$registryKeyValue | Should Not Be $null
|
||||
}
|
||||
|
||||
It 'Should not have set the registry key value' {
|
||||
$registryKeyValue.($registryParameters.ValueName) | Should Be ''
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Set registry key value with data and String type' {
|
||||
$configurationName = 'SetRegistryKeyValueString'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Present'
|
||||
ValueName = 'TestValue'
|
||||
ValueType = 'String'
|
||||
ValueData = 'TestString1'
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathWithDataAndType -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKeyValue = Get-ItemProperty -Path $registryParameters.Key -Name $registryParameters.ValueName -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should have created the registry key value' {
|
||||
$registryKeyValue | Should Not Be $null
|
||||
}
|
||||
|
||||
It 'Should have set the registry key value to the specified String value' {
|
||||
$registryKeyValue.($registryParameters.ValueName) | Should Be $registryParameters.ValueData
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($registryKeyValueType in $script:registryKeyValueTypes)
|
||||
{
|
||||
$registryKeyValueData = switch ($registryKeyValueType)
|
||||
{
|
||||
'String' { 'TestString2'; break }
|
||||
'Binary' { '0xCAC1111'; break }
|
||||
'DWord' { [Int32]::MaxValue.ToString(); break }
|
||||
'QWord' { [Int64]::MaxValue.ToString(); break }
|
||||
'MultiString' { @('MultiString1', 'MultiString2'); break }
|
||||
'ExpandString' { '%WINDIR%'; break }
|
||||
}
|
||||
|
||||
$expectedRegistryKeyValue = switch ($registryKeyValueType)
|
||||
{
|
||||
'String' { 'TestString2'; break }
|
||||
'Binary' { [Byte[]]@( 12, 172, 17, 17 ); break }
|
||||
'DWord' { [Int32]::MaxValue; break }
|
||||
'QWord' { [Int64]::MaxValue; break }
|
||||
'MultiString' { [String[]]@('MultiString1', 'MultiString2'); break }
|
||||
'ExpandString' { 'C:\windows'; break }
|
||||
}
|
||||
|
||||
Context "Overwrite a registry key value with a $registryKeyValueType value" {
|
||||
$configurationName = "OverwriteRegistryKeyValue$registryKeyValueType"
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Present'
|
||||
ValueName = 'TestValue'
|
||||
ValueType = $registryKeyValueType
|
||||
ValueData = $registryKeyValueData
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathWithDataAndType -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKeyValue = Get-ItemProperty -Path $registryParameters.Key -Name $registryParameters.ValueName -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should be able to retrieve the registry key value' {
|
||||
$registryKeyValue | Should Not Be $null
|
||||
}
|
||||
|
||||
It 'Should have set the registry key value to the specified value' {
|
||||
Compare-Object -ReferenceObject $expectedRegistryKeyValue -DifferenceObject $registryKeyValue.($registryParameters.ValueName) | Should Be $null
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Set the registry key default value to a Binary value of 0' {
|
||||
$configurationName = 'SetDefaultRegistryKeyValueBinary0'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Present'
|
||||
ValueName = ''
|
||||
ValueType = 'Binary'
|
||||
ValueData = '0x00'
|
||||
}
|
||||
|
||||
$expectedRegistryKeyValue = [Byte[]]@(0)
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathWithDataAndType -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKeyValue = Get-ItemProperty -Path $registryParameters.Key -Name $registryParameters.ValueName -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should be able to retrieve the registry key value' {
|
||||
$registryKeyValue | Should Not Be $null
|
||||
}
|
||||
|
||||
It 'Should have set the registry key value to the specified Binary value' {
|
||||
Compare-Object -ReferenceObject $expectedRegistryKeyValue -DifferenceObject $registryKeyValue.'(default)' | Should Be $null
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove a registry key value' {
|
||||
$configurationName = 'RemoveRegistryKeyValue'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Absent'
|
||||
ValueName = 'TestValue'
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathKeyAndNameOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKeyValue = Get-ItemProperty -Path $registryParameters.Key -Name $registryParameters.ValueName -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should have removed the registry key value' {
|
||||
$registryKeyValue | Should Be $null
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove a default registry key value' {
|
||||
$configurationName = 'RemoveDefaultRegistryKeyValue'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Absent'
|
||||
ValueName = ''
|
||||
ValueType = 'Binary'
|
||||
ValueData = '0'
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathWithDataAndType -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKeyValue = Get-ItemProperty -Path $registryParameters.Key -Name $registryParameters.ValueName -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should have removed the registry key value' {
|
||||
$registryKeyValue | Should Be $null
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove a registry key' {
|
||||
$configurationName = 'RemoveRegistryKey'
|
||||
|
||||
$registryParameters = @{
|
||||
Key = $script:testRegistryKeyPath
|
||||
Ensure = 'Absent'
|
||||
ValueName = ''
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathKeyAndNameOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @registryParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
$registryKey = Get-Item -Path $registryParameters.Key -ErrorAction 'SilentlyContinue'
|
||||
|
||||
It 'Should have removed the registry key value' {
|
||||
$registryKey | Should Be $null
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters' {
|
||||
MSFT_RegistryResource\Test-TargetResource @registryParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
|
||||
}
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
<#
|
||||
WARNING: DO NOT RUN THESE TESTS ON A VALUABLE MACHINE!
|
||||
Running on a disposable VM or AppVeyor is strongly recommended.
|
||||
If these tests go awry, your machine's registry could be corrupted which will brick your machine!
|
||||
If this happens to you, it is fixable, but the fix is difficult and time-consuming.
|
||||
#>
|
||||
|
||||
$errorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version 'Latest'
|
||||
|
||||
# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
|
||||
$script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
|
||||
$script:testHelpersPath = Join-Path -Path $script:testFolderFilePath -ChildPath 'TestHelpers'
|
||||
Import-Module -Name (Join-Path -Path $script:testHelpersPath -ChildPath 'CommonTestHelper.psm1')
|
||||
|
||||
$script:testEnvironment = Enter-DscResourceTestEnvironment `
|
||||
-DscResourceModuleName 'PSDscResources' `
|
||||
-DscResourceName 'MSFT_RegistryResource' `
|
||||
-TestType 'Integration'
|
||||
|
||||
try
|
||||
{
|
||||
Describe 'Registry Integration Tests' {
|
||||
BeforeAll {
|
||||
# Import Registry test helper
|
||||
$registryTestHelperFilePath = Join-Path -Path $script:testHelpersPath -ChildPath 'MSFT_RegistryResource.TestHelper.psm1'
|
||||
Import-Module -Name $registryTestHelperFilePath -Force
|
||||
|
||||
# Import Registry resource module for Get-TargetResource, Test-TargetResource, Set-TargetResource
|
||||
$moduleRootFilePath = Split-Path -Path $script:testsFolderFilePath -Parent
|
||||
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
|
||||
$registryResourceFolderFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'MSFT_RegistryResource'
|
||||
$registryResourceModuleFilePath = Join-Path -Path $registryResourceFolderFilePath -ChildPath 'MSFT_RegistryResource.psm1'
|
||||
Import-Module -Name $registryResourceModuleFilePath -Force
|
||||
|
||||
$baseRegistryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\TestKey'
|
||||
$script:registryKeyPath = $baseRegistryKeyPath
|
||||
|
||||
$script:doNotDeleteRegistryKey = $false
|
||||
$script:registryDriveOriginallyMounted = $true
|
||||
|
||||
$loopTimeoutMinutes = 1
|
||||
|
||||
$startLoopTime = Get-Date
|
||||
while ((Test-RegistryKeyExists -KeyPath $script:registryKeyPath) -and $loopMinutes -lt $loopTimeoutMinutes)
|
||||
{
|
||||
$randomNumber = Get-Random
|
||||
$script:registryKeyPath = $baseRegistryKeyPath + $randomNumber
|
||||
$loopMinutes = ((Get-Date) - $startLoopTime).Minutes
|
||||
}
|
||||
|
||||
if (Test-RegistryKeyExists -KeyPath $script:registryKeyPath)
|
||||
{
|
||||
$script:doNotDeleteRegistryKey = $true
|
||||
throw "Timed out while attempting to set up a non-destructive registry key for testing. Last testing key attempted: $script:registryKeyPath"
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
BeforeEach {
|
||||
# Remove the test registry key if it already exists
|
||||
if (Test-RegistryKeyExists -KeyPath $script:registryKeyPath)
|
||||
{
|
||||
Remove-RegistryKey -KeyPath $script:registryKeyPath
|
||||
}
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
# Remove the test registry key if it already exists
|
||||
if ((Test-RegistryKeyExists -KeyPath $script:registryKeyPath) -and -not $script:doNotDeleteRegistryKey)
|
||||
{
|
||||
Remove-RegistryKey -KeyPath $script:registryKeyPath
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Old tests' {
|
||||
# Get-TargetResource
|
||||
It 'Should return Present when retrieving a blank value from an existing registry key' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName ''
|
||||
$getTargetResourceResult.Ensure | Should Be 'Present'
|
||||
}
|
||||
|
||||
It 'Should return Absent when retrieving a blank value from a registry key that does not exist' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environmental'
|
||||
$getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName ''
|
||||
$getTargetResourceResult.Ensure | Should Be 'Absent'
|
||||
}
|
||||
|
||||
It 'Should return Present when retrieving an existing value from an existing registry key' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$registryValueName = 'Path'
|
||||
$getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName $registryValueName
|
||||
$getTargetResourceResult.Ensure | Should Be 'Present'
|
||||
}
|
||||
|
||||
It 'Should return Absent when retrieving a nonexistant value from an existing registry key' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$registryValueName = 'PsychoPath'
|
||||
$getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName $registryValueName
|
||||
$getTargetResourceResult.Ensure | Should Be 'Absent'
|
||||
}
|
||||
|
||||
$commonRegistryKeys = @( 'HKEY_CURRENT_USER', 'HKEY_CLASSES_ROOT', 'HKEY_USERS', 'HKEY_CURRENT_CONFIG' )
|
||||
foreach ($commonRegistryKey in $commonRegistryKeys)
|
||||
{
|
||||
It "Should return Present when retrieving a blank value from $commonRegistryKey" {
|
||||
$getTargetResourceResult = Get-TargetResource -Key $commonRegistryKey -ValueName ''
|
||||
$getTargetResourceResult.Ensure | Should Be 'Present'
|
||||
}
|
||||
}
|
||||
|
||||
# Set-TargetResource
|
||||
It 'Should create a new registry key' {
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName ''
|
||||
|
||||
# Verify that the registry key has been created
|
||||
$registryKeyExists = Test-RegistryKeyExists -KeyPath $script:registryKeyPath
|
||||
$registryKeyExists | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should create a new registry key tree' {
|
||||
$registryKeyTreePath = Join-Path -Path (Join-Path -Path (Join-Path -Path $script:registryKeyPath -ChildPath 'A') -ChildPath 'B') -ChildPath 'C'
|
||||
|
||||
Set-TargetResource -Key $registryKeyTreePath -ValueName ''
|
||||
|
||||
# Verify that the registry key has been created
|
||||
$registryKeyExists = Test-RegistryKeyExists -KeyPath $registryKeyTreePath
|
||||
$registryKeyExists | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should remove a registry key' {
|
||||
# Create the test registry key
|
||||
New-TestRegistryKey -KeyPath $script:registryKeyPath
|
||||
|
||||
# Verify that the registry key exists before removal
|
||||
$registryKeyExists = Test-RegistryKeyExists -KeyPath $script:registryKeyPath
|
||||
$registryKeyExists | Should Be $true
|
||||
|
||||
# Now remove the TestKey
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName '' -Ensure 'Absent'
|
||||
|
||||
# Verify that the registry key has been removed
|
||||
$registryKeyExists = Test-RegistryKeyExists -KeyPath $script:registryKeyPath
|
||||
$registryKeyExists | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should remove a registry key tree' {
|
||||
$registryKeyTreePath = Join-Path -Path (Join-Path -Path (Join-Path -Path $script:registryKeyPath -ChildPath 'A') -ChildPath 'B') -ChildPath 'C'
|
||||
|
||||
# Create the test registry key
|
||||
New-TestRegistryKey -KeyPath $registryKeyTreePath
|
||||
|
||||
# Verify that the registry key tree exists before removal
|
||||
$registryKeyExists = Test-RegistryKeyExists -KeyPath $registryKeyTreePath
|
||||
$registryKeyExists | Should Be $true
|
||||
|
||||
# Remove the test registry key tree
|
||||
Set-TargetResource -Key $registryKeyTreePath -ValueName '' -Ensure 'Absent'
|
||||
|
||||
# Verify that the registry key tree has been removed
|
||||
$registryKeyExists = Test-RegistryKeyExists -KeyPath $registryKeyTreePath
|
||||
$registryKeyExists | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should create a new string registry key value' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = 'TestData'
|
||||
$valueType = 'String'
|
||||
|
||||
# Create the new registry key value
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
|
||||
# Verify that the registry key value has been created with the correct data and type
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
$registryValueExists | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should create a new binary registry key value' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = 'aabbcc'
|
||||
$valueType = 'Binary'
|
||||
|
||||
# Create the new registry key value
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType -Hex $true
|
||||
|
||||
# Verify that the registry key value has been created with the correct data and type
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
$registryValueExists | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should set the default value of a registry key' {
|
||||
$valueName = ''
|
||||
$valueData = 'DefaultValue'
|
||||
|
||||
# Create the new registry key value
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
|
||||
|
||||
# Verify that the registry key value has been created with the correct data and type
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName '(default)' -ValueData $valueData -ValueType 'String'
|
||||
$registryValueExists | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should remove a registry key value' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = 'TestData'
|
||||
$valueType = 'String'
|
||||
|
||||
# Create the test registry value
|
||||
New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
|
||||
# Verify that the registry key value exists before removal
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName
|
||||
$registryValueExists | Should Be $true
|
||||
|
||||
# Remove the registry value
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -Ensure 'Absent'
|
||||
|
||||
# Verify that the registry key value has been removed
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName
|
||||
$registryValueExists | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should remove the default value for a registry key' {
|
||||
$valueName = ''
|
||||
$valueData = 'DefaultValue'
|
||||
$valueType = 'String'
|
||||
|
||||
# Create the test registry value
|
||||
New-RegistryValue -KeyPath $script:registryKeyPath -ValueName '(default)' -ValueData $valueData -ValueType $valueType
|
||||
|
||||
# Verify that the registry key value exists before removal
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName '(default)'
|
||||
$registryValueExists | Should Be $true
|
||||
|
||||
# Remove the registry value
|
||||
Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -Ensure 'Absent'
|
||||
|
||||
# Verify that the registry key value has been removed
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName '(default)'
|
||||
$registryValueExists | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should create a new key and value with path containing forward slashes' {
|
||||
$registryKeyPathWithForwardSlashes = $script:registryKeyPath + '/Test/Key'
|
||||
$valueName = 'Testing'
|
||||
$valueData = 'TestValue'
|
||||
|
||||
# Create the new registry key value
|
||||
Set-TargetResource -Key $registryKeyPathWithForwardSlashes -ValueName $valueName -ValueData $valueData
|
||||
|
||||
# Verify that the registry key value has been created with the correct data and type
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $registryKeyPathWithForwardSlashes -ValueName $valueName -ValueData $valueData
|
||||
$registryValueExists | Should Be $true
|
||||
}
|
||||
|
||||
# Test-TargetResource
|
||||
It 'Should return true for an existing registry key' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName ''
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return false for a registry key that does not exist' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentally'
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName ''
|
||||
$testTargetResourceResult | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true for an existing registry value' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$valueName = 'path'
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return false for a registry value that does not exist' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$valueName = 'NonExisting'
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName
|
||||
$testTargetResourceResult | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true when Ensure is Absent and registry key does not exist' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentally'
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName '' -Ensure 'Absent'
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return false when Ensure is Absent and registry key exists' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName '' -Ensure 'Absent'
|
||||
$testTargetResourceResult | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return false when Ensure is Absent and registry value exists with invalid data' {
|
||||
$registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
|
||||
$valueName = 'path'
|
||||
$valueData = 'FakePath'
|
||||
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName -ValueData $valueData -Ensure 'Absent'
|
||||
$testTargetResourceResult | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true for a multi-string registry value' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = @('a', 'b', 'c')
|
||||
$valueType = 'MultiString'
|
||||
|
||||
# Create the test registry value
|
||||
New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
|
||||
$testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName -ValueData $valueData
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true for a binary registry value' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = 'abcd123'
|
||||
$valueType = 'Binary'
|
||||
|
||||
# Create the test registry value
|
||||
New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
|
||||
$testTargetResourceResult = Test-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true for an empty binary registry value' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = ''
|
||||
$valueType = 'Binary'
|
||||
|
||||
# Create the test registry value
|
||||
New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
|
||||
# Verify that the registry key value has been created with the correct data and type
|
||||
$registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
$registryValueExists | Should Be $true
|
||||
|
||||
$testTargetResourceResult = Test-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true for binary registry value with zeroes' {
|
||||
$valueName = 'TestValue'
|
||||
$valueData = 'abcd0123'
|
||||
$valueType = 'Binary'
|
||||
|
||||
# Create the test registry value
|
||||
New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
|
||||
|
||||
$testTargetResourceResult = Test-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
|
||||
$testTargetResourceResult | Should Be $true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
$ConfigurationName
|
||||
)
|
||||
|
||||
Configuration $ConfigurationName
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Key,
|
||||
|
||||
[ValidateSet('Present', 'Absent')]
|
||||
[String]
|
||||
$Ensure = 'Present',
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
[AllowEmptyString()]
|
||||
$ValueName
|
||||
)
|
||||
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Registry Registry1
|
||||
{
|
||||
Key = $Key
|
||||
Ensure = $Ensure
|
||||
ValueName = $ValueName
|
||||
Force = $true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
$ConfigurationName
|
||||
)
|
||||
|
||||
Configuration $ConfigurationName
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Key,
|
||||
|
||||
[ValidateSet('Present', 'Absent')]
|
||||
[String]
|
||||
$Ensure = 'Present',
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
[AllowEmptyString()]
|
||||
$ValueName,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateSet('String', 'Binary', 'DWord', 'QWord', 'MultiString', 'ExpandString')]
|
||||
[String]
|
||||
$ValueType,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String[]]
|
||||
[AllowEmptyCollection()]
|
||||
$ValueData
|
||||
)
|
||||
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Registry Registry1
|
||||
{
|
||||
Key = $Key
|
||||
Ensure = $Ensure
|
||||
ValueName = $ValueName
|
||||
ValueType = $ValueType
|
||||
ValueData = $ValueData
|
||||
Force = $true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests if a registry key exists.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the registry key to test for existence.
|
||||
Must include the registry hive.
|
||||
#>
|
||||
function Test-RegistryKeyExists
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([Boolean])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath
|
||||
)
|
||||
|
||||
return Test-Path -Path $KeyPath
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests if a registry key value exists.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the registry key that should contain the value to test for existence.
|
||||
Must include the registry hive.
|
||||
|
||||
.PARAMETER ValueName
|
||||
The name of the value to test for existence.
|
||||
|
||||
.PARAMETER ValueData
|
||||
The data the existing value should contain.
|
||||
|
||||
.PARAMETER ValueType
|
||||
The value type that the registry value should have.
|
||||
#>
|
||||
function Test-RegistryValueExists
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([Boolean])]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNull()]
|
||||
[AllowEmptyString()]
|
||||
[String]
|
||||
$ValueName,
|
||||
|
||||
[String]
|
||||
$ValueData,
|
||||
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$ValueType
|
||||
)
|
||||
|
||||
try
|
||||
{
|
||||
$registryValue = Get-ItemProperty -Path $KeyPath -Name $ValueName -ErrorAction 'SilentlyContinue'
|
||||
Write-Verbose -Message "Test-RegistryValueExists - Registry key value: $registryKeyValue"
|
||||
|
||||
$registryValueExists = $null -ne $registryValue
|
||||
|
||||
Write-Verbose -Message "Test-RegistryValueExists - Registry value is not null: $registryValueExists"
|
||||
|
||||
if (-not $registryValueExists)
|
||||
{
|
||||
return $false
|
||||
}
|
||||
|
||||
$registryValue = $registryValue.$ValueName
|
||||
|
||||
if ($PSBoundParameters.ContainsKey('ValueType'))
|
||||
{
|
||||
Write-Verbose -Message "Test-RegistryValueExists - Registry value type: $($registryValue.GetType().Name)"
|
||||
|
||||
if ($ValueType -eq 'Binary')
|
||||
{
|
||||
$registryValueExists = $registryValueExists -and ($registryValue.GetType().Name -eq 'Byte[]')
|
||||
$registryValue = Convert-ByteArrayToHexString -Data $registryValue
|
||||
}
|
||||
else
|
||||
{
|
||||
$registryValueExists = $registryValueExists -and ($registryValue.GetType().Name -eq $ValueType)
|
||||
}
|
||||
}
|
||||
|
||||
if ($PSBoundParameters.ContainsKey('ValueData'))
|
||||
{
|
||||
Write-Verbose -Message "Test-RegistryValueExists - Registry value data: $registryValue"
|
||||
|
||||
$registryValueExists = $registryValueExists -and ($ValueData -eq $registryValue)
|
||||
}
|
||||
|
||||
return $registryValueExists
|
||||
}
|
||||
catch
|
||||
{
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Creates a registry key.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the registry key to be created.
|
||||
Must include the registry hive.
|
||||
#>
|
||||
function New-TestRegistryKey
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath
|
||||
)
|
||||
|
||||
$parentPath = Split-Path -Path $KeyPath -Parent
|
||||
|
||||
if (-not (Test-RegistryKeyExists -KeyPath $parentPath))
|
||||
{
|
||||
New-TestRegistryKey -KeyPath $parentPath
|
||||
}
|
||||
|
||||
Write-Verbose -Message "New-TestRegistryKey - Creating new registry key at: $KeyPath"
|
||||
|
||||
$null = New-Item -Path $KeyPath
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Creates a registry key.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the registry key to be created.
|
||||
Must include the registry hive.
|
||||
|
||||
.PARAMETER ValueName
|
||||
The name of the value to add
|
||||
|
||||
.PARAMETER ValueData
|
||||
The data of the value to add.
|
||||
|
||||
.PARAMETER ValueType
|
||||
The type of the value to add.
|
||||
#>
|
||||
function New-RegistryValue
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNull()]
|
||||
[AllowEmptyString()]
|
||||
[String]
|
||||
$ValueName,
|
||||
|
||||
[Object]
|
||||
$ValueData,
|
||||
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$ValueType
|
||||
)
|
||||
|
||||
if (-not (Test-Path -Path $KeyPath))
|
||||
{
|
||||
New-TestRegistryKey -KeyPath $KeyPath
|
||||
}
|
||||
|
||||
if ($ValueType -ieq 'Binary')
|
||||
{
|
||||
$convertedValueData = @()
|
||||
|
||||
if (($ValueData.Length % 2) -eq 1)
|
||||
{
|
||||
$ValueData = '0' + $ValueData
|
||||
}
|
||||
|
||||
for($index = 0; $index -lt $ValueData.Length - 1; $index += 2)
|
||||
{
|
||||
$convertedValueData += [Convert]::ToInt32($ValueData.Substring($index, 2), 16)
|
||||
}
|
||||
|
||||
$ValueData = [Byte[]] $convertedValueData
|
||||
|
||||
Write-Verbose -Message "New-RegistryValue - Binary data: $ValueData"
|
||||
}
|
||||
|
||||
$null = New-ItemProperty -Path $KeyPath -Name $ValueName -Value $ValueData -PropertyType $ValueType
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Removes a registry key.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the registry key to remove
|
||||
Must include the registry hive.
|
||||
#>
|
||||
function Remove-RegistryKey
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath
|
||||
)
|
||||
|
||||
$null = Remove-Item -Path $KeyPath -Recurse -Force
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Removes a registry value.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The path to the registry key that contains the value to remove.
|
||||
Must include the registry hive.
|
||||
|
||||
.PARAMETER ValueName
|
||||
The name of the value to remove.
|
||||
#>
|
||||
function Remove-RegistryValue
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNull()]
|
||||
[AllowEmptyString()]
|
||||
[String]
|
||||
$ValueName
|
||||
)
|
||||
|
||||
$null = Remove-ItemProperty -Path $KeyPath -Name $ValueName -Force
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Mounts the registry drive of the given registry key path.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The registry key path that contains the registry drive to mount.
|
||||
#>
|
||||
function Mount-RegistryDrive
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath
|
||||
)
|
||||
|
||||
$driveName = (Split-Path -Path $KeyPath -Qualifier).TrimEnd(':')
|
||||
Write-Verbose -Message "Mount-RegistryDrive - Drive name: $driveName"
|
||||
|
||||
$registryDriveRootMappings = @{
|
||||
'HKCR' = 'HKEY_CLASSES_ROOT'
|
||||
'HKUS' = 'HKEY_USERS'
|
||||
'HKCC' = 'HKEY_CURRENT_CONFIG'
|
||||
'HKCU' = 'HKEY_CURRENT_USER'
|
||||
'HKLM' = 'HKEY_LOCAL_MACHINE'
|
||||
}
|
||||
|
||||
if ($registryDriveRootMappings.ContainsKey($driveName))
|
||||
{
|
||||
# Abbreviated name was given. Use this as the new PSDrive name and the elongated name as the root
|
||||
$null = New-PSDrive -Name $driveName -Root $registryDriveRootMappings[$driveName] -PSProvider 'Registry' -Scope 'Script'
|
||||
}
|
||||
elseif ($registryDriveRootMappings.ContainsValue($driveName))
|
||||
{
|
||||
$mappingKey = $null
|
||||
|
||||
# Find the abbreviated key that goes with the given registry drive path
|
||||
foreach ($key in $registryDriveRootMappings.Keys)
|
||||
{
|
||||
if ($registryDriveRootMappings[$key] -ieq $driveName)
|
||||
{
|
||||
$mappingKey = $key
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# Mount the PSDrive with the abbreviated name as the Name and the elongated name as the root
|
||||
$null = New-PSDrive -Name $mappingKey -Root $driveName -PSProvider 'Registry' -Scope 'Script'
|
||||
}
|
||||
else
|
||||
{
|
||||
throw "Mount-RegistryDrive - Invalid registry drive in key path provided: $KeyPath"
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Removes a registry drive.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The registry key path that contains the registry drive to remove.
|
||||
#>
|
||||
function Dismount-RegistryDrive
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath
|
||||
)
|
||||
|
||||
$driveName = Split-Path -Path $KeyPath -Qualifier
|
||||
Write-Verbose -Message "Dismount-RegistryDrive - Drive name: $driveName"
|
||||
|
||||
$null = Remove-PSDrive -Name $driveName -PSProvider 'Registry' -Scope 'Script' -Force
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests if the registry drive of the given registry key path is mounted.
|
||||
|
||||
.PARAMETER KeyPath
|
||||
The registry key path that contains the registry drive to test.
|
||||
#>
|
||||
function Test-RegistryDriveMounted
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([Boolean])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$KeyPath
|
||||
)
|
||||
|
||||
$driveName = Split-Path -Path $KeyPath -Qualifier
|
||||
Write-Verbose -Message "Test-RegistryDriveMounted - Drive name: $driveName"
|
||||
|
||||
$psDriveNames = (Get-PSDrive).Name.ToUpperInvariant()
|
||||
|
||||
return $psDriveNames -icontains $driveName
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Helper function to convert a byte array to its hex string representation
|
||||
|
||||
.PARAMETER Data
|
||||
Specifies the byte array to be converted.
|
||||
#>
|
||||
function Convert-ByteArrayToHexString
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNull()]
|
||||
[System.Object]
|
||||
$Data
|
||||
)
|
||||
|
||||
$hexString = ''
|
||||
$Data | ForEach-Object { $hexString += ('{0:x2}' -f $_) }
|
||||
|
||||
return $hexString
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function `
|
||||
'Test-RegistryKeyExists', `
|
||||
'Test-RegistryValueExists', `
|
||||
'New-TestRegistryKey', `
|
||||
'New-RegistryValue', `
|
||||
'Remove-RegistryKey', `
|
||||
'Remove-RegistryValue', `
|
||||
'Dismount-RegistryDrive', `
|
||||
'Test-RegistryDriveMounted'
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user