![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 } |
My System Specs![]() |
![]() |
| 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 | |||