![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
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 |
|
|
|
#2 |
|
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 > |
|
|
|
#3 |
|
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 > |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |