简体   繁体   English

如何从命令行运行Powershell的特定命令/功能?

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

I have a PowerShell script with a list of commands defined in it. 我有一个PowerShell脚本,其中包含定义的命令列表。 And I am trying to Create a build step in Gitlab CI when run, executes the particular script. 我试图在运行时在Gitlab CI中创建一个构建步骤,执行特定的脚本。 For this I want to first Understand: How to run specific commands/functions of Powershell from Command line? 为此,我首先要了解:如何从命令行运行Powershell的特定命令/功能?

I have read Invoking a specific function from PowerShell script from C# and Calling Powershell functions from C# 我已经阅读了从C#的PowerShell脚本调用特定函数和从C#的 调用Powershell函数

The answers are specific to C#. 答案是特定于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. 代码看起来像这样(myPowerScript.ps1)我的Powershell脚本中有多个功能。 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? 我想调用foo2:我​​该怎么做? and in this case, how does PowerShell understand which function is invoked? 在这种情况下,PowerShell如何理解调用了哪个函数?

Your script "stuff.ps1": 您的脚本“ stuff.ps1”:

Param($Parameter1,
      $Parameter2)

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

#do stuff
}

Do-Stuff "Value1" "$Value2"

Run your ps script from commandline: 从命令行运行ps脚本:

powershell.exe -file "stuff.ps1" "ValueforParam1" "ValueForParam2"

This works: 这有效:

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

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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