为 SSIS 包执行设置环境变量时出错



我的Powershell脚本执行SSIS包,但首先覆盖环境变量。环境信息对象上的 Alter 方法失败,并显示一般错误消息:"在执行期间,对象 [环境信息[@Name='MyVariable'] 上的操作'更改'失败。

我还尝试删除环境变量并更改 Project 参数,但在 Project 对象的 Alter 方法上收到相同的错误。

我怀疑这是 1) 使用 32 位版本的 SQL Server 2012 的缺点,或 2) 权限问题。

我已确保执行的 Windows 帐户对 SSISDB 数据库和 SSIS 目录项目以及子文件夹、环境等具有完全权限。

关于错误或如何获取更多详细信息的任何想法?我在 Windows 事件日志中看不到任何内容。

这是我的代码:

# Variables
$ServerInstance = "MyServer"
$32bitSQLServer = "false" #use #null for 32-bit
$SSISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"
$FolderName = "MyFolder"
$ProjectName = "MyProject"
$PackageName = "MyPackage.dtsx"
$EnvironmentName = "MyEnvironment"
$VariableName = "MyVariable"
$VariableValue = Read-Host "What is the new environment variable value? "
# Create a connection to the server - Have to use Windows Authentication in order to Execute the Package
$sqlConnectionString = `
    "Data Source=" + $ServerInstance + ";Initial Catalog=master;Integrated Security=SSPI;"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString
# Create the Integration Services object
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices")  
$integrationServices = New-Object $SSISNamespace".IntegrationServices" $sqlConnection
# Get the Integration Services catalog
$catalog = $integrationServices.Catalogs["SSISDB"]
# Get the folder
$folder = $catalog.Folders[$FolderName]
# Get the project
$project = $folder.Projects[$ProjectName]
# Get the environment
$environment = $folder.Environments[$EnvironmentName]
# Get the environment reference
$environmentReference = $project.References.Item($EnvironmentName, $FolderName)            
$environmentReference.Refresh() 
# Get the package
$package = $project.Packages[$PackageName]
# Set the Environment Variable
$environment.Variables[$VariableName].Value = $VariableValue
$environment.Alter()
# Execute the package
Write-Host "Running Package " $PackageName "..."
$result = $package.Execute($32bitSQLServer, $environmentReference)
# Alternate approach, also not working
# $project.Parameters[$VariableName].Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Literal,$VariableValue)
# $project.Alter()
# $result = $package.Execute($32bitSQLServer, $environmentReference)
Write-Host "Done."

只需更改包上的参数,而不必担心环境变量。我确定它不会更改存储在服务器上包中的值,只会更改$package变量中保存的对象。像这样:

$package.Parameters[$VariableName].Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Literal, $VariableValue)
$package.Alter()
$execution = $integrationServices.Catalogs['SSISDB'].Executions[$package.Execute($false, ($package.Parent.References[$package_name, $folder_Name]))]
do {
    $execution.Refresh()
} until ($execution.Completed)

因此,通过一些挖掘,我找到了错误的答案。 原来我在我的GAC(Windows\assembly)中有多个版本的程序集Microsoft.SqlServer.Management.IntegrationServices。 检查 SSISDB 目录的架构,它是版本 12.0.5000.0,这意味着我需要程序集的 12.0.0.0 版本。我使用的代码:

[Reflection.Assembly]::LoadWithPartialName( "Microsoft.SqlServer.Management.IntegrationServices")

加载了错误的版本(可能是 13.0.0.0),所以我需要显式加载与此 SSIS 安装匹配的程序集版本,即 12.0.0.0:

[System.Reflection.Assembly]::Load("Microsoft.SqlServer.Management.IntegrationServices, Version=12.0.0.0, Culture=neutral, processorArchitecture=MSIL, PublicKeyToken=89845dcd8080cc91")

最新更新