Skip to content

Commit c17e338

Browse files
fix: Use local scope and indexer syntax in embedded PowerShell scripts
- ConnectDbaInstanceCommand: Use $csb['Data Source'], $csb['User ID'], $csb['Initial Catalog'] instead of dotted property syntax which resolves through IDictionary indexer with wrong keyword names - InvokeDbaQueryCommand: Change all 6 InvokeScript(false) calls to InvokeScript(true) for local scope, preventing caller variable overwrites ($db optimization and null leakage) - Rename $db to $p_db in embedded param lists to avoid scope collisions Fixes: Azure DataSource keyword error, $db variable optimization error, New-DbaDatabase null return on Windows Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 20819df commit c17e338

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

project/dbatools/Commands/ConnectDbaInstanceCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,13 +1872,13 @@ private DbaInstanceParameter RewriteForServicePrincipal(DbaInstanceParameter ins
18721872
// preventing connection string injection from server name or database values.
18731873
// The password is passed via PSCredential to avoid plain string materialization in C#.
18741874
string script = @"
1875-
param($server, $db, $cred)
1875+
param($server, $p_db, $cred)
18761876
$csb = New-Object Microsoft.Data.SqlClient.SqlConnectionStringBuilder
1877-
$csb.DataSource = $server
1877+
$csb['Data Source'] = $server
18781878
$csb['Authentication'] = 'Active Directory Service Principal'
1879-
$csb.UserID = $cred.UserName
1879+
$csb['User ID'] = $cred.UserName
18801880
$csb.Password = $cred.GetNetworkCredential().Password
1881-
if ($db) { $csb.InitialCatalog = $db }
1881+
if ($p_db) { $csb['Initial Catalog'] = $p_db }
18821882
$csb.ConnectionString
18831883
";
18841884
try

project/dbatools/Commands/InvokeDbaQueryCommand.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ private object ConnectToInstance(DbaInstanceParameter instance)
465465
instanceArg = instance.ToString();
466466
}
467467

468-
script = "param($inst, $cred, $db, $appIntent, $appendConn) " +
468+
script = "param($inst, $cred, $p_db, $appIntent, $appendConn) " +
469469
"Connect-DbaInstance -SqlInstance $inst -NonPooledConnection" +
470470
" -Verbose:$false";
471471
args.Add(instanceArg);
@@ -478,7 +478,7 @@ private object ConnectToInstance(DbaInstanceParameter instance)
478478

479479
if (!String.IsNullOrEmpty(Database))
480480
{
481-
script += " -Database $db";
481+
script += " -Database $p_db";
482482
}
483483
args.Add(Database);
484484

@@ -499,7 +499,7 @@ private object ConnectToInstance(DbaInstanceParameter instance)
499499
args.Add(AppendConnectionString);
500500

501501
Collection<PSObject> results = InvokeCommand.InvokeScript(
502-
false, ScriptBlock.Create(script), null, args.ToArray());
502+
true, ScriptBlock.Create(script), null, args.ToArray());
503503

504504
if (results != null && results.Count > 0)
505505
return results[0].BaseObject;
@@ -1026,7 +1026,7 @@ private void ResolveSqlObjectInputs()
10261026
// Use Export-DbaScript -Passthru to get the SQL script
10271027
string script = "param($obj) Export-DbaScript -InputObject $obj -Passthru -EnableException";
10281028
Collection<PSObject> results = InvokeCommand.InvokeScript(
1029-
false, ScriptBlock.Create(script), null, new object[] { obj });
1029+
true, ScriptBlock.Create(script), null, new object[] { obj });
10301030

10311031
if (results == null || results.Count == 0)
10321032
{
@@ -1083,7 +1083,7 @@ private void ResolvePathAndAdd(string path)
10831083
}
10841084
";
10851085
Collection<PSObject> results = InvokeCommand.InvokeScript(
1086-
false, ScriptBlock.Create(script), null, new object[] { path });
1086+
true, ScriptBlock.Create(script), null, new object[] { path });
10871087

10881088
if (results != null)
10891089
{
@@ -1125,16 +1125,16 @@ private void DownloadFile(string url, string destPath)
11251125
private object InvokeDatabaseSwitch(object connContext, string dbName)
11261126
{
11271127
string script = @"
1128-
param($ctx, $db)
1128+
param($ctx, $p_db)
11291129
$savedTimeout = $ctx.StatementTimeout
1130-
$newCtx = $ctx.Copy().GetDatabaseConnection($db)
1130+
$newCtx = $ctx.Copy().GetDatabaseConnection($p_db)
11311131
$newCtx.StatementTimeout = $savedTimeout
11321132
$newCtx
11331133
";
11341134
try
11351135
{
11361136
Collection<PSObject> results = InvokeCommand.InvokeScript(
1137-
false, ScriptBlock.Create(script), null, new object[] { connContext, dbName });
1137+
true, ScriptBlock.Create(script), null, new object[] { connContext, dbName });
11381138
if (results != null && results.Count > 0)
11391139
return results[0].BaseObject;
11401140
}
@@ -1216,7 +1216,7 @@ private string ResolveFilePath(string path)
12161216
{
12171217
string script = "param($p) (Resolve-Path -LiteralPath $p).ProviderPath";
12181218
Collection<PSObject> results = InvokeCommand.InvokeScript(
1219-
false, ScriptBlock.Create(script), null, new object[] { path });
1219+
true, ScriptBlock.Create(script), null, new object[] { path });
12201220
if (results != null && results.Count > 0)
12211221
return results[0].ToString();
12221222
}
@@ -1244,7 +1244,7 @@ private string GetTempFilePath(string prefix, int count)
12441244
{
12451245
string script = "param() Get-DbatoolsPath -Name temp";
12461246
Collection<PSObject> results = InvokeCommand.InvokeScript(
1247-
false, ScriptBlock.Create(script), null, new object[0]);
1247+
true, ScriptBlock.Create(script), null, new object[0]);
12481248
if (results != null && results.Count > 0)
12491249
{
12501250
string tempDir = results[0].ToString();

0 commit comments

Comments
 (0)