[PS] Using select-object with strings as parameters

Discussion in 'Scripting' started by Alan Taylor, May 5, 2006.

  1. Alan Taylor

    Alan Taylor Guest

    I'm trying to get "select-object" to correctly generate objects fit for a
    pipeline. I build up a string to pass to select-object. However, the
    output doesn't look correctly.

    Has anyone been able to get something like "1|select-object $foo_bar" to
    work correctly?

    Thanks,
    Alan


    PS C:\> 1|select-object @{expression = {"m1"}; name = "name"},@{expression =
    {"d1"}; name = "description"}

    name description
    ---- -----------
    m1 d1

    PS C:\> $foo_bar = "@{expression = `{`"m1`"`}; name = `"name`"},@{expression
    = `{`"d1`"`}; name = `"description`"}"
    PS C:\> $foo_bar
    @{expression = {"m1"}; name = "name"},@{expression = {"d1"}; name =
    "description"}
    PS C:\> 1|select-object $foo_bar

    @{expression = {"m1"}; name = "name"},@{expression = {"d1"}; name =
    "description"}
     
    Alan Taylor, May 5, 2006
    #1
    1. Advertisements

  2. Use invoke-expression to execute the string as PS code so that select-object
    gets an array of hashtables, instead of a single string:

    $foo_bar = "@{expression = `{`"m1`"`}; name = `"name`"},@{expression =
    `{`"d1`"`}; name = `"description`"}"
    1|select-object (invoke-expression $foo_bar)

    name description
    ---- -----------
    m1 d1
     
    Jouko Kynsijärvi, May 5, 2006
    #2
    1. Advertisements

  3. Hmm - select-object doesn't take a string, it takes an array of objects.
    These objects can be strings which name properties on the object, they can
    be scriptblocks that are used to calculate property values or they can be
    hashtables that follow a particular format. Now -since a collection of
    hashtables is just an object, it can be stored in a variable just like any
    other object.

    So - if this

    PS (1) > 1|select-object @{expression = {"m1"}; name =
    "name"},@{expression =
    name description
    ---- -----------
    m1 d1

    Is formatting things the way you want, then you can assign the array of
    hashtables used above to a variable by doing:

    PS (2) > $expr = @{expression = {"m1"}; name = "name"},@{expression =PS (3) >

    And then use it with select-object later on...

    PS (4) > 1|select-object $expr

    name description
    ---- -----------
    m1 d1

    Since the pattern is a collection of objects, it's easy to edit this
    pattern. Let's add another field descriptor to the collection...

    PS (5) > $expr += @{name="New";expression={"n1"}}
    PS (6) > 1|select-object $expr

    name description New
    ---- ----------- ---
    m1 d1 n1

    Next, let's change the name used in the first field to ZORK...

    PS (7) > $expr[0].name = "ZORK"
    PS (8) > 1|select-object $expr

    ZORK description New
    ---- ----------- ---
    m1 d1 n1


    And the expression used to generate the second field to multiply the
    pipeline object by 5:

    PS (9) > $expr[1].expression = {$_*5}
    PS (10) > 1|select-object $expr

    ZORK description New
    ---- ----------- ---
    m1 5 n1

    -bruce
     
    Bruce Payette [MSFT], May 6, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.