Skip to content

Commit

Permalink
ConvertTo-CimInstance add parameters for ClassName and Namespace (#130
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dan-hughes authored Sep 30, 2024
1 parent 00506d4 commit b172255
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 23 additions & 3 deletions source/Public/ConvertTo-CimInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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])
{
Expand Down
97 changes: 76 additions & 21 deletions tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
}

0 comments on commit b172255

Please sign in to comment.