2

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.

Brian Vallelunga
  • 9,869
  • 15
  • 60
  • 87
  • 2
    Although consuming NuGet packages in PowerShell is a bit of a chore, the [`Microsoft.SqlServer.DacFx`](https://www.nuget.org/packages/Microsoft.SqlServer.DacFx/) package does have Core support, and you're likely to have more success with those than the stuff that comes with SQL Server itself, since that has largely not transitioned away from Framework yet. For less advanced scenarios invoking [`SqlPackage`](https://learn.microsoft.com/sql/tools/sqlpackage/sqlpackage) directly is also an option. – Jeroen Mostert Jun 30 '22 at 07:24

0 Answers0