2

Is there a way for a batch file to call a powershell function? I've tried

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

And it works, the function gets called, but so does everything else in the powershell script.   

2 Answers 2

3

It looks like what you're trying to do is dot-source tes.ps1 so that you can use the Test-Me function that's defined in that file. When you dot-source a file, everything in that file gets executed. If you have other commands in tes.ps1 that you don't want to execute, then you'll need to put Test-Me in a separate file. The best way to do that: Create a file called Test-Me.ps1 that contains the contents of the function (don't declare a function with function { [...] }, just include the code inside the function's scriptblock), then invoke it like this in your batch file:

powershell "<path>\Test-Me.ps1 -Param1 'Hello world' -Param2 12345"
Sign up to request clarification or add additional context in comments.

1 Comment

Passing the whole command line as a single string to powershell should work, but I'd prefer using the -File option: powershell -File "C:\path\to\Test-Me.ps1" -Param1 "Hello world!" -Param2 12345.
0

@Adi Inbar 's solution works perfect.

Here is another way (more from structuring standpoint): If tes.ps1 only contains functions, create a brand new script with following contents:

.\tes.ps1
Test-Me -Param1 'Hello world' -Param2 12345

Then in batch file, run this brand new script with the "file" parameter:

Powershell -file *path_to_PS_script*

The tes.ps1 run first it will load all functions in the script scope. You can pick which function to run.

In this way tes.ps1 file serves as a central library of functions.

5 Comments

I'm inferring that tes.ps1 doesn't just contain functions, because he says that "everything else in the powershell script" also gets called. If it contained only functions, then dot-sourcing and calling the function as he's doing would work fine.
@Adi Inbar What I got from the question is that he/she only want to call the specific function and doesn't want "antything else gets called".
Right. And if it were all function definitions, nothing would get called except functions he invokes explicitly after dot-sourcing. If, as he says, other things from the script file are executed when he dot-sources it, then it's not all function definitions.
I guess it is not clear when he says "but so does everything else in the powershell script.". If my guess is right, it means: 1.variables being "loaded" to the session. 2. Other functions being "loaded" to the session. When you dot-sourcing a script, this is true that all variables and functions in the script will be "loaded" although they might not be "called" specifically. I have a feeling that he/she takes "being loaded" as "executed" but it is a assumption. @user2526047 if you can please clarify this.
@Adi you are right. I was under the impression that doc-sourcing only load the variable/function to the environment. No it "execute everything" in the script. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.