diff --git a/functions/Get-DbaWindowsLog.ps1 b/functions/Get-DbaWindowsLog.ps1 index 2df7cd1a60..3f56f036ee 100644 --- a/functions/Get-DbaWindowsLog.ps1 +++ b/functions/Get-DbaWindowsLog.ps1 @@ -280,6 +280,7 @@ function Get-DbaWindowsLog { #region Setup Runspace [Collections.Arraylist]$RunspaceCollection = @() $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() + $defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace $RunspacePool = [RunspaceFactory]::CreateRunspacePool($InitialSessionState) $RunspacePool.SetMinRunspaces(1) | Out-Null if ($MaxThreads -gt 0) { $null = $RunspacePool.SetMaxRunspaces($MaxThreads) } @@ -303,6 +304,6 @@ function Get-DbaWindowsLog { Receive-Runspace -Wait $RunspacePool.Close() $RunspacePool.Dispose() + [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace } -} - +} \ No newline at end of file diff --git a/functions/Read-DbaBackupHeader.ps1 b/functions/Read-DbaBackupHeader.ps1 index ba885bdd80..e0ec436abb 100644 --- a/functions/Read-DbaBackupHeader.ps1 +++ b/functions/Read-DbaBackupHeader.ps1 @@ -190,6 +190,7 @@ function Read-DbaBackupHeader { #Setup initial session state $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() + $defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace #Create Runspace pool, min - 1, max - 10 sessions: there is internal SQL Server queue for the restore operations. 10 threads seem to perform best $runspacePool = [runspacefactory]::CreateRunspacePool(1, 10, $InitialSessionState, $Host) $runspacePool.Open() @@ -277,6 +278,7 @@ function Read-DbaBackupHeader { } #Close the runspace pool $runspacePool.Close() + [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace } } diff --git a/internal/functions/Invoke-DbaAsync.ps1 b/internal/functions/Invoke-DbaAsync.ps1 index c7705e1e73..44270e8463 100644 --- a/internal/functions/Invoke-DbaAsync.ps1 +++ b/internal/functions/Invoke-DbaAsync.ps1 @@ -174,8 +174,9 @@ function Invoke-DbaAsync { $ds = New-Object system.Data.DataSet $da = New-Object system.Data.SqlClient.SqlDataAdapter($cmd) - + if ($MessagesToOutput) { + $defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace $pool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS + 1) $pool.ApartmentState = "MTA" $pool.Open() @@ -227,6 +228,7 @@ function Invoke-DbaAsync { } $pool.Close() $pool.Dispose() + [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace } else { #Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller and no -MessageToOutput if ($PSBoundParameters.Verbose) { diff --git a/internal/functions/Invoke-Parallel.ps1 b/internal/functions/Invoke-Parallel.ps1 index e95784a567..3c9691aaa8 100644 --- a/internal/functions/Invoke-Parallel.ps1 +++ b/internal/functions/Invoke-Parallel.ps1 @@ -180,6 +180,8 @@ function Invoke-Parallel { [switch] $Quiet = $false ) begin { + # save default runspace + $defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace #No max queue specified? Estimate one. #We use the script scope to resolve an odd PowerShell 2 issue where MaxQueue isn't seen later in the function if ( -not $PSBoundParameters.ContainsKey('MaxQueue') ) { @@ -548,7 +550,6 @@ function Invoke-Parallel { #collect garbage [gc]::Collect() } + [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace } -} - - +} \ No newline at end of file