0

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?

Padma Channal
  • 94
  • 1
  • 13

2 Answers2

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"
guiwhatsthat
  • 2,349
  • 1
  • 12
  • 24
  • 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? – Padma Channal Jun 21 '17 at 15:29
  • Set some parameters which define which function should run If($this -eq that){#Call function} – guiwhatsthat Jun 22 '17 at 05:51
0

This works:

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

Example:

powershell -command "& { mypowerScript.ps; foo2 }"
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Padma Channal
  • 94
  • 1
  • 13