简体   繁体   中英

How to run specific commands/functions of Powershell from Command line?

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?

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"

This works:

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

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM