Search code examples
powershelldbatoolspowershell-7

upgrade to Powershell 7 & dbaTools 2 causing "Assembly with same name is already loaded" error


I've moved a powershell script that was using modules SqlServer(22.1.18226) & dbaTools(1.0.116) in Powershell 5.1 to Powershell 7.3 using SqlServer(22.1.1) & dbaTools(2.1.5). Now I'm now getting error "Assembly with same name is already loaded" even on this simple script run from the command prompt: pwsh testScript.ps1

Import-Module sqlserver
Import-Module dbatools
Write-Host "Done"

Error message:

Line |
   2 |  Import-Module dbatools
     |  ~~~~~~~~~~~~~~~~~~~~~~
     | Couldn't import C:\Program
     | Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\core\lib\win-sqlclient\Microsoft.Data.SqlClient.dll |
     | Assembly with same name is already loaded

This works fine if I run it inside VS Code using the "run/play" button.

What am I doing wrong? Thanks


Solution

  • Not sure why this worked but changing the order of the Import-Module calls worked...

    Import-Module dbatools
    Import-Module sqlserver
    
    Write-Host "Done"