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

Replace function Test-OosDscParameterState with Test-DscParameterState or Compare-DscParameterState #70

Open
johlju opened this issue Nov 19, 2023 · 0 comments
Labels
enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community.

Comments

@johlju
Copy link
Member

johlju commented Nov 19, 2023

Suggest replacing the function Test-OosDscParameterState with the Test-DscParameterState or Compare-DscParameterState from the common module DscResource.Common.

<#
.SYNOPSIS
This method is used to compare current and desired values for any DSC resource
.PARAMETER CurrentValues
This is hashtable of the current values that are applied to the resource
.PARAMETER DesiredValues
This is a PSBoundParametersDictionary of the desired values for the resource
.PARAMETER ValuesToCheck
This is a list of which properties in the desired values list should be checkked.
If this is empty then all values in DesiredValues are checked.
#>
function Test-OosDscParameterState()
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true, Position = 1)]
[HashTable]
$CurrentValues,
[Parameter(Mandatory = $true, Position = 2)]
[Object]
$DesiredValues,
[Parameter(Position = 3)]
[Array]
$ValuesToCheck
)
$returnValue = $true
if (($DesiredValues.GetType().Name -ne "HashTable") `
-and ($DesiredValues.GetType().Name -ne "CimInstance") `
-and ($DesiredValues.GetType().Name -ne "PSBoundParametersDictionary"))
{
throw ("Property 'DesiredValues' in Test-OOSDscParameterState must be either a " + `
"Hashtable or CimInstance. Type detected was $($DesiredValues.GetType().Name)")
}
if (($DesiredValues.GetType().Name -eq "CimInstance") -and ($null -eq $ValuesToCheck))
{
throw ("If 'DesiredValues' is a Hashtable then property 'ValuesToCheck' must contain " + `
"a value")
}
if (($null -eq $ValuesToCheck) -or ($ValuesToCheck.Count -lt 1))
{
$KeyList = $DesiredValues.Keys
}
else
{
$KeyList = $ValuesToCheck
}
$KeyList | ForEach-Object -Process {
if ($_ -ne "Verbose")
{
if (($CurrentValues.ContainsKey($_) -eq $false) `
-or ($CurrentValues.$_ -ne $DesiredValues.$_) `
-or (($DesiredValues.ContainsKey($_) -eq $true) -and ($DesiredValues.$_.GetType().IsArray)))
{
if ($DesiredValues.GetType().Name -eq "HashTable" -or `
$DesiredValues.GetType().Name -eq "PSBoundParametersDictionary")
{
$CheckDesiredValue = $DesiredValues.ContainsKey($_)
}
else
{
$CheckDesiredValue = Test-OOSDscObjectHasProperty $DesiredValues $_
}
if ($CheckDesiredValue)
{
$desiredType = $DesiredValues.$_.GetType()
$fieldName = $_
if ($desiredType.IsArray -eq $true)
{
if (($CurrentValues.ContainsKey($fieldName) -eq $false) `
-or ($null -eq $CurrentValues.$fieldName))
{
Write-Verbose -Message ("Expected to find an array value for " + `
"property $fieldName in the current " + `
"values, but it was either not present or " + `
"was null. This has caused the test method " + `
"to return false.")
$returnValue = $false
}
else
{
$arrayCompare = Compare-Object -ReferenceObject $CurrentValues.$fieldName `
-DifferenceObject $DesiredValues.$fieldName
if ($null -ne $arrayCompare)
{
Write-Verbose -Message ("Found an array for property $fieldName " + `
"in the current values, but this array " + `
"does not match the desired state. " + `
"Details of the changes are below.")
$arrayCompare | ForEach-Object -Process {
Write-Verbose -Message "$($_.InputObject) - $($_.SideIndicator)"
}
$returnValue = $false
}
}
}
else
{
switch ($desiredType.Name)
{
"String"
{
if ([string]::IsNullOrEmpty($CurrentValues.$fieldName) `
-and [string]::IsNullOrEmpty($DesiredValues.$fieldName))
{
}
else
{
Write-Verbose -Message ("String value for property " + `
"$fieldName does not match. " + `
"Current state is " + `
"'$($CurrentValues.$fieldName)' " + `
"and desired state is " + `
"'$($DesiredValues.$fieldName)'")
$returnValue = $false
}
}
"Int32"
{
if (($DesiredValues.$fieldName -eq 0) `
-and ($null -eq $CurrentValues.$fieldName))
{
}
else
{
Write-Verbose -Message ("Int32 value for property " + `
"$fieldName does not match. " + `
"Current state is " + `
"'$($CurrentValues.$fieldName)' " + `
"and desired state is " + `
"'$($DesiredValues.$fieldName)'")
$returnValue = $false
}
}
"Int16"
{
if (($DesiredValues.$fieldName -eq 0) `
-and ($null -eq $CurrentValues.$fieldName))
{
}
else
{
Write-Verbose -Message ("Int16 value for property " + `
"$fieldName does not match. " + `
"Current state is " + `
"'$($CurrentValues.$fieldName)' " + `
"and desired state is " + `
"'$($DesiredValues.$fieldName)'")
$returnValue = $false
}
}
default
{
Write-Verbose -Message ("Unable to compare property $fieldName " + `
"as the type ($($desiredType.Name)) is " + `
"not handled by the " + `
"Test-OOSDscParameterState cmdlet")
$returnValue = $false
}
}
}
}
}
}
}
return $returnValue
}

@johlju johlju added enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community. labels Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community.
Projects
None yet
Development

No branches or pull requests

1 participant