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 - Calling parameters as a variable

Reply
 
Old 12-03-2006   #1 (permalink)
Marco Shaw


 
 

Calling parameters as a variable

Can something like this be made to work?

PS C:\> $string="-detailed"
PS C:\> get-help get-command $string
Get-Help : A parameter cannot be found that matches parameter name
'-detailed'.
At line:1 char:9
+ get-help <<<< get-command $string
PS C:\> get-help get-command "$string"
Get-Help : A parameter cannot be found that matches parameter name
'-detailed'.
At line:1 char:9
+ get-help <<<< get-command "$string"



My System SpecsSystem Spec
Old 12-03-2006   #2 (permalink)
Maximilian Hänel


 
 

Re: Calling parameters as a variable

Hi Marco,

> Can something like this be made to work?
>
> PS C:\> $string="-detailed"
> PS C:\> get-help get-command $string


Not sure why your version does not work, but this one does:

PS> $string="-detailed"
PS> invoke-expression "get-help get-command $string"

Or if you prefer aliases:

PS> iex "get-help get-command $string"

hth

Max
My System SpecsSystem Spec
Old 12-04-2006   #3 (permalink)
Bruce Payette [MSFT]


 
 

Re: Calling parameters as a variable

I'm presuming you want to do this to propagate a switch parameter in a
function? You could use invoke-expression however PowerShell has explicit
support for exactly this scenario. Switch parameters normally don't take
arguments but if the name is specified with a colon, then they do. This
allows you to "forward" a function switch parameter to a cmdlet switch
parameter. Here's an example:

function my-help ($topic, [switch] $noisy)
{
get-help $topic -detailed: $noisy
}

In this function, the function parameter $noisy is used to control
the -detailed: switch on get-help.

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book: http://manning.com/powershell
"Marco Shaw" <marco@Znbnet.nb.ca> wrote in message
news:OjX0dv0FHHA.4688@TK2MSFTNGP04.phx.gbl...
> Can something like this be made to work?
>
> PS C:\> $string="-detailed"
> PS C:\> get-help get-command $string
> Get-Help : A parameter cannot be found that matches parameter name
> '-detailed'.
> At line:1 char:9
> + get-help <<<< get-command $string
> PS C:\> get-help get-command "$string"
> Get-Help : A parameter cannot be found that matches parameter name
> '-detailed'.
> At line:1 char:9
> + get-help <<<< get-command "$string"
>



My System SpecsSystem Spec
Old 12-04-2006   #4 (permalink)
Keith Hill [MVP]


 
 

Re: Calling parameters as a variable

"Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message
news:eGWUoa2FHHA.3468@TK2MSFTNGP04.phx.gbl...
> I'm presuming you want to do this to propagate a switch parameter in a
> function? You could use invoke-expression however PowerShell has explicit
> support for exactly this scenario. Switch parameters normally don't take
> arguments but if the name is specified with a colon, then they do. This
> allows you to "forward" a function switch parameter to a cmdlet switch
> parameter. Here's an example:
>
> function my-help ($topic, [switch] $noisy)
> {
> get-help $topic -detailed: $noisy
> }
>
> In this function, the function parameter $noisy is used to control
> the -detailed: switch on get-help.


I've said it before and I'll say it again, that's one of those little
features of PoSh that I love.

--
Keith


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can adv function take a variable # of parameters? PowerShell
Bad Variable Type when calling via COM PowerShell
Passing parameters to a cmdlet in a variable PowerShell
Passing dynamic number of parameters to a function as a variable PowerShell
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