1

I have a PowerShell script with a list of commands defined in it. And I am trying to Create a build step in Gitlab CI when run, executes the particular script. For this I want to first Understand: How to run specific commands/functions of Powershell from Command line?

I have read Invoking a specific function from PowerShell script from C# and Calling Powershell functions from C#

The answers are specific to C#. Is there any way I can do it from the command line?

Code looks somewhat like this (myPowerScript.ps1) I have more than One function in my powershell script. It is something like:

         function foo1{
         Param($Parameter1,
         $Parameter2)#
         # Foo1 implementation
         }


         function foo2{
         Param($Parameter1,
         $Parameter2)#
         # Foo2 implementation
         }

         function foo3{
         Param($Parameter1,
         $Parameter2)#
         # Foo3 implementation
         }  

I want to invoke foo2: How do I do it? and in this case, how does PowerShell understand which function is invoked?

2
  • Just invoke powershell.exe. Commented Jun 21, 2017 at 13:51
  • The PowerShell console is a command line. Commented Jun 21, 2017 at 16:43

2 Answers 2

0

Your script "stuff.ps1":

Param($Parameter1,
      $Parameter2)

function Do-Stuff
param($Parameter1,
      $Parameter2)

#do stuff
}

Do-Stuff "Value1" "$Value2"

Run your ps script from commandline:

powershell.exe -file "stuff.ps1" "ValueforParam1" "ValueForParam2"
Sign up to request clarification or add additional context in comments.

2 Comments

I have more than One function in my powershell script. It is something like: function foo1{ Param($Parameter1, $Parameter2)# # Foo1 implementation } function foo2{ Param($Parameter1, $Parameter2)# # Foo2 implementation } function foo3{ Param($Parameter1, $Parameter2)# # Fo31 implementation } So in this case how does powershell understand which function is invoked?
Set some parameters which define which function should run If($this -eq that){#Call function}
0

This works:

powershell -command "& { script1.ps; My-Func }"

Example:

powershell -command "& { mypowerScript.ps; foo2 }"

Comments

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.