Skip to content

Commit

Permalink
Merge pull request dataplat#2820 from wsmelton/improve-FindDbaDatabas…
Browse files Browse the repository at this point in the history
…eGrowthEvent

Add filters to Find-DbaDatabaseGrowthEvent
  • Loading branch information
potatoqualitee authored Dec 11, 2017
2 parents 4d2f3dd + 2dc2e8a commit f266b28
Showing 1 changed file with 88 additions and 22 deletions.
110 changes: 88 additions & 22 deletions functions/Find-DbaDatabaseGrowthEvent.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#
function Find-DbaDatabaseGrowthEvent {
<#
.SYNOPSIS
Expand All @@ -6,25 +7,47 @@ function Find-DbaDatabaseGrowthEvent {
.DESCRIPTION
Finds any database AutoGrow events in the Default Trace.
The following events are included:
92 - Data File Auto Grow
93 - Log File Auto Grow
94 - Data File Auto Shrink
95 - Log File Auto Shrink
.PARAMETER SqlInstance
The SQL Server that you're connecting to.
SQL Server name or SMO object representing the SQL Server to connect to. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.
.PARAMETER SqlCredential
SqlCredential object used to connect to the SQL Server as a different user.
Allows you to login to servers using SQL Logins instead of Windows Authentication (AKA Integrated or Trusted). To use:
$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.
Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
To connect as a different Windows user, run PowerShell as that user.
.PARAMETER Database
The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed.
.PARAMETER ExcludeDatabase
The database(s) to exclude - this list is auto-populated from the server
.PARAMETER EventType
Provide a filter on growth event type to filter the results.
Allowed values: Growth, Shrink
.PARAMETER FileType
Provide a filter on file type to filter the results.
Allowed vaules: Data, Log
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: AutoGrow
Tags: AutoGrow,Growth,Database
Author: Aaron Nelson
Query Extracted from SQL Server Management Studio (SSMS) 2016.
Expand All @@ -36,34 +59,79 @@ function Find-DbaDatabaseGrowthEvent {
https://dbatools.io/Find-DbaDatabaseGrowthEvent
.EXAMPLE
Find-DBADatabaseGrowthEvent -SqlInstance localhost
Find-DbaDatabaseGrowthEvent -SqlInstance localhost
Returns any database AutoGrow events in the Default Trace for every database on the localhost instance.
.EXAMPLE
Find-DBADatabaseGrowthEvent -SqlInstance ServerA\SQL2016, ServerA\SQL2014
Find-DbaDatabaseGrowthEvent -SqlInstance ServerA\SQL2016, ServerA\SQL2014
Returns any database AutoGrow events in the Default Traces for every database on ServerA\sql2016 & ServerA\SQL2014.
.EXAMPLE
Find-DBADatabaseGrowthEvent -SqlInstance ServerA\SQL2016 | Format-Table -AutoSize -Wrap
Find-DbaDatabaseGrowthEvent -SqlInstance ServerA\SQL2016 | Format-Table -AutoSize -Wrap
Returns any database AutoGrow events in the Default Trace for every database on the ServerA\SQL2016 instance in a table format.
.EXAMPLE
Find-DbaDatabaseGrowthEvent -SqlInstance ServerA\SQL2016 -EventType Shrink
Returns any database Auto Shrink events in the Default Trace for every database on the ServerA\SQL2016 instance.
.EXAMPLE
Find-DbaDatabaseGrowthEvent -SqlInstance ServerA\SQL2016 -EventType Growth -FileType Data
Returns any database Auto Growth events on data files in the Default Trace for every database on the ServerA\SQL2016 instance.
#>
[CmdletBinding()]
Param (
param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlServer")]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]
$SqlCredential,
[DbaInstance[]]$SqlInstance,
[PSCredential]$SqlCredential,
[Alias("Databases")]
[object[]]$Database,
[object[]]$ExcludeDatabase,
[ValidateSet('Growth', 'Shrink')]
[string]$EventType,
[ValidateSet('Data', 'Log')]
[string]$FileType,
[switch][Alias('Silent')]$EnableException
)

begin {
$eventClass = New-Object System.Collections.ArrayList
92..95 | ForEach-Object { $null = $eventClass.Add($_) }

if (Test-Bound 'EventType', 'FileType') {
switch ($FileType) {
'Data' {
<# should only contain events for data: 92 (grow), 94 (shrink) #>
$eventClass.Remove(93)
$eventClass.Remove(95)
}
'Log' {
<# should only contain events for log: 93 (grow), 95 (shrink) #>
$eventClass.Remove(92)
$eventClass.Remove(94)
}
}
switch ($EventType) {
'Growth' {
<# should only contain events for growth: 92 (data), 93 (log) #>
$eventClass.Remove(94)
$eventClass.Remove(95)
}
'Shrink' {
<# should only contain events for shrink: 94 (data), 95 (log) #>
$eventClass.Remove(92)
$eventClass.Remove(93)
}
}
}

$eventClassFilter = $eventClass -join ","

$sql = "
BEGIN TRY
IF (SELECT CONVERT(INT,[value_in_use]) FROM sys.configurations WHERE [name] = 'default trace enabled' ) = 1
Expand Down Expand Up @@ -95,8 +163,7 @@ function Find-DbaDatabaseGrowthEvent {
([IntegerData]*8.0/1024) AS ChangeInSize
FROM::fn_trace_gettable( @base_tracefilename, DEFAULT )
WHERE
[EventClass] >= 92
AND [EventClass] <= 95
[EventClass] IN ($eventClassFilter)
AND [ServerName] = @@SERVERNAME
AND [DatabaseName] IN (_DatabaseList_)
ORDER BY [StartTime] DESC;
Expand Down Expand Up @@ -134,35 +201,34 @@ function Find-DbaDatabaseGrowthEvent {
}
process {
foreach ($instance in $SqlInstance) {
Write-Message -Level Verbose -Message "Connecting to $instance"
Write-Message -Level Verbose -Message "Attempting to connect to $instance"
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
}
catch {
Write-Message -Level Warning -Message "Can't connect to $instance. Moving on."
continue
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}

$dbs = $server.Databases

if ($Database) {
$dbs = $dbs | Where-Object Name -in $Database
$dbs = $dbs | Where-Object Name -In $Database
}

if ($ExcludeDatabase) {
$dbs = $dbs | Where-Object Name -notin $ExcludeDatabase
$dbs = $dbs | Where-Object Name -NotIn $ExcludeDatabase
}

#Create dblist name in 'bd1', 'db2' format
$dbsList = "'$($($dbs | ForEach-Object {$_.Name}) -join "','")'"
Write-Message -Level Verbose -Message "Executing query against $dbsList on $instance"

$sql = $sql -replace '_DatabaseList_', $dbsList
Write-Message -Level Debug -Message $sql
Write-Message -Level Debug -Message "Executing SQL Statement:`n $sql"

$props = 'ComputerName', 'InstanceName', 'SqlInstance', 'EventClass', 'DatabaseName', 'Filename', 'Duration', 'StartTime', 'EndTime', 'ChangeInSize'
$defaults = 'ComputerName', 'InstanceName', 'SqlInstance', 'EventClass', 'DatabaseName', 'Filename', 'Duration', 'StartTime', 'EndTime', 'ChangeInSize'

Select-DefaultView -InputObject $server.Query($sql) -Property $props
Select-DefaultView -InputObject $server.Query($sql) -Property $defaults
}
}
}
Expand Down

0 comments on commit f266b28

Please sign in to comment.