Search code examples
powershellvisual-studio-2015pester

PowerShell Module cannot be loaded when running Pester test in Visual Studio


I have created a PowerShell test script, Common.tests.ps1, using Pester against some functions in a PowerShell script, Common.ps1, in the same directory.

There is a TestInitializer.ps1 script, also in the same directory, which uses the Microsoft.Xrm.Data.PowerShell module to create and get records in a Dynamics CRM instance.

When running the PowerShell test script from Visual Studio, the test fails in the Test Explorer with a message:

CommandNotFoundException: The module 'Microsoft.Xrm.Data.PowerShell' could not be loaded. For more information, run 'Import-Module Microsoft.Xrm.Data.PowerShell'.

The same test when run from the PowerShell ISE, however, runs without issue. This would seem to be as if the module is not installed for the instance as run by Visual Studio (I confirmed this when running Get-Module -ListAvailable and seeing that the output did not include the Microsoft.Xrm.Data.PowerShell module for the Visual Studio test), though even explicit calls like Import-Module Microsoft.Xrm.Data.PowerShell -Global -Force don't seem to load the module during the script execution with Visual Studio.

Here's Common.test.ps1:

$here = (Split-Path -Parent $MyInvocation.MyCommand.Path)
. $here\Common.ps1
. $here\TestInitializer.ps1

Describe "SelectionToSingleCharString" {
Context "StringTransforms" {
    It "Retrieves a CRM record and uses the optionset value to retrieve a single character" {
    SelectionToSingleCharString($crmRecord.new_type) | Should Be "I"
        }
    }
}

Snippet from TestInitializer.ps1:

# Whether or not this is uncommented does not matter
#Import-Module "$env:SystemRoot\System32\WindowsPowerShell\v1.0\Modules\Microsoft.Xrm.Data.PowerShell\Microsoft.Xrm.Data.PowerShell.psd1" -Global -Force

#$modules = Get-Module -ListAvailable
#Write-Host $modules

# Failing here
Microsoft.Xrm.Data.PowerShell\Connect-CrmOnPremDiscovery -ServerUrl $loginServerUrl -OrganizationName $loginOrgName -Credential $cred

I may instead design the test to use Mock instead of actually attempting to create/read records, though not being able to load external modules and run in Visual Studio would be limiting.


Solution

  • Regarding module install directories (skip this if you are interested in the actual problem):

    First you should never install modules to $PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules). This folder is reserved for modules shipped with Windows only.

    You should always install your modules used only by your user under the following path:

    $Home\Documents\WindowsPowerShell\Modules 
    

    And for a system-wide installation under:

    $Env:ProgramFiles\WindowsPowerShell\Modules
    

    Further reading regarding installing modules in PowerShell can be found on MSDN.

    Regarding your actual problem:

    What Visual Studio Version are you using? I have Visual Studio 2017 Community edition installed and can not reproduce your error. My PowerShell is also running as a 64-bit process. Your PowerShell could be running as a 32-bit process. For the 32-bit PowerShell the module directories are different. This would explain why the module you installed isn't showing up in Visual Studio.

    You can verify if your PowerShell is running in a 64-bit process with the following command:

    PS> [Environment]::Is64BitProcess
    True
    

    To make your modules accessible for the 32-bit PowerShell you need to install them under the following path as well:

    {$Env:ProgramFiles(x86)}\WindowsPowerShell\Modules