Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Microsoft.SqlServer.Dac.DacServices from Powershell Core?

I'm trying to use DacServices from a Powershell Core script. Unfortunately, it seems DacServices requires System.Diagnostics.Eventing.EventDescriptor, which isn't available in .NET 6. I'm using Powershell Core 7.2.5. Is there a convenient way around this?

I'm loading the types with this code:

$sqlServerDacPaths = "${env:ProgramFiles}\Microsoft SQL Server\150\DAC\bin\Microsoft.SqlServer.Dac.dll", "${env:ProgramFiles}\Microsoft SQL Server\150\DAC\bin\Microsoft.SqlServer.Dac.Extensions.dll"

foreach ($path in $sqlServerDacPaths) {
    if (Test-Path $path) {
        Write-Host "Adding $path"
        Add-Type -Path $path
    }
}

# Instantiate DacServices
$dacServices = New-Object Microsoft.SqlServer.Dac.DacServices $connectionString

The exception I get instantiating DacServices is:

The type initializer for 'Microsoft.SqlServer.Dac.DacServices' threw an exception... Could not load type 'System.Diagnostics.Eventing.EventDescriptor' from assembly 'System.Core, Version=4.0.0.0

I've tried both the SQL 15 and 16 versions of DacServices without luck.

like image 441
Brian Vallelunga Avatar asked Mar 15 '26 08:03

Brian Vallelunga


1 Answers

Sorry for late anwser, you can try running the script in lower version of Powershell, Powershell 5.1 is built on top of the .NET Framework v4.5 which has access to type System.Diagnostics.Eventing.EventDescriptor

It worked in my case:

powershell -Version 5.1 pathToYourScript
like image 175
Dawid Pietrzak Avatar answered Mar 17 '26 01:03

Dawid Pietrzak