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