functions/Set-DbaQueryStoreConfig.ps1

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
Function Set-DbaQueryStoreConfig
{
<#
.SYNOPSIS
Configure Query Store settings for a specific or multiple databases.
  
.DESCRIPTION
Configure Query Store settings for a specific or multiple databases.
  
.PARAMETER SqlInstance
The SQL Server that you're connecting to.
 
.PARAMETER SqlCredential
SqlCredential object used to connect to the SQL Server as a different user.
 
.PARAMETER Databases
Set Query Store configuration for specific databases.
 
.PARAMETER Exclude
Set Query Store configuration for all databases on the connected server except databases entered through this parameter.
 
.PARAMETER AllDatabases
Run command against all user databases    
 
.PARAMETER State
Set the state of the Query Store. Valid options are "ReadWrite", "ReadOnly" and "Off".
 
.PARAMETER FlushInterval
Set the flush to disk interval of the Query Store in seconds.
 
.PARAMETER CollectionInterval
Set the runtime statistics collection interval of the Query Store in minutes.
 
.PARAMETER MaxSize
Set the maximum size of the Query Store in MB.
 
.PARAMETER CaptureMode
Set the query capture mode of the Query Store. Valid options are "Auto" and "All".
 
.PARAMETER CleanupMode
Set the query cleanup mode policy. Valid options are "Auto" and "Off".
 
.PARAMETER StaleQueryThreshold
Set the stale query threshold in days.
 
.PARAMETER WhatIf
Shows what would happen if the command were to run
  
.PARAMETER Confirm
Prompts for confirmation of every step. For example:
 
Are you sure you want to perform this action?
Performing the operation "Changing Desired State" on target "pubs on SQL2016\VNEXT".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
 
.NOTES
Author: Enrico van de Laar ( @evdlaar )
Tags: QueryStore
dbatools PowerShell module (https://dbatools.io)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
 
.LINK
https://dbatools.io/Set-QueryStoreConfig
 
.EXAMPLE
Set-DbaQueryStoreConfig -SqlInstance ServerA\SQL -State ReadWrite -FlushInterval 600 -CollectionInterval 10 -MaxSize 100 -CaptureMode All -CleanupMode Auto -StaleQueryThreshold 100 -AllDatabases
 
Configure the Query Store settings for all user databases in the ServerA\SQL Instance.
 
.EXAMPLE
Set-DbaQueryStoreConfig -SqlInstance ServerA\SQL -FlushInterval 600
 
Only configure the FlushInterval setting for all Query Store databases in the ServerA\SQL Instance.
 
.EXAMPLE
Set-DbaQueryStoreConfig -SqlInstance ServerA\SQL -Databases AdventureWorks -State ReadWrite -FlushInterval 600 -CollectionInterval 10 -MaxSize 100 -CaptureMode all -CleanupMode Auto -StaleQueryThreshold 100
 
Configure the Query Store settings for the AdventureWorks database in the ServerA\SQL Instance.
 
.EXAMPLE
Set-DbaQueryStoreConfig -SqlInstance ServerA\SQL -Exclude AdventureWorks -State ReadWrite -FlushInterval 600 -CollectionInterval 10 -MaxSize 100 -CaptureMode all -CleanupMode Auto -StaleQueryThreshold 100
 
Configure the Query Store settings for all user databases except the AdventureWorks database in the ServerA\SQL Instance.
  
#>

    [CmdletBinding(SupportsShouldProcess = $true)]
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [object[]]$SqlInstance,
        [PsCredential]$SqlCredential,
        [switch]$AllDatabases,
        [ValidateSet('ReadWrite', 'ReadOnly', 'Off')]
        [string[]]$State,
        [int64]$FlushInterval,
        [int64]$CollectionInterval,
        [int64]$MaxSize,
        [ValidateSet('Auto', 'All')]
        [string[]]$CaptureMode,
        [ValidateSet('Auto', 'Off')]
        [string[]]$CleanupMode,
        [int64]$StaleQueryThreshold
    )
    
    DynamicParam
    {
        if ($SqlInstance)
        {
            Get-ParamSqlDatabases -SqlServer $SqlInstance[0] -SqlCredential $SqlCredential
        }
    }
    
    BEGIN
    {
        # Convert from RuntimeDefinedParameter object to regular array
        $databases = $psboundparameters.Databases
        $exclude = $psboundparameters.Exclude
    }
    
    PROCESS
    {
        if (!$databases -and !$exclude -and !$alldatabases)
        {
            Write-Warning "You must specify databases to execute against using either -Databases, -Exclude or -AllDatabases"
            continue
        }
        
        if (!$State -and !$FlushInterval -and !$CollectionInterval -and !$MaxSize -and !$CaptureMode -and !$CleanupMode -and !$StaleQueryThreshold)
        {
            Write-Warning "You must specify something to change."
            return
        }
        
        foreach ($instance in $SqlInstance)
        {
            Write-Verbose "Connecting to $instance"
            try
            {
                $server = Connect-SqlServer -SqlServer $instance -SqlCredential $SqlCredential
                
            }
            catch
            {
                Write-Warning "Can't connect to $instance. Moving on."
                continue
            }
            
            if ($server.VersionMajor -lt 13)
            {
                
                Write-Warning "The SQL Server Instance ($instance) has a lower SQL Server version than SQL Server 2016. Skipping server."
                continue
            }
            
            # We have to exclude all the system databases since they cannot have the Query Store feature enabled
            $dbs = $server.Databases | Where-Object { $_.IsSystemObject -eq $false }
            
            if ($databases.count -gt 0)
            {
                $dbs = $dbs | Where-Object { $databases -contains $_.Name }
            }
            
            if ($exclude.count -gt 0)
            {
                $dbs = $dbs | Where-Object { $exclude -notcontains $_.Name }
            }
            
            
            foreach ($db in $dbs)
            {
                Write-Verbose "Processing $db on $instance"
                
                if ($db.IsAccessible -eq $false)
                {
                    Write-Warning "The database $db on server $instance is not accessible. Skipping database."
                    Continue
                }
                
                if ($State)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing DesiredState to $state"))
                    {
                        $db.QueryStoreOptions.DesiredState = $State
                    }
                }
                
                if ($FlushInterval)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing DataFlushIntervalInSeconds to $FlushInterval"))
                    {
                        $db.QueryStoreOptions.DataFlushIntervalInSeconds = $FlushInterval
                    }
                }
                
                if ($CollectionInterval)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing StatisticsCollectionIntervalInMinutes to $CollectionInterval"))
                    {
                        $db.QueryStoreOptions.StatisticsCollectionIntervalInMinutes = $CollectionInterval
                    }
                }
                
                if ($MaxSize)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing MaxStorageSizeInMB to $MaxSize"))
                    {
                        $db.QueryStoreOptions.MaxStorageSizeInMB = $MaxSize
                    }
                }
                
                if ($CaptureMode)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing QueryCaptureMode to $CaptureMode"))
                    {
                        $db.QueryStoreOptions.QueryCaptureMode = $CaptureMode
                    }
                }
                
                if ($CleanupMode)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing SizeBasedCleanupMode to $CleanupMode"))
                    {
                        $db.QueryStoreOptions.SizeBasedCleanupMode = $CleanupMode
                    }
                }
                
                if ($StaleQueryThreshold)
                {
                    if ($Pscmdlet.ShouldProcess("$db on $instance", "Changing StaleQueryThresholdInDays to $StaleQueryThreshold"))
                    {
                        $db.QueryStoreOptions.StaleQueryThresholdInDays = $StaleQueryThreshold
                    }
                }
                
                # Alter the Query Store Configuration
                if ($Pscmdlet.ShouldProcess("$db on $instance", "Altering Query Store configuration on database"))
                {
                    try
                    {
                        $db.QueryStoreOptions.Alter()
                        $db.Refresh()
                    }
                    catch
                    {
                        Write-Warning "Could not modify configuration. Error was: $_"
                        continue
                    }
                }
                
                
                if ($Pscmdlet.ShouldProcess("$db on $instance", "Getting results from Get-DbaQueryStoreConfig"))
                {
                    # Display resulting changes
                    Get-DbaQueryStoreConfig -SqlInstance $server -Databases $db.name -Verbose:$false
                }
            }
        }
    }
}