Reputation: 478
I'm trying to create a dictionary in Powershell. Here is the script, I'm working on,
$environmentId = "Test"
$Dictionary = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$xml = [xml] (Get-Content "deploy.config")
$xml.SelectNodes("descendant::configuration/environment[@id='$($environmentId)']/descendant::text()[normalize-space()]") | ? Value | % {
$Dictionary.Add($_.ParentNode.ToString(), $_.Value)
}
write-output $Dictionary
This script is working in Powershell Version 4.0. But currently we are using Version 2.0. When I run this script on 2.0 version, throwing following error,
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "Value" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Users\pwsp_kkumar\Desktop\dictionary.ps1:6 char:129
+ $xml.SelectNodes("descendant::configuration/environment[@id='$($environmentId)']/descendant::text()[normalize-spa
ce()]") | ? <<<< Value | % {
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand
Can someone please suggest me the Powershell version 2.0 equivalent command, to fix the above error. Thank you.
Upvotes: 1
Views: 1569
Reputation: 18156
The problem is with this part of the pipeline:
? Value
If you're trying to make sure that the Value property is not null, you could use
? {$_.Value}
That should work with no problem. It also matches what you're adding later ($_.Value).
Upvotes: 1