Import-Module SQLPSX corrupts PSModulePath

May 12, 2010 at 6:11 PM

In Version 2.2, Import-Module SQLPSX corrupts PSModulePath and doesn't import the module if the SQLPSX path isn't in the first Module Path contained in $Env:PSModulePath.

It takes the first module path, prepends some info, appends some info and adds it to the current PSModulePath.  Unfortunately the pre-pended part shouldn't be there.

I've included a possible fix after the problem is detailed.

# Current PSModulePath
$Env:PSModulePath
C:\Documents and Settings\<USER>\My Documents\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\

# Code from SQLPSX.psm1
$mpath = $env:PSModulePath.Split(";") ; $DestinationLocation = "SQLPSX\Modules;" + $mpath[0] + "\SQLPSX\Modules;";
$env:PSModulePath = $env:PSModulePath + $DestinationLocation;

#affect of code
#$mpath is an array of all the module paths.
$mpath
C:\Documents and Settings\<USER>\My Documents\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\

#Value of $DestinationLocation
$DestinationLocation
SQLPSX\Modules;C:\Documents and Settings\<USER>\My Documents\WindowsPowerShell\Modules\SQLPSX\Modules;

#$DestinationLocation is then appending to the current PSModulePath, which ends up being
$Env:PSModulePath
C:\Documents and Settings\smills\My Documents\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\SQLPSX\Modules;C:\Documents and Settings\smills\My Documents\WindowsPowerShell\Modules\SQLPSX\Modules;

#Get it as an array.  Pay special attention to the second path.  It is no longer pointing to the location it was initially.
$Env:PSModulePath.Split(';')
C:\Documents and Settings\smills\My Documents\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\SQLPSX\Modules
C:\Documents and Settings\smills\My Documents\WindowsPowerShell\Modules\SQLPSX\Modules

I would suggest replacing all the code with something like the following:

# Location of Modules for SQLPSX
$SQLPSXModuleRoot = $PSScriptRoot + '\Modules'

# Only add it to PSModulePath if it isn't already there.
if ( $Env:PSModulePath.Split(';') -notcontains $SQLPSXModuleRoot )
{
	$Env:PSModulePath += ';' + $SQLPSXModuleRoot
}

# the following ones are turned off from the list of modules being loaded: WPK, ISECreamBasic, OracleClient, OracleIse, and SQLIse.
$SQLPSXloadModules = "SQLServer","Agent","Repl","SSIS","SQLParser","Showmbrs","SQLmaint","adolib";

Write-Host 'Importing SQLPSX'

# Here's the loop that will load the modules
foreach($PSXmodule in $SQLPSXloadModules){
  Write-Host "Loading SQLPSX Module '$PSXModule'";
  
  #Include -Force:$Force to support "Import-Module SQLPSX -Force".  
  #Useful if a function has been modified, since you don't have to remove and re-import the module.
  Import-Module $PSXmodule -Force:$Force;
}

# Cleanup variables so they won't be exported as part of the module.
Remove-Variable SQLPSXModuleRoot -ErrorAction 'SilentlyContinue'
Remove-Variable SQLPSXloadModules -ErrorAction 'SilentlyContinue'

Write-Host "SQLPSX Imported"

Thanks for the great Extension.

-Stephen

Developer
May 12, 2010 at 7:26 PM
Hello Stephen, that's very similar to what you can find in http://sqlpsx.codeplex.com/SourceControl/changeset/view/48940#658582 Bernd
Developer
May 13, 2010 at 12:49 AM
bernd_k wrote:
Hello Stephen, that's very similar to what you can find in http://sqlpsx.codeplex.com/SourceControl/changeset/view/48940#658582 Bernd

 Bernd,

I went through the thread I didn't even realized you improved the script now called SQLPSX.psm1.  This is the first time I've seen it.  Thanks for the information.. 

I will take a look at both yours and Stephen scripts to incorporated the changes into the next release.  Please, next time don't be too shy. 

Your script looks pretty good! 

Thanks for the feedbacks,

Max

Developer
May 13, 2010 at 12:51 AM

Stephen,

I greatly appreciate your feedback and will take consideration your script chnages.  Good Job!

Thanks again,

Max

 

Developer
May 13, 2010 at 9:24 AM
When I wrote it, I had no opportunity to test what would happen, when Oracle dlls are not installed. I would like to use some configuration to load sql, smo, oracle in any combination. At that time your loader was around and I hoped that the question of user preferences would be settled by now.

BTW I like it when remove-module works as clean as possible ( = removes add-on menus added), but in practice I just close a tab (= runspace) and reopen it, that's more relieable. My current way is that all modules import other modules they need. 
As long as we have no quick way to edit configuration, I want the commands  ipmo sqlpsx; ipmo oracleise to load the oracle modules or just ipmo oracleIse if sqlpsx imported by profile. I'm thinking about loading oracle modules depending on presence of ODAC components as alternative too. 

My main goal is, that the Oracle components don't interfere with the SQL stuff (besides the short-cuts of course). And to avoid that those having no Oracle installed import unusable modules.
I hope that the published revision is stable enough, so that I can guide those interested in  Oracle through the first steps next days.

And Stephen thanks for your feedback. 

Bernd 

2010/5/13 MaxTrinidad <notifications@codeplex.com>

From: MaxTrinidad

bernd_k wrote:
Hello Stephen, that's very similar to what you can find in http://sqlpsx.codeplex.com/SourceControl/changeset/view/48940#658582 Bernd

 Bernd,

I went through the thread I didn't even realized you improved the script now called SQLPSX.psm1.  This is the first time I've seen it.  Thanks for the information.. 

I will take a look at both yours and Stephen scripts to incorporated the changes into the next release.  Please, next time don't be too shy. 

Your script looks pretty good! 

Thanks for the feedbacks,

Max

Read the full discussion online.

To add a post to this discussion, reply to this email (SQLPSX@discussions.codeplex.com)

To start a new discussion for this project, email SQLPSX@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com