functionPublish-DbaDacPackage{<#
.SYNOPSIS
The Publish-DbaDacPackage command takes a dacpac which is the output from an SSDT project and publishes it to a database. Changing the schema to match the dacpac and also to run any scripts in the dacpac (pre/post deploy scripts).

.DESCRIPTION
Deploying a dacpac uses the DacFx which historically needed to be installed on a machine prior to use. In 2016 the DacFx was supplied by Microsoft as a nuget package (Microsoft.Data.Tools.MSBuild) and this uses that nuget package.

.PARAMETER PublishXml
Specifies the publish profile which will include options and sqlCmdVariables.

.PARAMETER Database
Specifies the name of the database being published.

.PARAMETER ConnectionString
Specifies the connection string to the database you are upgrading. This is not required if SqlInstance is specified.

.PARAMETER GenerateDeploymentScript
If this switch is enabled, the publish script will be generated.

.PARAMETER GenerateDeploymentReport
If this switch is enabled, the publish XML report will be generated.

.PARAMETER Type
Selecting the type of the export: Dacpac (default) or Bacpac.

.PARAMETER DacOption
Export options for a corresponding export type. Can be created by New-DbaDacOption -Type Dacpac | Bacpac

.PARAMETER OutputPath
Specifies the filesystem path (directory) where output files will be generated.

.PARAMETER ScriptOnly
If this switch is enabled, only the change scripts will be generated.

.PARAMETER IncludeSqlCmdVars
If this switch is enabled, SqlCmdVars in publish.xml will have their values overwritten.

.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

.PARAMETER DacFxPath
Path to the dac dll. If this is ommited, then the version of dac dll which is packaged with dbatools is used.

Does not deploy the changes, but will generate the deployment report that would be executed against WideWorldImporters.
#>[CmdletBinding(DefaultParameterSetName='Obj')]param([Alias("ServerInstance","SqlServer")][DbaInstance[]]$SqlInstance,[Alias("Credential")][PSCredential]$SqlCredential,[Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path,[Parameter(Mandatory,ParameterSetName='Xml')][string]$PublishXml,[Parameter(Mandatory,ValueFromPipelineByPropertyName)][string[]]$Database,[string[]]$ConnectionString,[parameter(ParameterSetName='Xml')][switch]$GenerateDeploymentScript,[parameter(ParameterSetName='Xml')][switch]$GenerateDeploymentReport,[Switch]$ScriptOnly,[ValidateSet('Dacpac','Bacpac')][string]$Type='Dacpac',[string]$OutputPath="$home\Documents",[switch]$IncludeSqlCmdVars,[Parameter(ParameterSetName='Obj')][object]$DacOption,[switch]$EnableException,[String]$DacFxPath)

begin{if((Test-Bound-Not-ParameterNameSqlInstance)-and(Test-Bound-Not-ParameterNameConnectionString)){Stop-Function-Message"You must specify either SqlInstance or ConnectionString."return}if($Type-eq'Dacpac'){if((Test-Bound-ParameterNameGenerateDeploymentScript)-or(Test-Bound-ParameterNameGenerateDeploymentReport)){$defaultcolumns='ComputerName','InstanceName','SqlInstance','Database','Dacpac','PublishXml','Result','DatabaseScriptPath','MasterDbScriptPath','DeploymentReport','DeployOptions','SqlCmdVariableValues'}else{$defaultcolumns='ComputerName','InstanceName','SqlInstance','Database','Dacpac','PublishXml','Result','DeployOptions','SqlCmdVariableValues'}}elseif($Type-eq'Bacpac'){if($ScriptOnly-or$GenerateDeploymentReport-or$GenerateDeploymentScript){Stop-Function-Message"ScriptOnly, GenerateDeploymentScript, and GenerateDeploymentReport cannot be used in a Bacpac scenario."-ErrorRecord$_return}$defaultcolumns='ComputerName','InstanceName','SqlInstance','Database','Bacpac','Result','DeployOptions'}

if((Test-Bound-ParameterNameScriptOnly)-and(Test-Bound-Not-ParameterNameGenerateDeploymentScript)-and(Test-Bound-Not-ParameterNameGenerateDeploymentScript)){Stop-Function-Message"You must at least one of GenerateDeploymentScript or GenerateDeploymentReport when using ScriptOnly"return}