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

ConvertTo-CimInstance add parameters for ClassName and Namespace #130

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
}
}
}