Skip to content

Commit 41e2f99

Browse files
Add SMO type extensions (Query/Invoke) to dbatools.library
Move the .Query() and .Invoke() ScriptMethod extensions for Microsoft.SqlServer.Management.Smo.Server and Database from dbatools to dbatools.library. These type extensions are used by 336+ callers throughout the codebase and must be available as soon as dbatools.library is imported. Also fix test runner to load $TestConfig before Pester runs, and update build script to copy xml/ directory to artifacts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent df366aa commit 41e2f99

4 files changed

Lines changed: 139 additions & 0 deletions

File tree

build/build.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ if ($v6Unsafe) {
248248
Copy-Item -Path (Join-Path $root "dbatools.library.psd1") -Destination $dbatoolsLibraryDir -Force
249249
Copy-Item -Path (Join-Path $root "dbatools.library.psm1") -Destination $dbatoolsLibraryDir -Force
250250
Copy-Item -Path (Join-Path $root "LICENSE") -Destination $dbatoolsLibraryDir -Force -ErrorAction SilentlyContinue
251+
252+
# Copy xml directory (types/format files)
253+
$xmlSrc = Join-Path $root "xml"
254+
if (Test-Path $xmlSrc) {
255+
$xmlDest = Join-Path $dbatoolsLibraryDir "xml"
256+
$null = New-Item -ItemType Directory -Path $xmlDest -Force
257+
Copy-Item -Path (Join-Path $xmlSrc "*") -Destination $xmlDest -Recurse -Force
258+
Write-Host "Copied xml directory to artifacts/dbatools.library/xml" -ForegroundColor Green
259+
}
251260
Write-Host "Copied module files to artifacts/dbatools.library" -ForegroundColor Green
252261

253262
# Copy third-party-licenses

dbatools.library.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
# but only in Full .NET because Core does something different
3535
RequiredAssemblies = @()
3636

37+
# Type files (.ps1xml) to be loaded when importing this module
38+
TypesToProcess = @('xml/dbatools.Types.ps1xml')
39+
3740
# Script module or binary module file associated with this manifest.
3841
RootModule = 'dbatools.library.psm1'
3942

scripts/ralph-test-runner.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ if (-not $SkipImport) {
117117
Write-Host "`n=== Skipping module import ===" -ForegroundColor Yellow
118118
}
119119

120+
# Set up TestConfig so Pester tests can access CommonParameters and instance info
121+
$TestConfig = Get-TestConfig
122+
Write-Host " TestConfig loaded (CommonParameters: $($TestConfig.CommonParameters.Count), Instances: $($TestConfig.InstanceSingle))" -ForegroundColor Green
123+
120124
# =============================================================================
121125
# Step 3: Resolve test files
122126
# =============================================================================

xml/dbatools.Types.ps1xml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Types>
3+
<!--Microsoft.SqlServer.Management.Smo.Database -->
4+
<Type>
5+
<Name>Microsoft.SqlServer.Management.Smo.Database</Name>
6+
<Members>
7+
<ScriptMethod>
8+
<Name>Query</Name>
9+
<Script>
10+
param (
11+
$Query,
12+
$AllTables = $false
13+
)
14+
15+
if ($AllTables) { ($this.ExecuteWithResults($Query)).Tables }
16+
else { ($this.ExecuteWithResults($Query)).Tables[0] }
17+
</Script>
18+
</ScriptMethod>
19+
<ScriptMethod>
20+
<Name>Invoke</Name>
21+
<Script>
22+
param (
23+
$Command
24+
)
25+
$this.ExecuteNonQuery($Command)
26+
</Script>
27+
</ScriptMethod>
28+
</Members>
29+
</Type>
30+
31+
<!--Microsoft.SqlServer.Management.Smo.Server -->
32+
<Type>
33+
<Name>Microsoft.SqlServer.Management.Smo.Server</Name>
34+
<Members>
35+
<ScriptMethod>
36+
<Name>Query</Name>
37+
<Script>
38+
param (
39+
[string]$Query,
40+
[string]$Database,
41+
[bool]$AllTables
42+
)
43+
44+
try {
45+
if ($Database) {
46+
$dataSet = $this.Databases[$Database].ExecuteWithResults($Query)
47+
} else {
48+
$dataSet = $this.ConnectionContext.ExecuteWithResults($Query)
49+
}
50+
if ($AllTables) {
51+
$dataSet.Tables
52+
} else {
53+
$dataSet.Tables[0]
54+
}
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
64+
}
65+
</Script>
66+
</ScriptMethod>
67+
<ScriptMethod>
68+
<Name>Invoke</Name>
69+
<Script>
70+
param (
71+
[string]$Command,
72+
[string]$Database
73+
)
74+
75+
try {
76+
if ($Database) {
77+
$this.Databases[$Database].ExecuteNonQuery($Command)
78+
} else {
79+
$this.ConnectionContext.ExecuteNonQuery($Command)
80+
}
81+
} catch {
82+
$message = ''
83+
$innerException = $_.Exception.InnerException
84+
while ($innerException.InnerException) {
85+
$message += $innerException.Message
86+
$innerException = $innerException.InnerException
87+
}
88+
$message += $innerException.Message
89+
throw $message
90+
}
91+
</Script>
92+
</ScriptMethod>
93+
</Members>
94+
</Type>
95+
<Type>
96+
<Name>Dataplat.Dbatools.dbaSystem.DbatoolsException</Name>
97+
<Members>
98+
<MemberSet>
99+
<Name>PSStandardMembers</Name>
100+
<Members>
101+
<NoteProperty>
102+
<Name>SerializationDepth</Name>
103+
<Value>2</Value>
104+
</NoteProperty>
105+
</Members>
106+
</MemberSet>
107+
</Members>
108+
</Type>
109+
<Type>
110+
<Name>Dataplat.Dbatools.dbaSystem.DbatoolsExceptionRecord</Name>
111+
<Members>
112+
<MemberSet>
113+
<Name>PSStandardMembers</Name>
114+
<Members>
115+
<NoteProperty>
116+
<Name>SerializationDepth</Name>
117+
<Value>2</Value>
118+
</NoteProperty>
119+
</Members>
120+
</MemberSet>
121+
</Members>
122+
</Type>
123+
</Types>

0 commit comments

Comments
 (0)