You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Invoke-DbaDbShrink - Add WAIT_AT_LOW_PRIORITY support via DBCC SHRINKFILE T-SQL
Replaces SMO $file.Shrink() calls with direct DBCC SHRINKFILE T-SQL to enable
WAIT_AT_LOW_PRIORITY support (SQL Server 2022+). Adds -WaitAtLowPriority switch
and -AbortAfterWait parameter (Self|Blockers). Includes version guard with
Stop-Function for older instances. Updates parameter validation tests.
Closes#10193
(do Invoke-DbaDbShrink)
Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@@ -202,6 +220,11 @@ function Invoke-DbaDbShrink {
202
220
}
203
221
$StatementTimeoutSeconds=$StatementTimeout*60
204
222
223
+
$walp=""
224
+
if ($WaitAtLowPriority) {
225
+
$walp=" WITH WAIT_AT_LOW_PRIORITY (ABORT_AFTER_WAIT = $($AbortAfterWait.ToUpper()))"
226
+
}
227
+
205
228
$sql='SELECT
206
229
AVG(avg_fragmentation_in_percent) AS [avg_fragmentation_in_percent]
207
230
, MAX(avg_fragmentation_in_percent) AS [max_fragmentation_in_percent]
@@ -255,6 +278,10 @@ function Invoke-DbaDbShrink {
255
278
continue
256
279
}
257
280
281
+
if ($WaitAtLowPriority-and$instance.VersionMajor-lt16) {
282
+
Stop-Function-Message "WAIT_AT_LOW_PRIORITY for DBCC SHRINKFILE requires SQL Server 2022 (version 16) or later. $instance is running version $($instance.VersionMajor)."-Target $instance-Continue
if ($spaceAvailableKB-le$desiredSpaceAvailableKB) {
283
312
Write-Message-Level Warning -Message "File size of ($startingSizeKB) is less than or equal to the desired outcome ($desiredFileSizeKB) for $($file.Name)"
284
313
} else {
@@ -313,7 +342,14 @@ function Invoke-DbaDbShrink {
313
342
$shrinkSizeKB=$desiredFileSizeKB
314
343
}
315
344
Write-Message-Level Verbose -Message ('Shrinking {0} to {1}'-f$file.Name,$shrinkSizeKB)
0 commit comments