-
Notifications
You must be signed in to change notification settings - Fork 54
WindowsOptionalFeature: Ensure empty array output is not enumerated prematurely #192
base: dev
Are you sure you want to change the base?
Conversation
@@ -330,7 +330,7 @@ function Convert-CustomPropertyArrayToStringArray | |||
} | |||
} | |||
|
|||
return $propertiesAsStrings | |||
Write-Output $propertiesAsStrings -NoEnumerate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jambar42 - can you use a parameter name here? E.g. Write-Output -InputObject ...
or $propertiesAsStrings | Write-Output -NoEnumerate
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried both of the suggestions listed above and both return the same results. The act of piping the object or feeding it into -InputObject both cause enumeration resulting in a failed LCM call to the Get method.
Invoke-DscResource -Name WindowsOptionalFeature -Method Get -ModuleName PSDSCResources -Property @{Name = 'HyperVisorPlatform'; Ensure = 'Present'}
A general error occurred that is not covered by a more specific error code.
+ CategoryInfo : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
+ FullyQualifiedErrorId : MI RESULT 1
+ PSComputerName : localhost
Another option that may work is to make the output of line 58 always be an array.
PSDscResources/DscResources/MSFT_WindowsOptionalFeature/MSFT_WindowsOptionalFeature.psm1
Lines 54 to 65 in c6323a4
$windowsOptionalFeatureResource = @{ | |
LogPath = $windowsOptionalFeatureProperties.LogPath | |
Ensure = Convert-FeatureStateToEnsure -State $windowsOptionalFeatureProperties.State | |
CustomProperties = Convert-CustomPropertyArrayToStringArray ` | |
-CustomProperties $windowsOptionalFeatureProperties.CustomProperties | |
Name = $windowsOptionalFeatureProperties.FeatureName | |
LogLevel = $windowsOptionalFeatureProperties.LogLevel | |
Description = $windowsOptionalFeatureProperties.Description | |
DisplayName = $windowsOptionalFeatureProperties.DisplayName | |
} | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is really strange. We have a standard that we must only used named parameters, rather than positional. Specifying a parameter name shouldn't have changed the behavior here. I don't know what could have caused that.
What you could try is casting the value to an array in the return - although not sure this would work either:
return @($propertiesAsStrings)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my results using the various outputs from Convert-CustomPropertyArrayToStringArray. So far, I've only found the first to return an empty array directly from this function. If you have other methods of ensuring this function always returns an array, even if empty I am curious to see them.
Write-Output $propertiesAsStrings -NoEnumerate
(Convert-CustomPropertyArrayToStringArray -CustomProperties @()).GetType()
IsPublic IsSerial Name BaseType
True True String[] System.Array
Write-Output -InputObject $propertiesAsStrings -NoEnumerate
(Convert-CustomPropertyArrayToStringArray -CustomProperties @()).GetType()
You cannot call a method on a null-valued expression.
At line:1 char:1
(Convert-CustomPropertyArrayToStringArray -CustomProperties @()).GetT ...
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull
return @($propertiesAsStrings)
(Convert-CustomPropertyArrayToStringArray -CustomProperties @()).GetType()
You cannot call a method on a null-valued expression.
At line:1 char:1
(Convert-CustomPropertyArrayToStringArray -CustomProperties @()).GetT ...
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other option would be to adjust the calling of this function, however I don't think this is as clean and doesn't fit well with being unit tested.
[System.String[]]$CustomProperties = Convert-CustomPropertyArrayToStringArray `
-CustomProperties $windowsOptionalFeatureProperties.CustomProperties
$windowsOptionalFeatureResource = @{
LogPath = $windowsOptionalFeatureProperties.LogPath
Ensure = Convert-FeatureStateToEnsure -State $windowsOptionalFeatureProperties.State
CustomProperties = $CustomProperties
Name = $windowsOptionalFeatureProperties.FeatureName
LogLevel = $windowsOptionalFeatureProperties.LogLevel
Description = $windowsOptionalFeatureProperties.Description
DisplayName = $windowsOptionalFeatureProperties.DisplayName
}
Pull Request (PR) description
Ensure empty array output is not enumerated prematurely.
This Pull Request (PR) fixes the following issues
Task list
Entry should say what was changed, and how that affects users (if applicable).
and comment-based help.