From 4d710e53a20c0719c9fc427f3a57abbe08b14b2b Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Sat, 10 Nov 2018 00:56:04 +0100 Subject: [PATCH 1/3] fixes #3860 --- internal/functions/Invoke-DbaAsync.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/functions/Invoke-DbaAsync.ps1 b/internal/functions/Invoke-DbaAsync.ps1 index c7705e1e73..ca3e04bb09 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) { + $save = [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 = $save } 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) { From 4864b7db941a9478a03456240a65338fe05225b4 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Sat, 10 Nov 2018 01:11:11 +0100 Subject: [PATCH 2/3] prevented breaking runspaces --- functions/Get-DbaWindowsLog.ps1 | 5 +++-- functions/Read-DbaBackupHeader.ps1 | 2 ++ internal/functions/Invoke-Parallel.ps1 | 7 ++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/functions/Get-DbaWindowsLog.ps1 b/functions/Get-DbaWindowsLog.ps1 index 2df7cd1a60..e31d1e9781 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() + $save = [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 = $save } -} - +} \ No newline at end of file diff --git a/functions/Read-DbaBackupHeader.ps1 b/functions/Read-DbaBackupHeader.ps1 index ba885bdd80..64feaeb33c 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() + $save = [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 = $save } } diff --git a/internal/functions/Invoke-Parallel.ps1 b/internal/functions/Invoke-Parallel.ps1 index e95784a567..f624bb2757 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 + $save = [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 = $save } -} - - +} \ No newline at end of file From 40772bcbb52e4e08fd7d1062cfb3ccf31ca9c1a4 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Sat, 10 Nov 2018 01:15:43 +0100 Subject: [PATCH 3/3] update variable name to be more distinct --- functions/Get-DbaWindowsLog.ps1 | 4 ++-- functions/Read-DbaBackupHeader.ps1 | 4 ++-- internal/functions/Invoke-DbaAsync.ps1 | 4 ++-- internal/functions/Invoke-Parallel.ps1 | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/functions/Get-DbaWindowsLog.ps1 b/functions/Get-DbaWindowsLog.ps1 index e31d1e9781..3f56f036ee 100644 --- a/functions/Get-DbaWindowsLog.ps1 +++ b/functions/Get-DbaWindowsLog.ps1 @@ -280,7 +280,7 @@ function Get-DbaWindowsLog { #region Setup Runspace [Collections.Arraylist]$RunspaceCollection = @() $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() - $save = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace + $defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace $RunspacePool = [RunspaceFactory]::CreateRunspacePool($InitialSessionState) $RunspacePool.SetMinRunspaces(1) | Out-Null if ($MaxThreads -gt 0) { $null = $RunspacePool.SetMaxRunspaces($MaxThreads) } @@ -304,6 +304,6 @@ function Get-DbaWindowsLog { Receive-Runspace -Wait $RunspacePool.Close() $RunspacePool.Dispose() - [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $save + [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 64feaeb33c..e0ec436abb 100644 --- a/functions/Read-DbaBackupHeader.ps1 +++ b/functions/Read-DbaBackupHeader.ps1 @@ -190,7 +190,7 @@ function Read-DbaBackupHeader { #Setup initial session state $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() - $save = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace + $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() @@ -278,7 +278,7 @@ function Read-DbaBackupHeader { } #Close the runspace pool $runspacePool.Close() - [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $save + [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace } } diff --git a/internal/functions/Invoke-DbaAsync.ps1 b/internal/functions/Invoke-DbaAsync.ps1 index ca3e04bb09..44270e8463 100644 --- a/internal/functions/Invoke-DbaAsync.ps1 +++ b/internal/functions/Invoke-DbaAsync.ps1 @@ -176,7 +176,7 @@ function Invoke-DbaAsync { $da = New-Object system.Data.SqlClient.SqlDataAdapter($cmd) if ($MessagesToOutput) { - $save = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace + $defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace $pool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS + 1) $pool.ApartmentState = "MTA" $pool.Open() @@ -228,7 +228,7 @@ function Invoke-DbaAsync { } $pool.Close() $pool.Dispose() - [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $save + [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 f624bb2757..3c9691aaa8 100644 --- a/internal/functions/Invoke-Parallel.ps1 +++ b/internal/functions/Invoke-Parallel.ps1 @@ -181,7 +181,7 @@ function Invoke-Parallel { ) begin { # save default runspace - $save = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace + $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') ) { @@ -550,6 +550,6 @@ function Invoke-Parallel { #collect garbage [gc]::Collect() } - [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $save + [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace } } \ No newline at end of file