tags:

views:

59

answers:

2

Hi,

I have written a powershell script which is a complete function taking parameters (e.g. function name (param) { } ) and below this is a call to the function, with the parameter.

I want to be able to call this function in its .ps1 file, passing in the parameter. How would I be able to package a call to the function via a .bat or .cmd file? I am using Powershell v2.0.

Thanks

A: 

Hi,

I believe all you have to do is name the parameters in the call to the script like the following:

powershell.exe Path\ScripName -Param1 Value1 -Param2 Value2

Param1 and Param2 are actual parameter names in the function signature.

Enjoy!

Doug
How do I call the actual function though? Will this call my function? What if I have >1 function in the same .ps1 file with the signature in terms of parameters?
dotnetdev
A: 

You should use so called "dot-sourcing" of the script and the command with more than one statement: dot-sourcing of the script + call of the function with parameters.

The test script Test-Function.ps1:

function Test-Me($param1, $param2)
{
 "1:$param1, 2:$param2"
}

The calling .bat file:

powershell ". .\Test-Function.ps1; Test-Me -Param1 'Hello world' -Param2 12345"

powershell ". .\Test-Function.ps1; Test-Me -Param1 \"Hello world\" -Param2 12345"

Notes: this is not a requirement but I would recommend enclosing the entire command text with double quotation marks escaping, if needed, inner quotation marks using CMD escape rules.

Roman Kuzmin