|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Sets default objects of a database in a SQL Server Database Engine instance. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + This command sets default objects of a database in a SQL Server Database Engine instance. |
| 7 | + It can set the default filegroup, default FILESTREAM filegroup, and default Full-Text catalog |
| 8 | + using SMO methods. |
| 9 | +
|
| 10 | + .PARAMETER ServerObject |
| 11 | + Specifies current server connection object. |
| 12 | +
|
| 13 | + .PARAMETER DatabaseObject |
| 14 | + Specifies a database object to modify. |
| 15 | +
|
| 16 | + .PARAMETER Name |
| 17 | + Specifies the name of the database to be modified. |
| 18 | +
|
| 19 | + .PARAMETER DefaultFileGroup |
| 20 | + Sets the default filegroup for the database. The filegroup must exist in the database. |
| 21 | +
|
| 22 | + .PARAMETER DefaultFileStreamFileGroup |
| 23 | + Sets the default FILESTREAM filegroup for the database. The filegroup must exist in the database. |
| 24 | +
|
| 25 | + .PARAMETER DefaultFullTextCatalog |
| 26 | + Sets the default Full-Text catalog for the database. The catalog must exist in the database. |
| 27 | +
|
| 28 | + .PARAMETER Force |
| 29 | + Specifies that the database defaults should be modified without any confirmation. |
| 30 | +
|
| 31 | + .PARAMETER Refresh |
| 32 | + Specifies that the **ServerObject**'s databases should be refreshed before |
| 33 | + modifying the database object. This is helpful when databases could have been |
| 34 | + modified outside of the **ServerObject**, for example through T-SQL. But |
| 35 | + on instances with a large amount of databases it might be better to make |
| 36 | + sure the **ServerObject** is recent enough, or pass in **DatabaseObject**. |
| 37 | +
|
| 38 | + .PARAMETER PassThru |
| 39 | + Specifies that the database object should be returned after modification. |
| 40 | +
|
| 41 | + .EXAMPLE |
| 42 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 43 | + $databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase' |
| 44 | + $databaseObject | Set-SqlDscDatabaseDefault -DefaultFileGroup 'MyFileGroup' |
| 45 | +
|
| 46 | + Sets the default filegroup of the database named **MyDatabase** to **MyFileGroup**. |
| 47 | +
|
| 48 | + .EXAMPLE |
| 49 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 50 | + $serverObject | Set-SqlDscDatabaseDefault -Name 'MyDatabase' -DefaultFullTextCatalog 'MyCatalog' -Force |
| 51 | +
|
| 52 | + Sets the default Full-Text catalog of the database named **MyDatabase** to **MyCatalog** without prompting for confirmation. |
| 53 | +
|
| 54 | + .EXAMPLE |
| 55 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 56 | + $databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase' |
| 57 | + $databaseObject | Set-SqlDscDatabaseDefault -DefaultFileGroup 'DataFileGroup' -DefaultFileStreamFileGroup 'FileStreamFileGroup' -DefaultFullTextCatalog 'FTCatalog' |
| 58 | +
|
| 59 | + Sets multiple default objects for the database named **MyDatabase**. |
| 60 | +
|
| 61 | + .INPUTS |
| 62 | + Microsoft.SqlServer.Management.Smo.Server |
| 63 | + Server object accepted from the pipeline (ServerObject parameter set). |
| 64 | +
|
| 65 | + Microsoft.SqlServer.Management.Smo.Database |
| 66 | + Database object accepted from the pipeline (DatabaseObject parameter set). |
| 67 | +
|
| 68 | + .OUTPUTS |
| 69 | + None. But when **PassThru** is specified the output is `[Microsoft.SqlServer.Management.Smo.Database]`. |
| 70 | +#> |
| 71 | +function Set-SqlDscDatabaseDefault |
| 72 | +{ |
| 73 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] |
| 74 | + [OutputType([Microsoft.SqlServer.Management.Smo.Database])] |
| 75 | + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] |
| 76 | + param |
| 77 | + ( |
| 78 | + [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] |
| 79 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 80 | + $ServerObject, |
| 81 | + |
| 82 | + [Parameter(ParameterSetName = 'DatabaseObject', Mandatory = $true, ValueFromPipeline = $true)] |
| 83 | + [Microsoft.SqlServer.Management.Smo.Database] |
| 84 | + $DatabaseObject, |
| 85 | + |
| 86 | + [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)] |
| 87 | + [ValidateNotNullOrEmpty()] |
| 88 | + [System.String] |
| 89 | + $Name, |
| 90 | + |
| 91 | + [Parameter()] |
| 92 | + [ValidateNotNullOrEmpty()] |
| 93 | + [System.String] |
| 94 | + $DefaultFileGroup, |
| 95 | + |
| 96 | + [Parameter()] |
| 97 | + [ValidateNotNullOrEmpty()] |
| 98 | + [System.String] |
| 99 | + $DefaultFileStreamFileGroup, |
| 100 | + |
| 101 | + [Parameter()] |
| 102 | + [ValidateNotNullOrEmpty()] |
| 103 | + [System.String] |
| 104 | + $DefaultFullTextCatalog, |
| 105 | + |
| 106 | + [Parameter()] |
| 107 | + [System.Management.Automation.SwitchParameter] |
| 108 | + $Force, |
| 109 | + |
| 110 | + [Parameter(ParameterSetName = 'ServerObject')] |
| 111 | + [System.Management.Automation.SwitchParameter] |
| 112 | + $Refresh, |
| 113 | + |
| 114 | + [Parameter()] |
| 115 | + [System.Management.Automation.SwitchParameter] |
| 116 | + $PassThru |
| 117 | + ) |
| 118 | + |
| 119 | + process |
| 120 | + { |
| 121 | + if ($PSCmdlet.ParameterSetName -eq 'ServerObject') |
| 122 | + { |
| 123 | + # Get the database object |
| 124 | + $previousErrorActionPreference = $ErrorActionPreference |
| 125 | + $ErrorActionPreference = 'Stop' |
| 126 | + |
| 127 | + $DatabaseObject = $ServerObject | Get-SqlDscDatabase -Name $Name -Refresh:$Refresh.IsPresent -ErrorAction 'Stop' |
| 128 | + |
| 129 | + $ErrorActionPreference = $previousErrorActionPreference |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + $Name = $DatabaseObject.Name |
| 134 | + $ServerObject = $DatabaseObject.Parent |
| 135 | + } |
| 136 | + |
| 137 | + Write-Verbose -Message ($script:localizedData.DatabaseDefault_Set -f $Name, $ServerObject.InstanceName) |
| 138 | + |
| 139 | + if ($Force.IsPresent -and -not $Confirm) |
| 140 | + { |
| 141 | + $ConfirmPreference = 'None' |
| 142 | + } |
| 143 | + |
| 144 | + try |
| 145 | + { |
| 146 | + $wasUpdate = $false |
| 147 | + |
| 148 | + if ($PSBoundParameters.ContainsKey('DefaultFileGroup')) |
| 149 | + { |
| 150 | + $descriptionMessage = $script:localizedData.DatabaseDefault_SetFileGroup_ShouldProcessVerboseDescription -f $Name, $DefaultFileGroup, $ServerObject.InstanceName |
| 151 | + $confirmationMessage = $script:localizedData.DatabaseDefault_SetFileGroup_ShouldProcessVerboseWarning -f $Name, $DefaultFileGroup |
| 152 | + $captionMessage = $script:localizedData.DatabaseDefault_SetFileGroup_ShouldProcessCaption |
| 153 | + |
| 154 | + if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) |
| 155 | + { |
| 156 | + $DatabaseObject.SetDefaultFileGroup($DefaultFileGroup) |
| 157 | + $wasUpdate = $true |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if ($PSBoundParameters.ContainsKey('DefaultFileStreamFileGroup')) |
| 162 | + { |
| 163 | + $descriptionMessage = $script:localizedData.DatabaseDefault_SetFileStreamFileGroup_ShouldProcessVerboseDescription -f $Name, $DefaultFileStreamFileGroup, $ServerObject.InstanceName |
| 164 | + $confirmationMessage = $script:localizedData.DatabaseDefault_SetFileStreamFileGroup_ShouldProcessVerboseWarning -f $Name, $DefaultFileStreamFileGroup |
| 165 | + $captionMessage = $script:localizedData.DatabaseDefault_SetFileStreamFileGroup_ShouldProcessCaption |
| 166 | + |
| 167 | + if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) |
| 168 | + { |
| 169 | + $DatabaseObject.SetDefaultFileStreamFileGroup($DefaultFileStreamFileGroup) |
| 170 | + $wasUpdate = $true |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if ($PSBoundParameters.ContainsKey('DefaultFullTextCatalog')) |
| 175 | + { |
| 176 | + $descriptionMessage = $script:localizedData.DatabaseDefault_SetFullTextCatalog_ShouldProcessVerboseDescription -f $Name, $DefaultFullTextCatalog, $ServerObject.InstanceName |
| 177 | + $confirmationMessage = $script:localizedData.DatabaseDefault_SetFullTextCatalog_ShouldProcessVerboseWarning -f $Name, $DefaultFullTextCatalog |
| 178 | + $captionMessage = $script:localizedData.DatabaseDefault_SetFullTextCatalog_ShouldProcessCaption |
| 179 | + |
| 180 | + if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) |
| 181 | + { |
| 182 | + $DatabaseObject.SetDefaultFullTextCatalog($DefaultFullTextCatalog) |
| 183 | + $wasUpdate = $true |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if ($wasUpdate) |
| 188 | + { |
| 189 | + Write-Verbose -Message ($script:localizedData.DatabaseDefault_Updated -f $Name) |
| 190 | + } |
| 191 | + |
| 192 | + if ($PassThru.IsPresent) |
| 193 | + { |
| 194 | + return $DatabaseObject |
| 195 | + } |
| 196 | + } |
| 197 | + catch |
| 198 | + { |
| 199 | + $errorMessage = $script:localizedData.DatabaseDefault_SetFailed -f $Name, $ServerObject.InstanceName |
| 200 | + |
| 201 | + $exception = [System.InvalidOperationException]::new($errorMessage, $_.Exception) |
| 202 | + |
| 203 | + $PSCmdlet.ThrowTerminatingError( |
| 204 | + [System.Management.Automation.ErrorRecord]::new( |
| 205 | + $exception, |
| 206 | + 'SSDDD0003', # cSpell: disable-line |
| 207 | + [System.Management.Automation.ErrorCategory]::InvalidOperation, |
| 208 | + $Name |
| 209 | + ) |
| 210 | + ) |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments