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 - Call function with parameters that also call functions (.Net and P

Reply
 
Old 06-10-2008   #1 (permalink)
bsdz


 
 

Call function with parameters that also call functions (.Net and P

Hi

I would like to construct a parameter dynamically as I pass it to a
function. The following example demonstrates my problem. Here is a simple
concat function.

PS> function concat([string]$a, [string]$b) { return $a + $b }
PS> concat "apples" " and bananas"
apples and bananas

Clearly, if I try something like this, powershell processes each token
separated by space. So I can see why this doesn't work.

PS> concat "apples" " and {0}" -f "bananas"
apples and {0}

However, if i try the following, it appears that Powershell cannot recognise
my .Net method as a function in it own right and treats it as a group of
strings (as expected by a command line program).

PS> concat "apples" [string]::Format(" and {0}", "bananas")
apples[string]::Format

I understand I can group tokens together with braces ({}); otherwise, I am
not sure how I can do what I need to do using Powershell v1. (I would also be
interested in how to do this in v2 if different).

Many thanks in advance


My System SpecsSystem Spec
Old 06-10-2008   #2 (permalink)
Tao Ma


 
 

Re: Call function with parameters that also call functions (.Net and P

Hi bsdz,

Shay has already given solutions to help you get your work done.

You can type this in PowerShell and it will show how powershell process your
command parameters.
help about_Parsing

Tao Ma

"bsdz" <bsdz@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:1B3E175A-CF85-44A1-A89C-6622FBDD5B9D@xxxxxx
Quote:

> Hi
>
> I would like to construct a parameter dynamically as I pass it to a
> function. The following example demonstrates my problem. Here is a simple
> concat function.
>
> PS> function concat([string]$a, [string]$b) { return $a + $b }
> PS> concat "apples" " and bananas"
> apples and bananas
>
> Clearly, if I try something like this, powershell processes each token
> separated by space. So I can see why this doesn't work.
>
> PS> concat "apples" " and {0}" -f "bananas"
> apples and {0}
>
> However, if i try the following, it appears that Powershell cannot
> recognise
> my .Net method as a function in it own right and treats it as a group of
> strings (as expected by a command line program).
>
> PS> concat "apples" [string]::Format(" and {0}", "bananas")
> apples[string]::Format
>
> I understand I can group tokens together with braces ({}); otherwise, I am
> not sure how I can do what I need to do using Powershell v1. (I would also
> be
> interested in how to do this in v2 if different).
>
> Many thanks in advance
>

My System SpecsSystem Spec
Old 06-10-2008   #3 (permalink)
Shay Levi


 
 

Re: Call function with parameters that also call functions (.Net and P


To simplify the function and concat ALL passed arguments you can use:

PS 36> function concat {"$args"}
PS 37> concat apple "and banana" "and whatever"
apple and banana and whatever

# you need to enclose the format expression in ()
PS 38> concat "apples" (" and {0}" -f "bananas")
apples and bananas

PS 39> concat "apples" ([string]::Format(" and {0}", "bananas"))
apples and bananas



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi
>
> I would like to construct a parameter dynamically as I pass it to a
> function. The following example demonstrates my problem. Here is a
> simple concat function.
>
PS>> function concat([string]$a, [string]$b) { return $a + $b } concat
PS>> "apples" " and bananas"
PS>>
Quote:

> apples and bananas
>
> Clearly, if I try something like this, powershell processes each token
> separated by space. So I can see why this doesn't work.
>
PS>> concat "apples" " and {0}" -f "bananas"
PS>>
Quote:

> apples and {0}
>
> However, if i try the following, it appears that Powershell cannot
> recognise my .Net method as a function in it own right and treats it
> as a group of strings (as expected by a command line program).
>
PS>> concat "apples" [string]::Format(" and {0}", "bananas")
PS>>
Quote:

> apples[string]::Format
>
> I understand I can group tokens together with braces ({}); otherwise,
> I am not sure how I can do what I need to do using Powershell v1. (I
> would also be interested in how to do this in v2 if different).
>
> Many thanks in advance
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
how to call external exe with parameters PowerShell
How to make function call from VB.Net to C++/CLI DLL (Both areVS2005) .NET General
Call SPlus function from C#.NET language .NET General
function call from within powershell script PowerShell
How to call application with parameters 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