forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-DbaView.ps1
201 lines (161 loc) · 8.66 KB
/
Find-DbaView.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
function Find-DbaView {
<#
.SYNOPSIS
Returns all views that contain a specific case-insensitive string or regex pattern.
.DESCRIPTION
This function can either run against specific databases or all databases searching all user or user and system views.
.PARAMETER SqlInstance
The target SQL Server instance or instances. This can be a collection and receive pipeline input
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Windows and SQL Authentication supported. 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 Pattern
String pattern that you want to search for in the view text body
.PARAMETER IncludeSystemObjects
By default, system views are ignored but you can include them within the search using this parameter.
Warning - this will likely make it super slow if you run it on all databases.
.PARAMETER IncludeSystemDatabases
By default system databases are ignored but you can include them within the search using this parameter
.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: View
Author: Claudio Silva (@ClaudioESSilva)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Find-DbaView
.EXAMPLE
PS C:\> Find-DbaView -SqlInstance DEV01 -Pattern whatever
Searches all user databases views for "whatever" in the text body
.EXAMPLE
PS C:\> Find-DbaView -SqlInstance sql2016 -Pattern '\w+@\w+\.\w+'
Searches all databases for all views that contain a valid email pattern in the text body
.EXAMPLE
PS C:\> Find-DbaView -SqlInstance DEV01 -Database MyDB -Pattern 'some string' -Verbose
Searches in "mydb" database views for "some string" in the text body
.EXAMPLE
PS C:\> Find-DbaView -SqlInstance sql2016 -Database MyDB -Pattern RUNTIME -IncludeSystemObjects
Searches in "mydb" database views for "runtime" in the text body
#>
[CmdletBinding()]
param (
[parameter(Position = 0, Mandatory, ValueFromPipeline)]
[Alias("ServerInstance", "SqlServer", "SqlServers")]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[Alias("Databases")]
[object[]]$Database,
[object[]]$ExcludeDatabase,
[parameter(Mandatory)]
[string]$Pattern,
[switch]$IncludeSystemObjects,
[switch]$IncludeSystemDatabases,
[Alias('Silent')]
[switch]$EnableException
)
begin {
$sql = "SELECT OBJECT_SCHEMA_NAME(vw.object_id) as ViewSchema, vw.name, m.definition as TextBody FROM sys.sql_modules m, sys.views vw WHERE m.object_id = vw.object_id"
if (!$IncludeSystemObjects) { $sql = "$sql AND vw.is_ms_shipped = 0" }
$everyservervwcount = 0
}
process {
foreach ($Instance in $SqlInstance) {
try {
$server = Connect-SqlInstance -SqlInstance $Instance -SqlCredential $SqlCredential
} catch {
Write-Message -Level Warning -Message "Failed to connect to: $Instance"
continue
}
if ($server.versionMajor -lt 9) {
Write-Message -Level Warning -Message "This command only supports SQL Server 2005 and above."
Continue
}
if ($IncludeSystemDatabases) {
$dbs = $server.Databases | Where-Object { $_.Status -eq "normal" }
} else {
$dbs = $server.Databases | Where-Object { $_.Status -eq "normal" -and $_.IsSystemObject -eq $false }
}
if ($Database) {
$dbs = $dbs | Where-Object Name -In $Database
}
if ($ExcludeDatabase) {
$dbs = $dbs | Where-Object Name -NotIn $ExcludeDatabase
}
$totalcount = 0
$dbcount = $dbs.count
foreach ($db in $dbs) {
Write-Message -Level Verbose -Message "Searching on database $db"
# If system objects aren't needed, find view text using SQL
# This prevents SMO from having to enumerate
if (!$IncludeSystemObjects) {
Write-Message -Level Debug -Message $sql
$rows = $db.ExecuteWithResults($sql).Tables.Rows
$vwcount = 0
foreach ($row in $rows) {
$totalcount++; $vwcount++; $everyservervwcount++
$viewSchema = $row.ViewSchema
$view = $row.name
Write-Message -Level Verbose -Message "Looking in View: $viewSchema.$view TextBody for $pattern"
if ($row.TextBody -match $Pattern) {
$vw = $db.Views | Where-Object {$_.Schema -eq $viewSchema -and $_.Name -eq $view}
$viewText = $vw.TextBody.split("`n`r")
$vwTextFound = $viewText | Select-String -Pattern $Pattern | ForEach-Object { "(LineNumber: $($_.LineNumber)) $($_.ToString().Trim())" }
[PSCustomObject]@{
ComputerName = $server.ComputerName
SqlInstance = $server.ServiceName
Database = $db.Name
Schema = $vw.Schema
Name = $vw.Name
Owner = $vw.Owner
IsSystemObject = $vw.IsSystemObject
CreateDate = $vw.CreateDate
LastModified = $vw.DateLastModified
ViewTextFound = $vwTextFound -join "`n"
View = $vw
ViewFullText = $vw.TextBody
} | Select-DefaultView -ExcludeProperty View, ViewFullText
}
}
} else {
$Views = $db.Views
foreach ($vw in $Views) {
$totalcount++; $vwcount++; $everyservervwcount++
$viewSchema = $row.ViewSchema
$view = $vw.Name
Write-Message -Level Verbose -Message "Looking in View: $viewSchema.$view TextBody for $pattern"
if ($vw.TextBody -match $Pattern) {
$viewText = $vw.TextBody.split("`n`r")
$vwTextFound = $viewText | Select-String -Pattern $Pattern | ForEach-Object { "(LineNumber: $($_.LineNumber)) $($_.ToString().Trim())" }
[PSCustomObject]@{
ComputerName = $server.ComputerName
SqlInstance = $server.ServiceName
Database = $db.Name
Schema = $vw.Schema
Name = $vw.Name
Owner = $vw.Owner
IsSystemObject = $vw.IsSystemObject
CreateDate = $vw.CreateDate
LastModified = $vw.DateLastModified
ViewTextFound = $vwTextFound -join "`n"
View = $vw
ViewFullText = $vw.TextBody
} | Select-DefaultView -ExcludeProperty View, ViewFullText
}
}
}
Write-Message -Level Verbose -Message "Evaluated $vwcount views in $db"
}
Write-Message -Level Verbose -Message "Evaluated $totalcount total views in $dbcount databases"
}
}
end {
Write-Message -Level Verbose -Message "Evaluated $everyservervwcount total views"
}
}