@@ -64,7 +64,7 @@ export function buildPowerShellScript(
6464 }
6565
6666 const preambleLines : string [ ] = [ ] ;
67- const paramParts : string [ ] = [ ] ;
67+ const splatEntries : string [ ] = [ ] ;
6868
6969 for ( const [ key , value ] of Object . entries ( args ) ) {
7070 // Validate parameter key (letters, digits only)
@@ -81,30 +81,34 @@ export function buildPowerShellScript(
8181 `$__securePass = ConvertTo-SecureString '${ escapedPass } ' -AsPlainText -Force` ,
8282 `$__cred = New-Object System.Management.Automation.PSCredential('${ escapedUser } ', $__securePass)`
8383 ) ;
84- paramParts . push ( `- SqlCredential $__cred` ) ;
84+ splatEntries . push ( ` SqlCredential = $__cred` ) ;
8585 continue ;
8686 }
8787
8888 if ( typeof value === "boolean" ) {
89- if ( value ) paramParts . push ( `- ${ key } ` ) ;
89+ if ( value ) splatEntries . push ( ` ${ key } = $true ` ) ;
9090 } else if ( typeof value === "number" ) {
9191 if ( ! Number . isFinite ( value ) ) throw new Error ( `Non-finite number for -${ key } ` ) ;
92- paramParts . push ( `- ${ key } ${ value } ` ) ;
92+ splatEntries . push ( ` ${ key } = ${ value } ` ) ;
9393 } else {
9494 // Sanitize string: escape embedded single-quotes
9595 const escaped = String ( value ) . replace ( / ' / g, "''" ) ;
96- paramParts . push ( `- ${ key } '${ escaped } '` ) ;
96+ splatEntries . push ( ` ${ key } = '${ escaped } '` ) ;
9797 }
9898 }
9999
100- const invocation = [ commandName , ...paramParts ] . join ( " " ) ;
100+ const splatBlock =
101+ splatEntries . length > 0
102+ ? [ `$params = @{` , ...splatEntries , `}` ] . join ( "\n" )
103+ : `$params = @{}` ;
101104
102105 return [
103106 `Set-StrictMode -Off` ,
104107 `$ErrorActionPreference = 'Stop'` ,
105108 `Import-Module dbatools -ErrorAction Stop` ,
106109 ...preambleLines ,
107- `$result = ${ invocation } | Select-Object -First ${ maxRows } ` ,
110+ splatBlock ,
111+ `$result = ${ commandName } @params | Select-Object -First ${ maxRows } ` ,
108112 `if ($null -eq $result) { Write-Output '[]'; exit 0 }` ,
109113 `$result | ConvertTo-Json -Depth 5 -Compress` ,
110114 ] . join ( "\n" ) ;
0 commit comments