From b172255ee3dc86040f8293da5624256a1f936f3a Mon Sep 17 00:00:00 2001 From: Daniel Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Mon, 30 Sep 2024 08:18:07 +0100 Subject: [PATCH] `ConvertTo-CimInstance` add parameters for ClassName and Namespace (#130) --- CHANGELOG.md | 2 + source/Public/ConvertTo-CimInstance.ps1 | 26 ++++- .../Public/ConvertTo-CimInstance.Tests.ps1 | 97 +++++++++++++++---- 3 files changed, 101 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad4574d..543d7aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Assert-ElevatedUser` - Add new parameter `ErrorMessage` to allow custom error messages. +- `ConvertTo-CimInstance` + - Add parameters for `ClassName` and `Namespace` for custom values. Fixes [#128](https://github.com/dsccommunity/DscResource.Common/issues/128) ### Fixed diff --git a/source/Public/ConvertTo-CimInstance.ps1 b/source/Public/ConvertTo-CimInstance.ps1 index 9ff4372..52ab748 100644 --- a/source/Public/ConvertTo-CimInstance.ps1 +++ b/source/Public/ConvertTo-CimInstance.ps1 @@ -10,6 +10,16 @@ .PARAMETER Hashtable A hashtable with the values to convert. + .PARAMETER ClassName + The ClassName of the CimInstance to create. + + Default value is to 'MSFT_KeyValuePair'. + + .PARAMETER Namespace + The Namespace of the CimInstance to create. + + Default value is to 'root/microsoft/Windows/DesiredStateConfiguration'. + .OUTPUTS System.Object[] @@ -29,16 +39,26 @@ function ConvertTo-CimInstance [OutputType([System.Object[]])] param ( - [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Hashtable')] + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [System.Collections.Hashtable] - $Hashtable + $Hashtable, + + [Parameter()] + [ValidateNotNullOrEmpty()] + [System.String] + $ClassName = 'MSFT_KeyValuePair', + + [Parameter()] + [ValidateNotNullOrEmpty()] + [System.String] + $Namespace = 'root/microsoft/Windows/DesiredStateConfiguration' ) process { foreach ($item in $Hashtable.GetEnumerator()) { - New-CimInstance -ClassName 'MSFT_KeyValuePair' -Namespace 'root/microsoft/Windows/DesiredStateConfiguration' -Property @{ + New-CimInstance -ClassName $ClassName -Namespace $Namespace -Property @{ Key = $item.Key Value = if ($item.Value -is [array]) { diff --git a/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 b/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 index 42a8747..885d069 100644 --- a/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 @@ -55,33 +55,88 @@ Describe 'ConvertTo-CimInstance' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Des } } - Context 'When the array contains the expected record count' { - It 'Should not throw exception' { - { - $script:result = [Microsoft.Management.Infrastructure.CimInstance[]] ( - $hashtable | ConvertTo-CimInstance - ) - } | Should -Not -Throw - } + Context 'When parameters ''ClassName'' and ''Namespace'' are not supplied' { + Context 'When the array contains the expected record count' { + It 'Should not throw exception' { + { + $script:result = [Microsoft.Management.Infrastructure.CimInstance[]] ( + $hashtable | ConvertTo-CimInstance + ) + } | Should -Not -Throw + } - It "Should record count should be $($hashTable.Count)" { - $script:result.Count | Should -Be $hashtable.Count - } + It 'Should record count should be correct' { + $script:result.Count | Should -Be $hashtable.Count + } - It 'Should return result of type CimInstance[]' { - $script:result.GetType().Name | Should -Be 'CimInstance[]' - } + It 'Should return result of type CimInstance[]' { + $script:result.GetType().Name | Should -Be 'CimInstance[]' + } - It 'Should return value "k1" in the CimInstance array should be "v1"' { - ($script:result | Where-Object Key -eq k1).Value | Should -Be 'v1' - } + It 'Should return value "k1" in the CimInstance array should be "v1"' { + ($script:result | Where-Object Key -eq k1).Value | Should -Be 'v1' + } - It 'Should return value "k2" in the CimInstance array should be "100"' { - ($script:result | Where-Object Key -eq k2).Value | Should -Be 100 + It 'Should return value "k2" in the CimInstance array should be "100"' { + ($script:result | Where-Object Key -eq k2).Value | Should -Be 100 + } + + It 'Should return value "k3" in the CimInstance array should be "1,2,3"' { + ($script:result | Where-Object Key -eq k3).Value | Should -Be '1,2,3' + } + + It 'Should be the correct ''ClassName''' { + $script:result.CimSystemProperties.ClassName[0] | Should -Be 'MSFT_KeyValuePair' + } + + It 'Should be the correct ''Namespace''' { + $script:result.CimSystemProperties.Namespace[0] | Should -Be 'root/microsoft/Windows/DesiredStateConfiguration' + } } + } - It 'Should return value "k3" in the CimInstance array should be "1,2,3"' { - ($script:result | Where-Object Key -eq k3).Value | Should -Be '1,2,3' + Context 'When parameters ''ClassName'' and ''Namespace'' are supplied' { + Context 'When the array contains the expected record count' { + It 'Should not throw exception' { + $mockCimInstanceParams = @{ + ClassName = 'MSFT_TaskNamedValue' + Namespace = 'Root/Microsoft/Windows/TaskScheduler' + } + + { + $script:result = [Microsoft.Management.Infrastructure.CimInstance[]] ( + $hashtable | ConvertTo-CimInstance @mockCimInstanceParams + ) + } | Should -Not -Throw + } + + It 'Should record count should be correct' { + $script:result.Count | Should -Be $hashtable.Count + } + + It 'Should return result of type CimInstance[]' { + $script:result.GetType().Name | Should -Be 'CimInstance[]' + } + + It 'Should return value "k1" in the CimInstance array should be "v1"' { + ($script:result | Where-Object Key -eq k1).Value | Should -Be 'v1' + } + + It 'Should return value "k2" in the CimInstance array should be "100"' { + ($script:result | Where-Object Key -eq k2).Value | Should -Be 100 + } + + It 'Should return value "k3" in the CimInstance array should be "1,2,3"' { + ($script:result | Where-Object Key -eq k3).Value | Should -Be '1,2,3' + } + + It 'Should be the correct ''ClassName''' { + $script:result.CimSystemProperties.ClassName[0] | Should -Be 'MSFT_TaskNamedValue' + } + + It 'Should be the correct ''Namespace''' { + $script:result.CimSystemProperties.Namespace[0] | Should -Be 'Root/Microsoft/Windows/TaskScheduler' + } } } }