-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathSet-SqlDscDatabaseDefaultFullTextCatalog.ps1
More file actions
187 lines (148 loc) · 7.62 KB
/
Set-SqlDscDatabaseDefaultFullTextCatalog.ps1
File metadata and controls
187 lines (148 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.SYNOPSIS
Sets the default full-text catalog for a database in a SQL Server Database Engine instance.
.DESCRIPTION
This command sets the default full-text catalog for a database in a SQL Server
Database Engine instance. The catalog name must be a valid full-text catalog
that exists in the database. The command uses the SetDefaultFullTextCatalog()
method on the SMO Database object to set the default catalog.
.PARAMETER ServerObject
Specifies current server connection object.
.PARAMETER Name
Specifies the name of the database to modify.
.PARAMETER DatabaseObject
Specifies the database object to modify (from Get-SqlDscDatabase).
.PARAMETER Refresh
Specifies that the **ServerObject**'s databases should be refreshed before
trying to get the database object. This is helpful when databases could have been
modified outside of the **ServerObject**, for example through T-SQL. But
on instances with a large amount of databases it might be better to make
sure the **ServerObject** is recent enough.
This parameter is only used when setting the default full-text catalog using
**ServerObject** and **Name** parameters.
.PARAMETER CatalogName
Specifies the name of the full-text catalog to set as the default for the database.
.PARAMETER Force
Specifies that the default full-text catalog should be modified without any confirmation.
.PARAMETER PassThru
Specifies that the database object should be returned after modification.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscDatabaseDefaultFullTextCatalog -ServerObject $serverObject -Name 'MyDatabase' -CatalogName 'MyCatalog'
Sets the default full-text catalog of the database named **MyDatabase** to **MyCatalog**.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase'
Set-SqlDscDatabaseDefaultFullTextCatalog -DatabaseObject $databaseObject -CatalogName 'MyCatalog' -Force
Sets the default full-text catalog using a database object without prompting for confirmation.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscDatabaseDefaultFullTextCatalog -ServerObject $serverObject -Name 'MyDatabase' -CatalogName 'MyCatalog' -PassThru
Sets the default full-text catalog and returns the updated database object.
.EXAMPLE
$databaseObject | Set-SqlDscDatabaseDefaultFullTextCatalog -CatalogName 'MyCatalog'
Sets the default full-text catalog using pipeline input.
.INPUTS
Microsoft.SqlServer.Management.Smo.Database
The database object to modify (from Get-SqlDscDatabase).
.OUTPUTS
None.
When PassThru is specified the output is [Microsoft.SqlServer.Management.Smo.Database].
#>
function Set-SqlDscDatabaseDefaultFullTextCatalog
{
[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.')]
[OutputType()]
[OutputType([Microsoft.SqlServer.Management.Smo.Database])]
[CmdletBinding(DefaultParameterSetName = 'ServerObjectSet', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param
(
[Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true)]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject,
[Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,
[Parameter(ParameterSetName = 'ServerObjectSet')]
[System.Management.Automation.SwitchParameter]
$Refresh,
[Parameter(ParameterSetName = 'DatabaseObjectSet', Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Database]
$DatabaseObject,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$CatalogName,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$PassThru
)
begin
{
if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
}
}
process
{
# Get the database object based on the parameter set
switch ($PSCmdlet.ParameterSetName)
{
'ServerObjectSet'
{
$previousErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$sqlDatabaseObject = $ServerObject |
Get-SqlDscDatabase -Name $Name -Refresh:($Refresh.IsPresent) -ErrorAction 'Stop'
$ErrorActionPreference = $previousErrorActionPreference
}
'DatabaseObjectSet'
{
$sqlDatabaseObject = $DatabaseObject
}
}
$verboseDescriptionMessage = $script:localizedData.DatabaseDefaultFullTextCatalog_Set_ShouldProcessVerboseDescription -f $sqlDatabaseObject.Name, $CatalogName, $sqlDatabaseObject.Parent.InstanceName
$verboseWarningMessage = $script:localizedData.DatabaseDefaultFullTextCatalog_Set_ShouldProcessVerboseWarning -f $sqlDatabaseObject.Name, $CatalogName
$captionMessage = $script:localizedData.DatabaseDefaultFullTextCatalog_Set_ShouldProcessCaption
if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
{
# Check if the default full-text catalog is already correct (idempotence)
if ($sqlDatabaseObject.DefaultFullTextCatalog -eq $CatalogName)
{
Write-Debug -Message ($script:localizedData.DatabaseDefaultFullTextCatalog_AlreadyCorrect -f $sqlDatabaseObject.Name, $CatalogName)
}
else
{
Write-Debug -Message ($script:localizedData.DatabaseDefaultFullTextCatalog_Updating -f $sqlDatabaseObject.Name, $CatalogName)
try
{
$sqlDatabaseObject.SetDefaultFullTextCatalog($CatalogName)
}
catch
{
$errorMessage = $script:localizedData.DatabaseDefaultFullTextCatalog_SetFailed -f $sqlDatabaseObject.Name, $CatalogName
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'SSDDF0004', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$sqlDatabaseObject
)
)
}
Write-Debug -Message ($script:localizedData.DatabaseDefaultFullTextCatalog_Updated -f $sqlDatabaseObject.Name, $CatalogName)
}
if ($PassThru.IsPresent)
{
# Refresh the database object to get the updated DefaultFullTextCatalog value from the server
$sqlDatabaseObject.Refresh()
return $sqlDatabaseObject
}
}
}
}