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 - Getting description from a variable

Reply
 
Old 11-10-2008   #1 (permalink)
PaulChavez


 
 

Getting description from a variable

Is there a way to get a description from a variable?

I know you can use Get-Variable to get the info, but I have a situation
where I'm running through a foreach loop and I want to get the description of
the current variable in the loop.

Set-Variable MyVar -value "hey" -description "Description of MyVar"
$MyVar | %{ #I want the description here }


My System SpecsSystem Spec
Old 11-10-2008   #2 (permalink)
Kiron


 
 

Re: Getting description from a variable

Only the variable's value/content is piped and Get-Variable needs the variable name, you can parse the command and extract the variable name.
In v2 CTP2 the [Management.Automation.PSParser]'s Tokenize() Method is also a good option to parse the command.
In this sample a RegEx extracts the variable name:

sv MyVar hey -des 'Description of MyVar'
# one-liner
$MyVar | %{(gv ($myInvocation.myCommand.definition -replace '^\$(\w+).+$','$1')).description}

# apply single line to the RegEx when the command is not a one-liner
$MyVar | %{
(gv ($myInvocation.myCommand.definition -replace '(?s)^\$(\w+).+$','$1')
).description
}

Another option is to pipe the variable name and grab the variable's properties in the ForEach-Object statement.

'myVar' | % {
$var = gv $_
$var.description
$var.value.toUpper()
}

--
Kiron
My System SpecsSystem Spec
Old 11-10-2008   #3 (permalink)
PaulChavez


 
 

Re: Getting description from a variable

Thanks. That's a little deeper than I wanted to get right now, and I realized
after a little more exploring it wouldn't be that easy and changed my logic.



"Kiron" wrote:
Quote:

> Only the variable's value/content is piped and Get-Variable needs the
> variable name, you can parse the command and extract the variable name.
> In v2 CTP2 the [Management.Automation.PSParser]'s Tokenize() Method is
> also a good option to parse the command.
> In this sample a RegEx extracts the variable name:
>
> sv MyVar hey -des 'Description of MyVar'
> # one-liner
> $MyVar | %{(gv ($myInvocation.myCommand.definition -replace
> '^\$(\w+).+$','$1')).description}
>
> # apply single line to the RegEx when the command is not a one-liner
> $MyVar | %{
> (gv ($myInvocation.myCommand.definition -replace '(?s)^\$(\w+).+$','$1')
> ).description
> }
>
> Another option is to pipe the variable name and grab the variable's
> properties in the ForEach-Object statement.
>
> 'myVar' | % {
> $var = gv $_
> $var.description
> $var.value.toUpper()
> }
>
> --
> Kiron
>
My System SpecsSystem Spec
Old 11-11-2008   #4 (permalink)
RickB


 
 

Re: Getting description from a variable

On Nov 10, 12:40*pm, PaulChavez <PaulCha...@xxxxxx>
wrote:
Quote:

> Is there a way to get a description from a variable?
>
> I know you can use Get-Variable to get the info, but I have a situation
> where I'm running through a foreach loop and I want to get the description of
> the current variable in the loop.
>
> Set-Variable MyVar -value "hey" -description "Description of MyVar"
> $MyVar | %{ #I want the description here }
get-variable myvar|%{$_.decription,$_.value,'both available here'}
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
AVI file description editing General Discussion
Re: Set Local Computer Description Vista General
how to change the boot description Vista installation & setup
Pop-Up Description Tutorials
How can I ensure that a variable is a built-in powershell variable? 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