Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Select object with escaped wildcard

Reply
 
Old 3 Weeks Ago   #1 (permalink)
stej


 
 

Select object with escaped wildcard

Hello there, I'm confused about how things work in this example:

New-Object psobject| Add-Member noteproperty '*' 'a' -pass | select -
expand '`*'

I think it should work, but it doesn't. Even adding backticks doesn't
effect the result - it still returns error '''Select-Object : Property
"`*" cannot be found.'''

(the reason why I try to solve it because I have some PSObject-s with
automatically added properties and the property names are not
restricted)

Does anybody have the idea how to get the property (with a special
wildcard character in its name) using select-object?

My System SpecsSystem Spec
Old 3 Weeks Ago   #2 (permalink)
Martin Zugec


 
 

Re: Select object with escaped wildcard

Hi,

well, in this case you can always refer to psobject.Properties:

$(New-Object psobject| Add-Member noteproperty '*' 'a' -
pass).psobject.Properties | Where {$_.Name -eq "*"}

I understand what you are trying to reach and I agree that escape
character should work in this case.

Martin
My System SpecsSystem Spec
Old 3 Weeks Ago   #3 (permalink)
stej


 
 

Re: Select object with escaped wildcard

Thanks for reply, Martin.

I prefer shorter way: (New-Object psobject| Add-Member noteproperty
'*' 'yes' -pass)."*"
but you reminded me in your answer, that there is something I don't
use very ofteh - that there is psobject property with some interesting
methods.

(I was tempted to submit it as a bug, but was not sure if I was right.
I'll wait some time before the submit. )

On 3 lis, 09:35, Martin Zugec <martin.zu...@newsgroup> wrote:
Quote:

> Hi,
>
> well, in this case you can always refer to psobject.Properties:
>
> $(New-Object psobject| Add-Member noteproperty '*' 'a' -
> pass).psobject.Properties | Where {$_.Name -eq "*"}
>
> I understand what you are trying to reach and I agree that escape
> character should work in this case.
>
> Martin
My System SpecsSystem Spec
Old 3 Weeks Ago   #4 (permalink)
Martin Zugec


 
 

Re: Select object with escaped wildcard

Both psobject and psbase are extremely important if you want to dig
deeper into Powershell and use it to script something it was never
designed for.

Whenever I found myself in situation that I try to do something that
Posh doesn't expect or when I run into situation that Posh tries to be
too smart, I am refering to psobject or psbase.

For me, most typical and painful usage is while handling with XML
documents\elements. While in Posh v2 (my dev station), most properties
are directly supported, if you try to run same code in Posh v1, it
will fail (for example ParentNode, InnerText etc etc).

I remember that I encountered similar situation as yours at one
customer - quick solution was to develop wrapper around Select that
was called Get-Property and was using psobject (there were few other
reasons for this).

Martin
My System SpecsSystem Spec
Old 3 Weeks Ago   #5 (permalink)
Mike Pfeiffer


 
 

RE: Select object with escaped wildcard

In PowerShell v1, Select-Object -ExpandProperty only works on values that are
arrays:

So in v1 this would work, becuase the property '*' contains an array as the
value:

$array = "a","b"
$x = New-Object psobject | add-member noteproperty '*' $array -PassThru
$x | select -ExpandProperty '*'

In v2 the value does not have to be an array, so you can simply do this:

$x = New-Object psobject | add-member noteproperty '*' 'a' -PassThru
$x | select -ExpandProperty '*'

To solve your issue using v1 you can use foreach-object:

$x = New-Object psobject | add-member noteproperty '*' 'a' -PassThru
$x | ForEach-Object {$_.'*'}

"stej" wrote:
Quote:

> Hello there, I'm confused about how things work in this example:
>
> New-Object psobject| Add-Member noteproperty '*' 'a' -pass | select -
> expand '`*'
>
> I think it should work, but it doesn't. Even adding backticks doesn't
> effect the result - it still returns error '''Select-Object : Property
> "`*" cannot be found.'''
>
> (the reason why I try to solve it because I have some PSObject-s with
> automatically added properties and the property names are not
> restricted)
>
> Does anybody have the idea how to get the property (with a special
> wildcard character in its name) using select-object?
> .
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
select-object bug? PowerShell
Unable to Select-Object PowerShell
Select-Object in PS1 Script Bug?! PowerShell
Select Multi Object in (my) computer Vista General
Select object as PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46