2016-08-05 33 views
3

我们有下面部署dacpac的功能。我一直在寻找如何传递dacpac的变量。这可能吗?用powershell部署dacpac变量替换

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" 

} 
+0

请问您是否可以详细说明dacpac的pass变量是什么意思?顺便说一下,上面的路径使用了一个非常旧的版本的DACFx。最新版本(可在此处获得:https://www.microsoft.com/en-us/download/details.aspx?id=53013)安装到SQL Server \ 130 \ DAC \ bin文件夹。 –

+0

缺少以下SqlCmd变量的值:DeploymentName。 (Microsoft.Data.Tools.Schema.Sql)我们有一个需要传递的变量 – MrBeanzy

+0

您不再需要安装SQLPackage,它们已经发布了一个包含它的NuGet包。看看这里https://www.nuget.org/packages/Microsoft.Data.Tools.Msbuild –

回答

1

你可以尝试创建部署选项,如下所示,并将它们传递作为第四个参数Deploy()功能。

$deployOptions = New-Object Microsoft.SqlServer.Dac.DacDeployOptions 
$deployOptions.SqlCommandVariableValues.Add("SqlCmdParamName", "ParamValue"); 
+1

尝试$ deployOptions.SqlCommandVariableValues而不是$ deployOptions.SqlCmdVariableValues –