PowerShell Using **DacServices** With SQLCMD Variables To Deploy A DACPAC
You should set options in the $deployOptions.SqlCommandVariableValues property. This is an updateabase Dictionary - you can't assign a new dictionary but you can update the key/value pairs inside it. For example to set a variable "MyDatabaseRef" to "Database123" use
$deployOptions.SqlCommandVariableValues.Add("MyDatabaseRef", "Database123");
The API reference is here.
I have another code snippet to share in relation to this, a method of processing multiple variables from a Powershell script argument;
param([hashtable] $SqlCmdVar)$deployOptions = New-Object Microsoft.SqlServer.Dac.DacDeployOptions# Process the Sql Command Variables#if ($SqlCmdVar -ne $null){ foreach($key in $SqlCmdVar.keys) { Write-Verbose -Message "Adding Sql Command Variable ""$key""..." $deployOptions.SqlCommandVariableValues.Add($key,$SqlCmdVar[$key]) }}
You would call the script like this;
myscript.ps1 -SqlCmdVar @{ variable1 = "my first value"; variable2 = "my second value"; variableetc = "more values"}