Powershell错误设置SSIS包执行的环境变量

时间:2017-11-02 21:57:38

标签: sql-server powershell ssis sql-server-2012

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

我也尝试删除环境变量并更改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."

2 个答案:

答案 0 :(得分:0)

只需更改包装上的参数即可,不必担心环境变量。我确信它不会更改存储在服务器上的包中的值,只会更改$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)

答案 1 :(得分:0)

因此,通过一些挖掘,我找到了错误的答案。原来我在我的GAC(Windows \ assembly)中有多个版本(11.0.0.0,12.0.0.0,13.0.0.0)的程序集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")