PowerShell 脚本中的 SQL 包提取命令失败

问题描述 投票:0回答:1

我编写了一个 PowerShell 脚本,该脚本使用 SQL 包创建 dacpac 并将其应用到我的本地数据库。 这个脚本运行良好,我正准备分享它,但它开始抛出错误。

An unexpected failure occurred: The type initializer for 'Microsoft.SqlServer.Dac.DacServices' threw an exception..

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Microsoft.SqlServer.Dac.DacServices' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SqlSchemaModelStaticState' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.TransactSql.ScriptDom, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
   at Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlCoreAnnotationRegister.RegisterModelAnnotations(ModelSchema storeSchema)
   at Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlSchemaModel.SqlSchemaModelStaticState.RegisterModelSchema()
   at Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlSchemaModel.SqlSchemaModelStaticState..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlSchemaModel.SqlSchemaModelStaticState.get_ModelSchema()
   at Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlSchemaModel.InitializeModelSchema()
   at Microsoft.SqlServer.Dac.DacServices..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.SqlServer.Dac.DacServices..ctor(String connectionString)
   at Microsoft.Data.Tools.Schema.CommandLineTool.Program.DoExtractDacpacOperation(CommandLineArguments parsedArgs)
   at Microsoft.Data.Tools.Schema.CommandLineTool.Program.PerformAction(CommandLineArguments parsedArgs)
   at Microsoft.Data.Tools.Schema.CommandLineTool.Program.Run(String[] args)
   at Microsoft.Data.Tools.Schema.CommandLineTool.Program.Main(String[] args)

我的环境没有任何改变。 可能的例外是我安装了 VS 2022(社区),带有 SSDT。 我只是不记得它是否在运行和不运行之间。 但是,我在本地计算机上的 PS 中运行此脚本,未连接到任何 VS 项目。

我尝试卸载VS。 我还重新安装了 SQL 包。

这是脚本。 当它调用位于

sqlpackage $arguments

的 SQL 包时,它会抛出错误
$tableData = ""
$dacpacFile = "databasename.dacpac"
$outputPath = "./"
$config = Get-Content "$PsScriptRoot\DacpacToLocalDb.json" | ConvertFrom-Json
$sourceServer = $config.SourceServer
$sourceDatabase = $config.SourceDatabase
$targetServer = $config.TargetServer
$targetDatabase = $config.TargetDatabase

if($(Write-Host "Copy Data Also? Y / N:  " -NoNewline -ForegroundColor Green; Read-Host) -eq 'y')
{ 
    $tableData = "/p:ExtractAllTableData=True"
}

Write-Host "Generating Dacpac file..." -ForegroundColor Green
$sourceConnectionString = "Server=$sourceServer;Database=$sourceDatabase;Encrypt=False; Integrated Security=SSPI;"
$arguments = "/Action:""Extract""", "/SourceConnectionString:""$sourceConnectionString""", "/TargetFile:""./$dacpacFile""", $tableData
sqlpackage $arguments

if($(Write-Host "Generate script? Y / N:  " -NoNewline -ForegroundColor Green; Read-Host) -eq 'y')
{
    Write-Host "Creating the SQL script..." -ForegroundColor Green
    $script = "$bin\$([System.IO.Path]::GetFileNameWithoutExtension($dacpacFile)).sql"
        $targetConnectionString = "Server=$targetServer;Database=$targetDatabase;Encrypt=False; Integrated Security=SSPI;"
        $arguments = "/Action:Script", "/SourceFile:""$dacpacFile""", "/TargetConnectionString:""$targetConnectionString""", "/OutputPath:""./dacpacScript.sql"""
        $arguments+=$sqlCmdVariables
        & sqlpackage $arguments
}

if($(Write-Host "Publish to '$targetServer'? Y / N:  " -NoNewline -ForegroundColor Green; Read-Host) -eq 'y')
{
    Write-Host "Applying file to target db..." -ForegroundColor Green
    $targetConnectionString = "Server=$targetServer;Database=$targetDatabase;Encrypt=False; Integrated Security=SSPI;"
    $arguments = "/Action:Publish", "/SourceFile:""./$dacpacFile""", "/TargetConnectionString:""$targetConnectionString""", "/p:ExcludeObjectTypes=Logins;ServerRoleMembership;ServerRoles;Credentials;Rolemembership;DatabaseRoles;Users", "/p:AdditionalDeploymentContributors=AgileSqlClub.DeploymentFilterContributor", `
    "/p:AdditionalDeploymentContributorPaths= ./", "/p:AdditionalDeploymentContributorArguments=SqlPackageFilter=IgnoreSchema(svc.solarwinds)"
    & sqlpackage $arguments
}

powershell sqlpackage
1个回答
0
投票

虽然这两条评论都很宝贵,但我认为@IVNSTN 的评论最接近案例。 我把这个问题搁置了几天,以便让 API 项目在我的本地环境中运行。 由于我在故障排除期间卸载了 VS,因此我必须为 API 项目重新安装它。 (VS 不是我团队的 IDE)。 完成后,我回到我的脚本,它按预期工作。 我不确定它修复了什么,但我会接受它。

© www.soinside.com 2019 - 2024. All rights reserved.