TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Notices

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

Microsoft Windows Powershell


Reply
 
Thread Tools Display Modes
Old 10-06-2008, 03:49 PM   #1
bsdz
Guest
 
Posts: n/a
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

  Reply With Quote
Old 10-06-2008, 03:49 PM   #2
Shay Levi
Guest
 
Posts: n/a
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

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



  Reply With Quote
Old 10-06-2008, 03:49 PM   #3
Tao Ma
Guest
 
Posts: n/a
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@discussions.microsoft.com> 写入消息新闻:1B3E175A-CF85-44A1-A89C-6622FBDD5B9D@microsoft.com...
> 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
>



  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 11:08 AM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional