2016-08-05 39 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 –