Sql server 使用powershell部署带有变量替换的dacpac
我们有下面部署dacpac的功能。我一直在寻找如何为dacpac传递变量。这可能吗Sql server 使用powershell部署带有变量替换的dacpac,sql-server,powershell,dacpac,sql-server-data-tools,Sql Server,Powershell,Dacpac,Sql Server Data Tools,我们有下面部署dacpac的功能。我一直在寻找如何为dacpac传递变量。这可能吗 Function DeployDB { param( [string]$SqlServerName = $( throw "Missing required parameter SqlServerName"), [string]$SqlServerUserName = $( throw "Missing required parameter SqlServerUserName"),
Function DeployDB {
param(
[string]$SqlServerName = $( throw "Missing required parameter SqlServerName"),
[string]$SqlServerUserName = $( throw "Missing required parameter SqlServerUserName"),
[string]$SqlServerPassword = $( throw "Missing required parameter SqlServerPassword"),
[string]$dacpac = $( throw "Missing required parameter dacpac"),
[string]$dbname = $( throw "Missing required parameter dbname")
)
Write-Host "Deploying the DB with the following settings"
Write-Host "Server Name: $SqlServerName"
Write-Host "DACPAC: $dacpac"
Write-Host "Name: $dbname"
# load in DAC DLL, This requires config file to support .NET 4.0.
# change file location for a 32-bit OS
#make sure you
add-type -path "C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\Microsoft.SqlServer.Dac.dll"
# Create a DacServices object, which needs a connection string
$dacsvcs = new-object Microsoft.SqlServer.Dac.DacServices "server=$SqlServerName;User ID=$SqlServerUserName;Password=$SqlServerPassword;"
# register event. For info on this cmdlet, see http://technet.microsoft.com/en-us/library/hh849929.aspx
register-objectevent -in $dacsvcs -eventname Message -source "msg" -action { out-host -in $Event.SourceArgs[1].Message.Message } | Out-Null
# Load dacpac from file & deploy database
$dp = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpac)
$dacsvcs.Deploy($dp, $dbname, $true)
# clean up event
unregister-event -source "msg"
}
您可以尝试如下创建部署选项,并将它们作为第四个参数传递给
deploy()
函数
$deployOptions = New-Object Microsoft.SqlServer.Dac.DacDeployOptions
$deployOptions.SqlCommandVariableValues.Add("SqlCmdParamName", "ParamValue");
您能详细说明一下dacpac的pass变量是什么意思吗?顺便说一下,上面的路径使用了非常旧的DACFx版本。最新版本(此处提供:)安装到SQL Server\130\DAC\bin文件夹。缺少以下SqlCmd变量的值:DeploymentName。(Microsoft.Data.Tools.Schema.Sql)我们有一个需要传递的变量。您不再需要安装SQLPackage。他们发布了一个包含该变量的NuGet包。请查看此处,尝试使用$deployOptions.SqlCommandVariableValues而不是$deployOptions.SqlCmdVariableValues