This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Get-DbaSqlQueryBased.ps1
147 lines (116 loc) · 5.8 KB
/
Get-DbaSqlQueryBased.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
function Get-DbaIdentityUsage {
<#
.SYNOPSIS
Displays information relating to IDENTITY seed usage. Works on SQL Server 2008 and above.
.DESCRIPTION
IDENTITY seeds have max values based off of their data type. This module will locate identity columns and report the seed usage.
.PARAMETER SqlInstance
Allows you to specify a comma separated list of servers to query.
.PARAMETER SqlCredential
Login to the target instance using alternate Windows or SQL Login Authentication. Accepts credential objects (Get-Credential).
.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 Threshold
Allows you to specify a minimum % of the seed range being utilized. This can be used to ignore seeds that have only utilized a small fraction of the range.
.PARAMETER ExcludeSystemDatabase
Allows you to suppress output on system databases
.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
Author: You, YourTwitterOrBlog
Tags: Identity
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
- License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Get-DbaIdentityUsage
.EXAMPLE
PS C:\> Get-DbaIdentityUsage -SqlInstance sql2008, sqlserver2012
Check identity seeds for servers sql2008 and sqlserver2012.
.EXAMPLE
PS C:\> Get-DbaIdentityUsage -SqlInstance sql2008 -Database TestDB
Check identity seeds on server sql2008 for only the TestDB database
.EXAMPLE
PS C:\> Get-DbaIdentityUsage -SqlInstance sql2008 -Database TestDB -Threshold 20
Check identity seeds on server sql2008 for only the TestDB database, limiting results to 20% utilization of seed range or higher
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[Alias("ServerInstance", "SqlServer", "SqlServers")]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[Alias("Databases")]
[string[]]$Database,
[string[]]$ExcludeDatabase,
[parameter(Position = 1, Mandatory = $false)]
[int]$Threshold = 0,
[parameter(Position = 2, Mandatory = $false)]
[switch]$ExcludeSystemDatabase,
[switch]$EnableException
)
begin {
$sql = "SELECT SERVERPROPERTY('MachineName') AS ComputerName,
ISNULL(SERVERPROPERTY('InstanceName'), 'MSSQLSERVER') AS InstanceName,
SERVERPROPERTY('ServerName') AS SqlInstance, etc etc from whatever"
if ($Threshold -gt 0) {
$sql += " WHERE [PercentUsed] >= " + $Threshold + " ORDER BY [PercentUsed] DESC"
}
else {
$sql += " ORDER BY [PercentUsed] DESC"
}
}
process {
foreach ($instance in $SqlInstance) {
Write-Message -Level Verbose -Message "Attempting to connect to $instance"
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10
}
catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
$dbs = $server.Databases
if ($Database) {
$dbs = $dbs | Where-Object {$Database -contains $_.Name}
}
if ($ExcludeDatabase) {
$dbs = $dbs | Where-Object Name -NotIn $ExcludeDatabase
}
if ($ExcludeSystemDatabase) {
$dbs = $dbs | Where-Object IsSystemObject -eq $false
}
foreach ($db in $dbs) {
Write-Message -Level Verbose -Message "Processing $db on $instance"
if ($db.IsAccessible -eq $false) {
Stop-Function -Message "The database $db is not accessible. Skipping database." -Continue
}
foreach ($row in $db.Query($sql)) {
if ($row.PercentUsed -eq [System.DBNull]::Value) {
continue
}
if ($row.PercentUsed -ge $threshold) {
[PSCustomObject]@{
ComputerName = $server.NetName
InstanceName = $server.ServiceName
SqlInstance = $server.DomainInstanceName
Database = $row.DatabaseName
Schema = $row.SchemaName
Table = $row.TableName
Column = $row.ColumnName
SeedValue = $row.SeedValue
IncrementValue = $row.IncrementValue
LastValue = $row.LastValue
MaxNumberRows = $row.MaxNumberRows
NumberOfUses = $row.NumberOfUses
PercentUsed = $row.PercentUsed
}
}
}
}
}
}
}