Powershell sql部署脚本无法与env-variavle一起使用



我在constants.groovy文件中有以下行:

myMap = [
"TEST":["SQL_URL":"Data Source=TEST,123", "credential":"" ]
]

jenkinsfile.groovy:中的此行

bat "powershell.exe -file pipeline/powershell/deploy_sql.ps1 ${myMap[env.Environment_Type].SQL_URL}"

deploy_sql.ps1:中的这一行

$Env = $args[0]
$msbuild = Start-Process -FilePath "msbuild" -ArgumentList '"Database ServicesDatabase Services.sqlproj"','/t:deploy','/p:Configuration=Release','/p:TargetConnectionString="Data Source="+$Env+";Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"','/p:BlockOnPossibleDataLoss=True','/p:TargetDatabase="bnhp_fsdb"','-fl','-flp:logfile=msbuild.log' -wait -PassThru

但我在日志中得到了这个错误:

Deploy error Deploy72002: Unable to connect to target server '+$Env+'. Please verify the connection information such as the server name, login credentials, and firewall rules for the target server.

当我使用完整的硬编码数据源运行时,它是有效的,所以我猜powershell语法有问题

PowerShell字符串文字有两种风格:逐字可扩展

逐字字符串文字由单引号('(包围,可扩展字符串由双引号("(包围。

将受影响的字符串文字转换为可扩展字符串需要我们:

  • 将周围引号更改为"
  • 通过加倍来转义字符串中的所有文本"
  • `转义任何剩余的特殊字符(反勾号(

在您的情况下,我们将更改:

'/p:TargetConnectionString="Data Source="+$Env+";Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"'

至:

"/p:TargetConnectionString=""Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"""

最新更新