【问题标题】:Powershell sql deploy script is not working with env variavlePowershell sql部署脚本不适用于环境变量
【发布时间】:2021-08-01 23:12:53
【问题描述】:

我在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 Services\Database 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 语法有问题

【问题讨论】:

    标签: sql-server powershell jenkins-pipeline


    【解决方案1】:

    PowerShell 字符串文字有两种形式:verbatimexpandable

    逐字字符串文字用单引号 (') 括起来,可扩展字符串用双引号 (") 括起来。

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

    • 将周围的引号更改为"
    • 通过 加倍 转义字符串中的所有文字 "
    • 使用`(反引号)转义所有剩余的特殊字符

    在你的情况下,我们会改变:

    '/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"""
    

    【讨论】:

    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 2018-09-16
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多