尝试在章鱼部署中的powershell脚本中执行exe

时间:2017-04-13 20:58:52

标签: powershell octopus-deploy

我已添加了将数据库迁移到Octopus Deploy中的dev服务器的步骤。我试图创建一个PowerShell脚本来执行此操作。在此部署的Nuget包中,我的可执行文件@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_yes: ((MainActivity)getContext()).tocdu(); break; case R.id.btn_no: break; default: break; } dismiss(); } 位于migrate.exe

我写了这样的剧本:

\Resources\

但是我收到了这条消息:

  

&安培; :术语'。\ Resources \ migrate.exe --assembly   "数据库\ BIN \调试\ Database.dll" --provider sqlserver2014 --connection" data source = $ dbServer; initial   catalog = ClarkChains; user id = $ dbUser; password = $ dbPass; persist security   信息= TRUE; MultipleActiveResultSets =真" -o'不被认为是的名称   cmdlet,函数,脚本文件或可操作程序。

我正在寻找有关如何正确调用可执行文件的示例。

2 个答案:

答案 0 :(得分:2)

亚当的评论基本上是钉在它上面。

Octopus有很多内置变量,包括当前部署的包目录的变量。 系统变量上的documentation为您提供了您正在寻找的那个:Octopus.Tentacle.CurrentDeployment.PackageFilePath

正如Adam所说,如果你想在字符串中使用变量,你需要用双引号括起字符串。当您使用单引号时,PowerShell不会评估变量或表达式。

此外,由于您在$arg1$arg2中使用双引号字符串,因此需要使用反引号`来转义这些字符串中的双引号需要将参数传递给migrate.exe。

您的脚本应如下所示:

Set-Location $Octopus.Tentacle.CurrentDeployment.PackageFilePath

$dbServer = $OctopusParameters["DBServer"]
$dbUser = $OctopusParameters["DBUser"]
$dbPass = $OctopusParameters["DBPass"]

Write-Host ("Running migration $dbServer")

$CMD = ".\Resources\migrate.exe"
$arg1 = "--assembly `"Database\bin\Debug\Database.dll`" --provider sqlserver2014"
$arg2 = "--connection `"data source=$dbServer;initial catalog=MyDB;user id=$dbUser;password=$dbPass;persist security info=true;MultipleActiveResultSets=True`" -o"

& $CMD $arg1 $arg2

Write-host("Migration finished $dbServer")

(很抱歉没有保持语法突出显示,但所有转义字符都与语法混淆,因为它不支持PowerShell的语法突出显示)

答案 1 :(得分:0)

您在上面使用的结构并不正确,但看起来环境的结构不正确。看看以下内容,

如果migrate.exe文件不存在,您将收到此处发布的错误。

& : The term '.\file.exe' is not recognized as the name of a cmdlet, ......

这表示存在环境问题,而不是脚本问题。 您可以解决此问题的方法包括以下内容。

A>定义完整的路径
B个为脚本设置新位置。

E.g。如果'file.exe'在C:\ temp \ mytemp \ file.exe中,则在脚本中执行以下操作,

$CMD = 'C:\temp\mytemp\file.exe'

或者你可以继续使用你的脚本,

$CMD = '.\mytemp\file.exe'
Set-Location 'C:\temp\'

这样就可以找到该文件。总之,您需要更改控制台脚本的位置或设置文件的完整路径 如果您不想这样做,我会推荐以下内容,

$myLocation = Get-Location
Set-Location 'C:\temp\'
& $CMD $arg1 $arg2
Set-Location $myLocation.Path

这样您就可以将控制台的位置更改为您需要的位置,然后返回控制台首先设置的位置。

简单地说,请确认exe文件存在于目标位置。

祝你好运:)