2017-06-21 186 views
0

我有一个PowerShell脚本,并在其中定义了一个命令列表。 我试图在运行时在Gitlab CI中创建一个构建步骤,执行特定的脚本。 为此,我想先了解: 如何从命令行运行Powershell的特定命令/函数?如何从命令行运行Powershell的特定命令/函数?

我已阅读 Invoking a specific function from PowerShell script from C#Calling Powershell functions from C#

的答案是特定于C#。 有没有什么办法可以从命令行执行?

代码看起来有点像这样(myPowerScript.ps1) 我的powershell脚本中有多个函数。它是这样的:

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


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

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

我想调用foo2:我​​该怎么做?在这种情况下,PowerShell如何理解哪个函数被调用?

+0

只需调用[powershell.exe](https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help)。 –

+0

PowerShell控制台_is_命令行。 –

回答

0

你的剧本 “stuff.ps1”:

Param($Parameter1, 
     $Parameter2) 

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

#do stuff 
} 

Do-Stuff "Value1" "$Value2" 

从从命令行运行你的ps脚本:

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

我的powershell脚本中有多个函数。它是这样的: 功能foo1 { 参数($参数1, $参数2)# #Foo1实施 } 功能foo2的{ 参数($参数1, $参数2)# #foo2的实施 } function foo3 Param($ Parameter1, $ Parameter2)# #Fo31 implementation }所以在这种情况下,powershell如何理解哪个函数被调用? –

+0

设置一些参数来定义哪个函数应该运行If($ this -eq that){#Call function} – guiwhatsthat

0

这工作:

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

例子:

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