Repository navigation
Expand file tree
/
Copy pathdbatools.Types.ps1xml
More file actions
212 lines (205 loc) 路 8.22 KB
/
Copy pathdbatools.Types.ps1xml
File metadata and controls
212 lines (205 loc) 路 8.22 KB
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
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<!--Microsoft.SqlServer.Management.Smo.Database -->
<Type>
<Name>Microsoft.SqlServer.Management.Smo.Database</Name>
<Members>
<ScriptMethod>
<Name>Query</Name>
<Script>
param (
$Query,
$AllTables = $false
)
# ExecuteWithResults does not run on a private connection: the execution manager of a database is the
# connection context of the parent server, which belongs to the caller. It issues a USE and never switches
# back, so the previous database is put back here. See #10555.
$connectionContext = $this.Parent.ConnectionContext
$previousDatabase = $connectionContext.CurrentDatabase
try {
if ($AllTables) { ($this.ExecuteWithResults($Query)).Tables }
else { ($this.ExecuteWithResults($Query)).Tables[0] }
} finally {
# The comparison is case sensitive on purpose. Database names use the collation of the instance, so
# on a case sensitive instance AppDb and appdb are two different databases, and -ne would report
# them as equal and skip the restore. It cannot restore needlessly: both sides are read from the
# same property, so the same database always spells itself the same way.
if ($previousDatabase -and $connectionContext.CurrentDatabase -cne $previousDatabase) {
$escapedDatabase = $previousDatabase.Replace("]", "]]")
try {
$null = $connectionContext.ExecuteNonQuery("USE [$escapedDatabase]")
} catch {
# Putting the database back is housekeeping and must never become the outcome of the call.
# A query that makes the previous database unreachable - taking it offline, dropping it,
# renaming it, revoking access - would otherwise throw here although it succeeded, and the
# caller would read that as "it did not run" and might do it a second time. A query that
# failed would have its own error replaced by this one, which is just as misleading.
# Write-Warning obeys the caller's $WarningPreference, and Stop turns it into a terminating
# error - which would hand the outcome of the call back to the housekeeping, the very thing
# this catch exists to prevent. Inquire stops just as dead where nobody can answer it. Only
# those two are neutralised, and only for this one call: a caller that asked for silence
# still gets silence, which passing -WarningAction Continue would have taken away.
if ($WarningPreference -notin "SilentlyContinue", "Continue") { $WarningPreference = "Continue" }
Write-Warning "The database context could not be restored to [$previousDatabase]: $($_.Exception.Message)"
}
}
}
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Invoke</Name>
<Script>
param (
$Command
)
# See the comment on Query above: this leaves the connection of the caller in this database, so the
# previous database is put back afterwards. See #10555.
$connectionContext = $this.Parent.ConnectionContext
$previousDatabase = $connectionContext.CurrentDatabase
try {
$this.ExecuteNonQuery($Command)
} finally {
# See the comment on Query above for why this is case sensitive and why a failing restore only warns.
if ($previousDatabase -and $connectionContext.CurrentDatabase -cne $previousDatabase) {
$escapedDatabase = $previousDatabase.Replace("]", "]]")
try {
$null = $connectionContext.ExecuteNonQuery("USE [$escapedDatabase]")
} catch {
# See the comment on Query above for why the warning preference is neutralised first.
if ($WarningPreference -notin "SilentlyContinue", "Continue") { $WarningPreference = "Continue" }
Write-Warning "The database context could not be restored to [$previousDatabase]: $($_.Exception.Message)"
}
}
}
</Script>
</ScriptMethod>
</Members>
</Type>
<!--Microsoft.SqlServer.Management.Smo.Server -->
<Type>
<Name>Microsoft.SqlServer.Management.Smo.Server</Name>
<Members>
<ScriptMethod>
<Name>Query</Name>
<Script>
param (
[string]$Query,
[string]$Database,
[bool]$AllTables
)
try {
if ($Database) {
# Going through the database object leaves the connection of the caller in that database, so the
# previous database is put back afterwards. This does not go through Database.Query, so it needs the
# same treatment of its own. See #10555.
$connectionContext = $this.ConnectionContext
$previousDatabase = $connectionContext.CurrentDatabase
try {
$dataSet = $this.Databases[$Database].ExecuteWithResults($Query)
} finally {
# See the comment on Database.Query for why this is case sensitive and why a failing restore only warns.
if ($previousDatabase -and $connectionContext.CurrentDatabase -cne $previousDatabase) {
$escapedDatabase = $previousDatabase.Replace("]", "]]")
try {
$null = $connectionContext.ExecuteNonQuery("USE [$escapedDatabase]")
} catch {
# See the comment on Database.Query for why the warning preference is neutralised first.
if ($WarningPreference -notin "SilentlyContinue", "Continue") { $WarningPreference = "Continue" }
Write-Warning "The database context could not be restored to [$previousDatabase]: $($_.Exception.Message)"
}
}
}
} else {
$dataSet = $this.ConnectionContext.ExecuteWithResults($Query)
}
if ($AllTables) {
$dataSet.Tables
} else {
$dataSet.Tables[0]
}
} catch {
$message = ''
$innerException = $_.Exception.InnerException
while ($innerException.InnerException) {
$message += $innerException.Message
$innerException = $innerException.InnerException
}
$message += $innerException.Message
throw $message
}
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Invoke</Name>
<Script>
param (
[string]$Command,
[string]$Database
)
try {
if ($Database) {
# See the comment on Query above: this does not go through Database.Invoke either, so the previous
# database is put back here as well. See #10555.
$connectionContext = $this.ConnectionContext
$previousDatabase = $connectionContext.CurrentDatabase
try {
$this.Databases[$Database].ExecuteNonQuery($Command)
} finally {
# See the comment on Database.Query for why this is case sensitive and why a failing restore only warns.
if ($previousDatabase -and $connectionContext.CurrentDatabase -cne $previousDatabase) {
$escapedDatabase = $previousDatabase.Replace("]", "]]")
try {
$null = $connectionContext.ExecuteNonQuery("USE [$escapedDatabase]")
} catch {
# See the comment on Database.Query for why the warning preference is neutralised first.
if ($WarningPreference -notin "SilentlyContinue", "Continue") { $WarningPreference = "Continue" }
Write-Warning "The database context could not be restored to [$previousDatabase]: $($_.Exception.Message)"
}
}
}
} else {
$this.ConnectionContext.ExecuteNonQuery($Command)
}
} catch {
$message = ''
$innerException = $_.Exception.InnerException
while ($innerException.InnerException) {
$message += $innerException.Message
$innerException = $innerException.InnerException
}
$message += $innerException.Message
throw $message
}
</Script>
</ScriptMethod>
</Members>
</Type>
<Type>
<Name>Dataplat.Dbatools.dbaSystem.DbatoolsException</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<NoteProperty>
<Name>SerializationDepth</Name>
<Value>2</Value>
</NoteProperty>
</Members>
</MemberSet>
</Members>
</Type>
<Type>
<Name>Dataplat.Dbatools.dbaSystem.DbatoolsExceptionRecord</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<NoteProperty>
<Name>SerializationDepth</Name>
<Value>2</Value>
</NoteProperty>
</Members>
</MemberSet>
</Members>
</Type>
</Types>