Skip to content

Commit df366aa

Browse files
Enable local InvokeScript scope; export new cmdlets
Switch InvokeCommand.InvokeScript calls to use local scope (true) across many command wrappers to preserve execution context and avoid cross-scope side effects. Update module exports in dbatools.library.psd1 to add several cmdlets (Get-DbaRunspace, Import-Command, Read-XEvent, Register-DbaMaintenanceTask, Register-DbaRunspace, Select-DbaObject, Set-DbatoolsConfig, Start-DbaRunspace, Stop-DbaRunspace, Write-Message). Load the compiled dbatools.dll from the module lib folder in dbatools.library.psm1 (after dependencies) with error handling. Add scripts/ralph-test-runner.ps1.
1 parent 5058cd7 commit df366aa

63 files changed

Lines changed: 498 additions & 212 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dbatools.library.psd1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@
8989
'Get-DbaRunningJob',
9090
'Get-DbaConnectedInstance',
9191
'Get-DbaConnection',
92+
'Get-DbaRunspace',
9293
'Get-DbatoolsChangeLog',
9394
'Get-DbatoolsConfig',
9495
'Get-DbatoolsConfigValue',
9596
'Get-DbatoolsError',
9697
'Get-DbatoolsLog',
9798
'Get-DbatoolsPath',
99+
'Import-Command',
98100
'Import-DbatoolsConfig',
99101
'Invoke-DbaAgFailover',
100102
'Invoke-DbatoolsFormatter',
@@ -117,6 +119,9 @@
117119
'New-DbatoolsSupportPackage',
118120
'New-DbaScriptingOption',
119121
'New-DbaSqlParameter',
122+
'Read-XEvent',
123+
'Register-DbaMaintenanceTask',
124+
'Register-DbaRunspace',
120125
'Remove-DbaAgDatabase',
121126
'Remove-DbaAgListener',
122127
'Remove-DbaAgReplica',
@@ -125,20 +130,25 @@
125130
'Resolve-DbaNetworkName',
126131
'Revoke-DbaAgPermission',
127132
'Resolve-DbaPath',
133+
'Select-DbaObject',
128134
'Set-DbaAgListener',
129135
'Set-DbaAgReplica',
130136
'Set-DbaAgentAlert',
131137
'Set-DbaAgentJob',
132138
'Set-DbaAvailabilityGroup',
133139
'Suspend-DbaAgDbDataMovement',
134140
'Sync-DbaAvailabilityGroup',
141+
'Set-DbatoolsConfig',
135142
'Set-DbatoolsInsecureConnection',
136143
'Set-DbatoolsPath',
144+
'Start-DbaRunspace',
145+
'Stop-DbaRunspace',
137146
'Test-DbaAgentJobOwner',
138147
'Test-DbaAgSpn',
139148
'Test-DbaAvailabilityGroup',
140149
'Test-DbaConnection',
141-
'Test-DbaPath'
150+
'Test-DbaPath',
151+
'Write-Message'
142152
)
143153
AliasesToExport = @()
144154

