1

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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. This fixed my problem. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.