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

167 views Asked by At

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

2

There are 2 answers

0
gjones On BEST ANSWER

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

Import-Module dbatools
Import-Module sqlserver

Write-Host "Done"
0
Arnold On

I have just spent a week working around this issue, which seems to have got worse since dbatools 2.1.10 and sqlserver 22.2.0 became available. Ended up raising a ticket on the dbatools github The workaround is to overwrite DLLs that are supplied by dbatools with ones from the sqlserver module.

For me, the following two were sufficient:

Microsoft.Data.SqlClient.dll

Microsoft.Identity.Client.dll