gjones
gjones

Reputation: 81

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

Upvotes: 1

Views: 1138

Answers (3)

Autumnal Sigh
Autumnal Sigh

Reputation: 13

I'm having a similar issue. For very short-term immediate fixes I've resorted to only loading one of the modules (dbatools or sqlserver) within any single script or session.

Fortunately, for me it resolves it for my immediate need with very minimal script alterations. If the primary function of the script is dbatools related (e.g Test-DbaBuild) I use that throughtout, while for other scripts I can avoid using dbatools where the sqlserver cmdlet is sufficient (e.g. Write-SqlTableData instead of Write-DbaDataTable)

Upvotes: 0

Arnold
Arnold

Reputation: 21

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

Upvotes: 0

gjones
gjones

Reputation: 81

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

Import-Module dbatools
Import-Module sqlserver

Write-Host "Done"

Upvotes: 0

Related Questions