Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Calling parameters as a variable

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-03-2006   #1 (permalink)
Marco Shaw
Guest


 

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
Guest


 

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-03-2006   #3 (permalink)
Bruce Payette [MSFT]
Guest


 

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]
Guest


 

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
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bad Variable Type when calling via COM Colin Bowern PowerShell 11 06-24-2008 12:39 PM
Calling ID Mary Anne Vista mail 3 02-09-2008 09:04 PM
Passing parameters to a cmdlet in a variable Alastair Smith PowerShell 1 02-08-2008 06:29 AM
Passing dynamic number of parameters to a function as a variable Robin Moffatt PowerShell 10 01-30-2008 12:38 AM
How can I ensure that a variable is a built-in powershell variable? Sung M Kim PowerShell 7 09-22-2006 06:28 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51