From 975a447dad1efa9e9067241015880d9ccbef7436 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 24 Jun 2017 18:09:39 +0200 Subject: [PATCH] Changes to Connect-SQL and Import-SQLPSModule Now it correctly loads the correct assemblies when SqlServer module is present (issue #649). Now SQLPS module will be correctly loaded (discovered) after installation of SQL Server (issue #659). Previously resources depending on SQLPS module could fail because SQLPS was not found after installation because the PSModulePath environment variable in the (LCM) PowerShell session did not contain the new module path. --- CHANGELOG.md | 8 ++++++++ Tests/Unit/xSQLServerHelper.Tests.ps1 | 1 + xSQLServerHelper.psm1 | 11 ++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c2aa376a..574bdd1926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -132,6 +132,14 @@ parameter Verbose. - Moved localization strings from xSQLServer.strings.psd1 to xSQLServerHelper.strings.psd1. + - Changes to Connect-SQL and Import-SQLPSModule + - Now it correctly loads the correct assemblies when SqlServer module is + present (issue #649). + - Now SQLPS module will be correctly loaded (discovered) after installation + of SQL Server. Previously resources depending on SQLPS module could fail + because SQLPS was not found after installation because the PSModulePath + environment variable in the (LCM) PowerShell session did not contain the new + module path. - Changes to xSQLServerSetup - BREAKING CHANGE: Replaced StartWin32Process helper function with the cmdlet Start-Process (issue #41, #93 and #126). diff --git a/Tests/Unit/xSQLServerHelper.Tests.ps1 b/Tests/Unit/xSQLServerHelper.Tests.ps1 index e7ea4fd0e0..6da15cb0e4 100644 --- a/Tests/Unit/xSQLServerHelper.Tests.ps1 +++ b/Tests/Unit/xSQLServerHelper.Tests.ps1 @@ -1071,6 +1071,7 @@ InModuleScope $script:moduleName { Describe 'Testing Connect-SQL' -Tag ConnectSql { BeforeEach { Mock -CommandName New-InvalidOperationException -MockWith $mockThrowLocalizedMessage -Verifiable + Mock -CommandName Import-SQLPSModule Mock -CommandName New-Object ` -MockWith $mockNewObject_MicrosoftDatabaseEngine ` -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter ` diff --git a/xSQLServerHelper.psm1 b/xSQLServerHelper.psm1 index 523e76ea3c..fe2601a309 100644 --- a/xSQLServerHelper.psm1 +++ b/xSQLServerHelper.psm1 @@ -37,7 +37,7 @@ function Connect-SQL $SetupCredential ) - $null = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') + Import-SQLPSModule if ($SQLInstanceName -eq 'MSSQLSERVER') { @@ -697,6 +697,15 @@ function Import-SQLPSModule else { Write-Verbose -Message ($script:localizedData.PreferredModuleNotFound) -Verbose + + <# + After installing SQL Server the current PowerShell session doesn't know about the new path + that was added for the SQLPS module. + This reloads PowerShell session environment variable PSModulePath to make sure it contains + all paths. + #> + $env:PSModulePath = [System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') + $module = (Get-Module -FullyQualifiedName 'SQLPS' -ListAvailable).Name }