Skip to content

Commit dfc9e3e

Browse files
Fix .Query() and .Invoke() on server for special connections and enhance exception text (#9410)
1 parent fbf8b4b commit dfc9e3e

File tree

3 files changed

+36
-80
lines changed

3 files changed

+36
-80
lines changed

bin/type-extensions.ps1

-45
This file was deleted.

dbatools.psm1

-3
Original file line numberDiff line numberDiff line change
@@ -1069,9 +1069,6 @@ if ($option.LoadTypes -or
10691069
Write-ImportTime -Text "Updating type data"
10701070
}
10711071

1072-
Import-Command -Path "$script:PSModuleRoot/bin/type-extensions.ps1"
1073-
Write-ImportTime -Text "Loading type extensions"
1074-
10751072
$loadedModuleNames = (Get-Module sqlserver, sqlps -ErrorAction Ignore).Name
10761073
if ($loadedModuleNames -contains 'sqlserver' -or $loadedModuleNames -contains 'sqlps') {
10771074
if (Get-DbatoolsConfigValue -FullName Import.SqlpsCheck) {

xml/dbatools.Types.ps1xml

+36-32
Original file line numberDiff line numberDiff line change
@@ -36,54 +36,58 @@ $this.ExecuteNonQuery($Command)
3636
<Name>Query</Name>
3737
<Script>
3838
param (
39-
$Query,
40-
$Database,
41-
$AllTables = $false
39+
[string]$Query,
40+
[string]$Database,
41+
[bool]$AllTables
4242
)
4343

44-
if (-not $Database) {
45-
if ($this.ConnectionContext.DatabaseEngineType -eq "SqlAzureDatabase") {
46-
$Database = $this.ConnectionContext.ExecuteScalar("select db_name()")
47-
if (-not $Database) {
48-
$Database = $this.ConnectionContext.SqlConnectionObject.Database
49-
}
50-
51-
if (-not $Database) {
52-
$Database = $this.ConnectionContext.DatabaseName
53-
}
44+
try {
45+
if ($Database) {
46+
$dataSet = $this.Databases[$Database].ExecuteWithResults($Query)
47+
} else {
48+
$dataSet = $this.ConnectionContext.ExecuteWithResults($Query)
5449
}
55-
if (-not $Database) {
56-
$Database = "master"
50+
if ($AllTables) {
51+
$dataSet.Tables
52+
} else {
53+
$dataSet.Tables[0]
5754
}
55+
} catch {
56+
$message = ''
57+
$innerException = $_.Exception.InnerException
58+
while ($innerException.InnerException) {
59+
$message += $innerException.Message
60+
$innerException = $innerException.InnerException
61+
}
62+
$message += $innerException.Message
63+
throw $message
5864
}
59-
if ($AllTables) { ($this.Databases[$Database].ExecuteWithResults($Query)).Tables }
60-
else { ($this.Databases[$Database].ExecuteWithResults($Query)).Tables[0] }
6165
</Script>
6266
</ScriptMethod>
6367
<ScriptMethod>
6468
<Name>Invoke</Name>
6569
<Script>
6670
param (
67-
$Command,
68-
$Database
71+
[string]$Command,
72+
[string]$Database
6973
)
7074

71-
if (-not $Database) {
72-
if ($this.ConnectionContext.DatabaseEngineType -eq "SqlAzureDatabase") {
73-
$Database = $this.ConnectionContext.ExecuteScalar("select db_name()")
74-
if (-not $Database) {
75-
$Database = $this.ConnectionContext.SqlConnectionObject.Database
76-
}
77-
78-
if (-not $Database) {
79-
$Database = $this.ConnectionContext.DatabaseName
80-
}
75+
try {
76+
if ($Database) {
77+
$this.Databases[$Database].ExecuteNonQuery($Command)
78+
} else {
79+
$this.ConnectionContext.ExecuteNonQuery($Command)
8180
}
82-
if (-not $Database) {
83-
$Database = "master"
81+
} catch {
82+
$message = ''
83+
$innerException = $_.Exception.InnerException
84+
while ($innerException.InnerException) {
85+
$message += $innerException.Message
86+
$innerException = $innerException.InnerException
8487
}
88+
$message += $innerException.Message
89+
throw $message
8590
}
86-
$this.Databases[$Database].ExecuteNonQuery($Command)
8791
</Script>
8892
</ScriptMethod>
8993
</Members>

0 commit comments

Comments
 (0)