1

My current environment of SharePoint 2010 Enterprise Server is having some of the servers updated to support PowerShell v3. At the moment we utilize PowerShell to manage some of the SharePoint Server Farm functions, including copying and installing WSPs, the Build and Deploy system is written in PowerShell. Since SharePoint 2010 still requires PowerShell v2 for the Management Shell I am trying to keep both in place, the current setup has PowerShell actually installed in a separate directory, on D:, where we place the PowerShell scripts we use including the PowerShell Community Extensions. All code is managed in SVN in order to maintain some control, as well as provide the ability to package and deploy it with the same Build and Deploy framework.

I have installed PowerShell v3 on a test SharePoint Server, this does allow me to run both version simulataneously, which is nice. However, I am trying to utilize the same login profile and depending on what version of PowerShell is being started then I want to Import modules specific to either v2 or v3 of PowerShell. This is where I am running into an issue.

My code to load the PSCX version depending on what Shell version of PowerShell I am running is:

if ($ModuleName -like "PSCX") {
  # Get the PS Version since this determines which one can be loaded
  $ver = $psversiontable.psversion.major
  $svnModPath = "C:\MAIN\trunk\PowerShell3\Main\Modules"
  # Load the PSCX v2
  if (Test-Path -Path $svnModPath) {
      # If this is a DEV machine, then basically running out of SVN
      Write-Host "Loading PSCX for DEV Environment PowerShell $ver"
  } else {
      # Server machine, so we want to take from the PowerShell install
      Write-Host "Loading PSCX for Deployed PowerShell $ver"
      $svnModPath = "D:\PowerShell3\Main\Modules"
  }
  if ($ver -eq 2) {
    $pscxPath = "\PSCX2.1.1\PSCX.psd1"
  }
  if ($ver -eq 3) {
    $pscxPath = "\PSCX\PSCX.psd1"
  }
  # Add the module path to the PSModulePath variable since this is not
  # being done by default on the Dev/SP machines, too many PSCX versions...
  $env:PSModulePath = "$svnModPath;" + $env:PSModulePath
  $svnModPath += $pscxPath
  $Module = Get-Module $ModuleName -ListAvailable | Where-Object { $_.Path -eq $svnModPath }
  # get time before 
  $StartTime = Get-Date
  # import module
  Import-Module -ModuleInfo $Module -DisableNameChecking
  if ($? -eq $false) {
    Write-Error -Message "$($MyInvocation.MyCommand.Name):: error importing module $ModuleName"
    return
  }
  $ElapsedTime = (Get-Date) - $StartTime
  if ($ShowLoadDetails) { Write-Host $("    {0,-$Column1Width} [ Loaded in {1:0.00} s ]" -f $ModuleName,$ElapsedTime.TotalSeconds) }
} 

When it gets to the point where it tries to get the Module it won't return anything with -ListAvailable, and even placing the path to the Manifest file doesn't find anything. I have a structure where under PowerShell\Main\Modules I have

  • PSCX - for PSv3
  • PSCX2.1.1 - for the last stable version I have of PSv2

In trying to troubleshoot whether or not the Management Shell can find the v2 PSCX directory if I try Test-ModuleManifest I get an indication it has found the manifest file, but it never shows me any exported commands.

Is this possible to do without having to make major server configurations to support both versions of PSCX?

1 Answer 1

0

Turns out this is possible, but the PSD1 file HAS to have the same name as the directory.

Missed this on multiple readings of the PSCX file, but

  if ($ver -eq 2) {
    $pscxPath = "\PSCX2.1.1\PSCX.psd1"
  }

Should be:

  if ($ver -eq 2) {
    $pscxPath = "\PSCX2.1.1\PSCX2.1.1.psd1"
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.