我正在尝试使用 Powershell Core 脚本中的
DacServices
。不幸的是,DacServices 似乎需要 System.Diagnostics.Eventing.EventDescriptor
,这在 .NET 6 中不可用。我使用的是 Powershell Core 7.2.5。有没有方便的方法解决这个问题?
我正在使用此代码加载类型:
$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
我实例化的例外情况
DacServices
是:
“Microsoft.SqlServer.Dac.DacServices”的类型初始值设定项引发异常... 无法加载类型“System.Diagnostics.Eventing.EventDescriptor” 程序集'System.Core,版本=4.0.0.0
我尝试了 SQL 15 和 16 版本的
DacServices
,但没有成功。
抱歉回复晚了,您可以尝试在较低版本的 Powershell 中运行脚本,Powershell 5.1 构建在 .NET Framework v4.5 之上,可以访问类型 系统.诊断.事件.EventDescriptor
它对我的情况有效:
powershell -Version 5.1 pathToYourScript
我在使用 PowerShell 7 时遇到了同样的问题。我还无法对答案发表评论,但 Dawid Pietrzak 的答案对我有用:
powershell -版本 5.1 pathToYourScript