![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||