1
+ <#
2
+ . SYNOPSIS
3
+ Will Invoke DacPac utilising sqlpackage.exe
4
+
5
+ . DESCRIPTION
6
+ Deploy a dacpac against specified instance of SQL Server Using psobject where Property is name of variable/parameter and value is the desired value.
7
+ The 'DefaultFilePrefix' is always the value of TargetDatabase parameter.
8
+
9
+ . PARAMETER SqlServerName
10
+ Name of the SQL Server. populates /TargetServerName:
11
+
12
+ . PARAMETER TargetDatabase
13
+ Defines the target database and will also be utilised for the the /v:DefaultFilePrefix
14
+
15
+ . PARAMETER SourceFile
16
+ Full Path to the .dacpac file to be published. Populates /SourceFile:
17
+
18
+ . PARAMETER Properties
19
+ [Hashtable] Where property is name of parameter with its value.
20
+
21
+ . PARAMETER Variables
22
+ [Hashtable] Object of variables with property name of var and value is input.
23
+
24
+ . EXAMPLE
25
+ $prop = @{BlockOnPossibleDataLoss = $true; BlockWhenDriftDetected = $false; CreateNewDatabase = $false; TreatVerificationErrorsAsWarnings = $true }
26
+ $vars = @{Variable1 = "Dev"; foo="bar" }
27
+ Publish-DacPac -SqlServername 'localhost\SQLEXPRESS' -TargetDatabase "TestDb" -SourceFile "C:\Staging\TestDb\TestDb.dacpac" -Properties $prop -Variables $vars
28
+
29
+ Will publish dacpac Testdb to localhost\SQLEXPRESS.
30
+
31
+ . NOTES
32
+ Requires sqlpackage.exe to be installed on executing machine within Program Files(x86), with administrator rights on database server.
33
+ #>
34
+ function Publish-DacPac {
35
+ param (
36
+ [Parameter (Mandatory = $true , position = 0 , HelpMessage = " Name of the SQL Server Instance." , ValueFromPipeline = $false , ValueFromPipelineByPropertyName = $false )]
37
+ [string ]$SqlServerName ,
38
+
39
+ [Parameter (Mandatory = $true , position = 1 , HelpMessage = " Name of Target Database" , ValueFromPipeline = $false , ValueFromPipelineByPropertyName = $false )]
40
+ [string ] $TargetDatabase ,
41
+
42
+ [Parameter (Mandatory = $true , position = 2 , helpmessage = " Full path to Dacpac file." )]
43
+ [ValidateScript ( {
44
+ if (-not (Test-Path $_ )) {
45
+ throw " File does not exist"
46
+ }
47
+ if (-not (Test-Path $_ - pathtype leaf)) {
48
+ throw " The path argument must be a file. Folder paths are not allowed."
49
+ }
50
+ if ($_.Extension -ne ' .dacpac' ) { throw " The file specified in the path must be of type dacpac." }
51
+ return $true
52
+ })]
53
+ [System.IO.FileInfo ]$SourceFile ,
54
+
55
+ [Parameter (Mandatory = $true , position = 3 , helpmessage = " PSObject containing Parameters for the dacpac to utilise." )]
56
+ [ValidateScript ( {
57
+ if ($_.Keys.Count -le 0 ) { throw " [hashtable] 'Parameters' must have atleast one key value pair" }else { return $true }
58
+ })]
59
+ [hashtable ]$Properties ,
60
+
61
+ [Parameter (mandatory = $true , position = 4 , helpmessage = " PSObject containing variables for dacpac" )]
62
+ [ValidateScript ( {
63
+ if ($_.Keys.Count -le 0 ) { throw " [hashtable] 'Variables' must have atleast one key value pair" }else { return $true }
64
+ })]
65
+ [hashtable ]$Variables
66
+ )
67
+
68
+ begin {
69
+
70
+ # Validate all keys provided are of valid Parameters for publish action case-sensitive
71
+ $validProperties = @ (" AdditionalDeploymentContributorArguments" , " AdditionalDeploymentContributors" ,
72
+ " AllowDropBlockingAssemblies" , " AllowIncompatiblePlatform" , " AllowUnsafeRowLevelSecurityDataMovement" , " BackupDatabaseBeforeChanges" , " BlockOnPossibleDataLoss" , " BlockWhenDriftDetected" ,
73
+ " CommandTimeout" , " CommentOutSetVarDeclarations" , " CompareUsingTargetCollation" , " CreateNewDatabase" , " DeployDatabaseInSingleUserMode" , " DisableAndReenableDdlTriggers" , " DoNotAlterChangeDataCaptureObjects" ,
74
+ " DoNotAlterReplicatedObjects" , " DoNotDropObjectType" , " DoNotDropObjectTypes" , " DropConstraintsNotInSource" , " DropDmlTriggersNotInSource" , " DropExtendedPropertiesNotInSource" , " DropIndexesNotInSource" ,
75
+ " DropObjectsNotInSource" , " DropPermissionsNotInSource" , " DropRoleMembersNotInSource" , " DropStatisticsNotInSource" , " ExcludeObjectType" , " ExcludeObjectTypes" , " GenerateSmartDefaults" , " IgnoreAnsiNulls" , " IgnoreAuthorizer" , " IgnoreColumnCollation" , " IgnoreColumnOrder" ,
76
+ " IgnoreComments" , " IgnoreCryptographicProviderFilePath" , " IgnoreDdlTriggerOrder" , " IgnoreDdlTriggerState" , " IgnoreDefaultSchema" , " IgnoreDmlTriggerOrder" , " IgnoreDmlTriggerState" , " IgnoreExtendedProperties" ,
77
+ " IgnoreFileAndLogFilePath" , " IgnoreFilegroupPlacement" , " IgnoreFileSize" , " IgnoreFillFactor" , " IgnoreFullTextCatalogFilePath" , " IgnoreIdentitySeed" , " IgnoreIncrement" , " IgnoreIndexOptions" ,
78
+ " IgnoreIndexPadding" , " IgnoreKeywordCasing" , " IgnoreLockHintsOnIndexes" , " IgnoreLoginSids" , " IgnoreNotForReplication" , " IgnoreObjectPlacementOnPartitionScheme" , " IgnorePartitionSchemes" ,
79
+ " IgnorePermissions" , " IgnoreQuotedIdentifiers" , " IgnoreRoleMembership" , " IgnoreRouteLifetime" , " IgnoreSemicolonBetweenStatements" , " IgnoreTableOptions" , " IgnoreUserSettingsObjects" , " IgnoreWhitespace" ,
80
+ " IgnoreWithNocheckOnCheckConstraints" , " IgnoreWithNocheckOnForeignKeys" , " IncludeCompositeObjects" , " IncludeTransactionalScripts" , " NoAlterStatementsToChangeClrTypes" , " PopulateFilesOnFileGroups" ,
81
+ " RegisterDataTierApplication" , " RunDeploymentPlanExecutors" , " ScriptDatabaseCollation" , " ScriptDatabaseCompatibility" , " ScriptDatabaseOptions" , " ScriptDeployStateChecks" ,
82
+ " ScriptFileSize" , " ScriptNewConstraintValidation" , " ScriptRefreshModule" , " Storage" , " TreatVerificationErrorsAsWarnings" , " VerifyCollationCompatibility" , " VerifyDeployment"
83
+ )
84
+
85
+ foreach ($key in $Properties.Keys ) {
86
+ if ($validProperties -cnotcontains $key ) {
87
+ throw " Property '$ ( $key ) ' is not a valid Property for sqlpackage.exe, Do note this is case sensitive."
88
+ }
89
+ }
90
+
91
+ $exe = Get-SqlPackage
92
+ if ($null -ne $exe ) {
93
+ Set-Alias - name sqlpackage - value $exe
94
+ }
95
+ else {
96
+ throw " Unable to locate sqlpackage.exe"
97
+ }
98
+
99
+ $args = " sqlpackage /a:Publish /sf:`" $SourceFile `" /tsn:`" $SqlServerName `" /tdn:`" $TargetDatabase `" /v:`" DefaultFilePrefix`" =`" $TargetDatabase `" "
100
+ }
101
+
102
+ process {
103
+
104
+ foreach ($key in $Properties.Keys ) {
105
+ $value = $Properties [$key ]
106
+ $args += " /p:`" $key `" =`" $value `" "
107
+ }
108
+
109
+ foreach ($key in $Variables.Keys ) {
110
+ $value = $Variables [$key ]
111
+ $args += " /v:`" $key `" =`" $value `" "
112
+ }
113
+
114
+ Invoke-Expression $args
115
+ }
116
+ }
0 commit comments