dbatools.library.psm1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,15 @@ foreach ($name in $names) {
328328
if ($PSVersionTable.PSEdition -ne "Core" -and $redirector) {
329329
# Store the redirector in script scope so it stays alive and can be accessed if needed
330330
$script:assemblyRedirector = $redirector
331+
}
332+
333+
# Load the main dbatools binary module (contains C# cmdlet implementations)
334+
# Must be loaded AFTER all dependency assemblies (SMO, SqlClient, etc.) are available
335+
$dbatoolsDll = [IO.Path]::Combine($script:libraryroot, "lib", "dbatools.dll")
336+
if (Test-Path $dbatoolsDll) {
337+
try {
338+
$null = Import-Module $dbatoolsDll
339+
} catch {
340+
Write-Error "Could not import dbatools binary module: $($_ | Out-String)"
341+
}
331342
}

project/dbatools/Commands/AddDbaAgDatabaseCommand.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,15 @@ private bool Step1SetSeedingMode(object server, object ag, Hashtable replicaServ
384384
{
385385
WriteMessageAtLevel(String.Format("Setting seeding mode for replica {0} to {1}.", replicaName, SeedingMode), MessageLevel.Verbose, null);
386386
object replica = GetReplicaObject(ag, replicaName);
387-
InvokeCommand.InvokeScript(false, _setSeedingModeAlter, null, new object[] { replica, SeedingMode });
387+
InvokeCommand.InvokeScript(true, _setSeedingModeAlter, null, new object[] { replica, SeedingMode });
388388

389389
if (String.Equals(SeedingMode, "Automatic", StringComparison.OrdinalIgnoreCase))
390390
{
391391
object replicaServer = replicaServerSMO[replicaName];
392392
WriteMessageAtLevel(
393393
String.Format("Setting GrantAvailabilityGroupCreateDatabasePrivilege on server {0} for Availability Group {1}.", replicaServer, AvailabilityGroup),
394394
MessageLevel.Verbose, null);
395-
InvokeCommand.InvokeScript(false, _grantAgPermission, null, new object[] { replicaServer, AvailabilityGroup });
395+
InvokeCommand.InvokeScript(true, _grantAgPermission, null, new object[] { replicaServer, AvailabilityGroup });
396396
}
397397
}
398398
catch (Exception ex)
@@ -528,7 +528,7 @@ private bool Step2BackupRestore(object server, object db, string dbName, Hashtab
528528
}
529529
}
530530

531-
InvokeCommand.InvokeScript(false, _restoreDb, null, new object[] { backups, restoreParams });
531+
InvokeCommand.InvokeScript(true, _restoreDb, null, new object[] { backups, restoreParams });
532532
}
533533
catch (Exception ex)
534534
{
@@ -749,7 +749,7 @@ private bool Step4AddSecondaries(object ag, string dbName, Hashtable replicaServ
749749
String.Format("Joining database {0} on replica {1}", dbName, replicaName),
750750
MessageLevel.Verbose, null);
751751
// NOTE: JoinAvailablityGroup() is a typo in SMO - do NOT fix
752-
InvokeCommand.InvokeScript(false, _joinAgDb, null, new object[] { replicaAgDb });
752+
InvokeCommand.InvokeScript(true, _joinAgDb, null, new object[] { replicaAgDb });
753753
}
754754
catch (Exception ex)
755755
{
@@ -1116,7 +1116,7 @@ private void RefreshObject(object obj)
11161116
{
11171117
try
11181118
{
1119-
InvokeCommand.InvokeScript(false, _refreshObj, null, new object[] { obj });
1119+
InvokeCommand.InvokeScript(true, _refreshObj, null, new object[] { obj });
11201120
}
11211121
catch (Exception)
11221122
{
@@ -1247,7 +1247,7 @@ private bool GetBoolFromScript(ScriptBlock script, object arg1, object arg2)
12471247
/// </summary>
12481248
private object InvokeScriptSingle(ScriptBlock script, params object[] args)
12491249
{
1250-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, script, null, args);
1250+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, script, null, args);
12511251
if (results != null && results.Count > 0 && results[0] != null)
12521252
return results[0].BaseObject ?? results[0];
12531253
return null;

project/dbatools/Commands/AddDbaAgListenerCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private void ProcessAvailabilityGroup(object agObj)
392392
if (result != null && result.Count > 0)
393393
{
394394
object listenerObj = result[0].BaseObject;
395-
InvokeCommand.InvokeScript(false, _invokeCreateScript, null, new object[] { listenerObj });
395+
InvokeCommand.InvokeScript(true, _invokeCreateScript, null, new object[] { listenerObj });
396396
createSucceeded = true;
397397
}
398398
}

project/dbatools/Commands/AddDbaAgReplicaCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ private void ProcessInstance(DbaInstanceParameter instance, string agName, strin
644644
WriteMessageVerbose("WSFC Cluster requires granting [NT AUTHORITY\\SYSTEM] a few things. Setting now.");
645645
try
646646
{
647-
InvokeCommand.InvokeScript(false, _wsfcPermissionsScript, null, new object[] { server });
647+
InvokeCommand.InvokeScript(true, _wsfcPermissionsScript, null, new object[] { server });
648648
}
649649
catch (Exception ex)
650650
{
@@ -684,7 +684,7 @@ private void ProcessInstance(DbaInstanceParameter instance, string agName, strin
684684
{
685685
try
686686
{
687-
InvokeCommand.InvokeScript(false, _grantSeedingScript, null,
687+
InvokeCommand.InvokeScript(true, _grantSeedingScript, null,
688688
new object[] { server, agName });
689689
}
690690
catch (Exception ex)
@@ -704,7 +704,7 @@ private void ProcessInstance(DbaInstanceParameter instance, string agName, strin
704704
{
705705
try
706706
{
707-
InvokeCommand.InvokeScript(false, _grantEndpointScript, null,
707+
InvokeCommand.InvokeScript(true, _grantEndpointScript, null,
708708
new object[] { server, serviceAccount });
709709
}
710710
catch (Exception ex)

project/dbatools/Commands/ConnectDbaInstanceCommand.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ private object RecursiveConnectWithNewCredentials(object inputObject)
947947

948948
try
949949
{
950-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_RecursiveConnect, null,
950+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_RecursiveConnect, null,
951951
serverName, Database, cred, MyInvocation.BoundParameters);
952952
if (results != null && results.Count > 0)
953953
return results[0].BaseObject;
@@ -964,7 +964,7 @@ private object CopyAndModifyContext(object inputObject)
964964
{
965965
try
966966
{
967-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_CopyContext, null,
967+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_CopyContext, null,
968968
inputObject,
969969
ApplicationIntent,
970970
NonPooledConnection.IsPresent,
@@ -987,7 +987,7 @@ private object CreateServerFromSqlConnection(object inputObject)
987987
{
988988
try
989989
{
990-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_CreateFromSqlConn, null, inputObject);
990+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_CreateFromSqlConn, null, inputObject);
991991
if (results != null && results.Count > 0)
992992
return results[0].BaseObject;
993993
}
@@ -1004,7 +1004,7 @@ private object CreateServerFromConnectionString(string connString, string server
10041004
// Mirrors PS1: SqlConnectionInfo from connection string properties, then ServerConnection -> Server
10051005
try
10061006
{
1007-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_CreateFromConnString, null,
1007+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_CreateFromConnString, null,
10081008
connString, TrustServerCertificate.IsPresent, Database);
10091009
if (results != null && results.Count > 0)
10101010
return results[0].BaseObject;
@@ -1069,7 +1069,7 @@ private object CreateServerFromString(DbaInstanceParameter instance, string serv
10691069

10701070
try
10711071
{
1072-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_CreateFromString, null,
1072+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_CreateFromString, null,
10731073
serverName,
10741074
authType,
10751075
username,
@@ -1118,7 +1118,7 @@ private static string BuildCreateServerScript()
11181118
{
11191119
return @"
11201120
param(
1121-
$serverName, $authType, $username, $sqlCredential, $accessToken,
1121+
$serverName, $authType, $username, $p_sqlCredential, $accessToken,
11221122
$appendConnectionString, $failoverPartner, $multiSubnetFailover,
11231123
$alwaysEncrypted, $applicationIntent, $clientName, $networkProtocol,
11241124
$connectTimeout, $database, $fullSmoName, $encryptConnection,
@@ -1232,7 +1232,7 @@ private static string BuildCreateServerScript()
12321232
# SecurePassword and UserName
12331233
if ($authType -in 'azure ad', 'azure sql', 'local sql') {
12341234
Write-Message -Level Debug -Message ""SecurePassword will be set""
1235-
$sqlConnectionInfo.SecurePassword = $sqlCredential.Password
1235+
$sqlConnectionInfo.SecurePassword = $p_sqlCredential.Password
12361236
Write-Message -Level Debug -Message ""UserName will be set to '$username'""
12371237
$sqlConnectionInfo.UserName = $username
12381238
}
@@ -1296,7 +1296,7 @@ private static string BuildCreateServerScript()
12961296
Write-Message -Level Debug -Message ""ConnectAsUserName will be set to '$username'""
12971297
$serverConnection.ConnectAsUserName = $username
12981298
Write-Message -Level Debug -Message ""ConnectAsUserPassword will be set""
1299-
$serverConnection.ConnectAsUserPassword = $sqlCredential.GetNetworkCredential().Password
1299+
$serverConnection.ConnectAsUserPassword = $p_sqlCredential.GetNetworkCredential().Password
13001300
}
13011301
13021302
Write-Message -Level Debug -Message ""Building Server from ServerConnection""
@@ -1390,7 +1390,7 @@ private bool ValidateConnection(object server, DbaInstanceParameter instance, bo
13901390

13911391
private void RetryWithTrustServerCertificate(object server)
13921392
{
1393-
InvokeCommand.InvokeScript(false, SB_RetryTrustCert, null, server);
1393+
InvokeCommand.InvokeScript(true, SB_RetryTrustCert, null, server);
13941394
}
13951395

13961396
#endregion Connection Validation
@@ -1576,7 +1576,7 @@ private void PostConnectionSetup(DbaInstanceParameter instance, object server, b
15761576

15771577
try
15781578
{
1579-
InvokeCommand.InvokeScript(false, SB_PostConnectionSetup, null,
1579+
InvokeCommand.InvokeScript(true, SB_PostConnectionSetup, null,
15801580
instance, server, isAzure,
15811581
Fields2000Db, Fields200xDb, Fields201xDb,
15821582
Fields2000Login, Fields200xLogin, Fields201xLogin,
@@ -1703,7 +1703,7 @@ internal string GetConfigValue(string fullName)
17031703
{
17041704
try
17051705
{
1706-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_GetConfigValue, null, fullName);
1706+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_GetConfigValue, null, fullName);
17071707
if (results != null && results.Count > 0 && results[0] != null)
17081708
{
17091709
object val = results[0].BaseObject;
@@ -1778,7 +1778,7 @@ private void ConnectContext(object server)
17781778
{
17791779
try
17801780
{
1781-
InvokeCommand.InvokeScript(false, SB_ConnectContext, null, server);
1781+
InvokeCommand.InvokeScript(true, SB_ConnectContext, null, server);
17821782
}
17831783
catch (Exception ex)
17841784
{
@@ -1790,7 +1790,7 @@ private void DisconnectServer(object server)
17901790
{
17911791
try
17921792
{
1793-
InvokeCommand.InvokeScript(false, SB_DisconnectServer, null, server);
1793+
InvokeCommand.InvokeScript(true, SB_DisconnectServer, null, server);
17941794
}
17951795
catch (Exception ex)
17961796
{
@@ -1800,7 +1800,7 @@ private void DisconnectServer(object server)
18001800

18011801
private void InvokeExecuteWithResults(object server, string sql)
18021802
{
1803-
InvokeCommand.InvokeScript(false, SB_ExecuteWithResults, null, server, sql);
1803+
InvokeCommand.InvokeScript(true, SB_ExecuteWithResults, null, server, sql);
18041804
}
18051805

18061806
private object GetSqlConnectionObject(object server)
@@ -1819,7 +1819,7 @@ private void LogMaskedConnectionString(object server)
18191819
{
18201820
try
18211821
{
1822-
InvokeCommand.InvokeScript(false, SB_LogMaskedConnStr, null, server);
1822+
InvokeCommand.InvokeScript(true, SB_LogMaskedConnStr, null, server);
18231823
}
18241824
catch { }
18251825
}
@@ -1828,7 +1828,7 @@ private void AddNoteProperty(object server, string name, object value)
18281828
{
18291829
try
18301830
{
1831-
InvokeCommand.InvokeScript(false, SB_AddNoteProperty, null, server, name, value);
1831+
InvokeCommand.InvokeScript(true, SB_AddNoteProperty, null, server, name, value);
18321832
}
18331833
catch { }
18341834
}
@@ -1837,7 +1837,7 @@ private void AddConnectionHashValue(string key, object value)
18371837
{
18381838
try
18391839
{
1840-
InvokeCommand.InvokeScript(false, SB_AddConnHashValue, null, key, value);
1840+
InvokeCommand.InvokeScript(true, SB_AddConnHashValue, null, key, value);
18411841
}
18421842
catch { }
18431843
}
@@ -1846,7 +1846,7 @@ private bool IsRunningOnCore()
18461846
{
18471847
try
18481848
{
1849-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_IsRunningOnCore, null, new object[0]);
1849+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_IsRunningOnCore, null, new object[0]);
18501850
if (results != null && results.Count > 0 && results[0] != null)
18511851
{
18521852
return results[0].BaseObject is bool val && val;
@@ -1858,7 +1858,7 @@ private bool IsRunningOnCore()
18581858

18591859
private PSObject InvokeNewDbaAzAccessToken()
18601860
{
1861-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, SB_NewAzAccessToken, null, Tenant, SqlCredential);
1861+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, SB_NewAzAccessToken, null, Tenant, SqlCredential);
18621862
if (results != null && results.Count > 0)
18631863
return results[0];
18641864
return null;
@@ -1883,7 +1883,7 @@ private DbaInstanceParameter RewriteForServicePrincipal(DbaInstanceParameter ins
18831883
";
18841884
try
18851885
{
1886-
Collection<PSObject> results = InvokeCommand.InvokeScript(false, ScriptBlock.Create(script), null,
1886+
Collection<PSObject> results = InvokeCommand.InvokeScript(true, ScriptBlock.Create(script), null,
18871887
azureServer, Database, SqlCredential);
18881888
if (results != null && results.Count > 0)
18891889
{

project/dbatools/Commands/DisableDbaAgHadrCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private void ProcessInstance(DbaInstanceParameter instance)
165165
{
166166
try
167167
{
168-
InvokeCommand.InvokeScript(false, _disableHadrScript, null,
168+
InvokeCommand.InvokeScript(true, _disableHadrScript, null,
169169
new object[] { computer, Credential, Credential != null, instanceName });
170170
}
171171
catch (Exception ex)
@@ -189,9 +189,9 @@ private void ProcessInstance(DbaInstanceParameter instance)
189189
{
190190
try
191191
{
192-
InvokeCommand.InvokeScript(false, _stopServicesScript, null,
192+
InvokeCommand.InvokeScript(true, _stopServicesScript, null,
193193
new object[] { computer, instanceName });
194-
InvokeCommand.InvokeScript(false, _startServicesScript, null,
194+
InvokeCommand.InvokeScript(true, _startServicesScript, null,
195195
new object[] { computer, instanceName });
196196
}
197197
catch (Exception ex)

project/dbatools/Commands/EnableDbaAgHadrCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private void ProcessInstance(DbaInstanceParameter instance)
168168
{
169169
try
170170
{
171-
InvokeCommand.InvokeScript(false, _enableHadrScript, null,
171+
InvokeCommand.InvokeScript(true, _enableHadrScript, null,
172172
new object[] { computer, Credential, Credential != null, instanceName });
173173
}
174174
catch (Exception ex)
@@ -192,9 +192,9 @@ private void ProcessInstance(DbaInstanceParameter instance)
192192
{
193193
try
194194
{
195-
InvokeCommand.InvokeScript(false, _stopServicesScript, null,
195+
InvokeCommand.InvokeScript(true, _stopServicesScript, null,
196196
new object[] { computer, instanceName });
197-
InvokeCommand.InvokeScript(false, _startServicesScript, null,
197+
InvokeCommand.InvokeScript(true, _startServicesScript, null,
198198
new object[] { computer, instanceName });
199199
}
200200
catch (Exception ex)

0 commit comments

Comments
 (0)