4

While I am a long time user of Stackoverflow, this is my first question.
I am working to deploy SSIS Projects via Powershell. I am a Senior .NET Developer not a DBA. I have very little familiarity with SSIS package deployment.

I have worked through several blog / tutorials to gain the understanding I now have but something is still amiss.

I believe I setup my SSIS package correctly. According to my understanding, the connection managers are setup appropriately. I posted a snapshot of my connection managers below. Please note the 'fx (project)' next to the connection manager descriptions.

Connection Manager Snapshot.

I have two Powershell scripts. One that deploys the package and another that sets up the environment variables and project parameters.
The problem I am encountering has to do with the environment not being selected when the project is executed despite my setting up the relationship. Hence my project parameters do not see my environment variables.

I receive the following errors when the package is executed:

The parameter "FlatFileConnectionManager_ConnectionString" is configured to use an environment variable, but no environment has been selected. Check the "Environment" checkbox and specify the environment to use, or specify a literal value for the parameter.

The parameter "LocalHostAdventureWorksDW2012_ConnectionString" is configured to use an environment variable, but no environment has been selected. Check the "Environment" checkbox and specify the environment to use, or specify a literal value for the parameter.

Powershell Deployment script:

Add-Type -AssemblyName "Microsoft.SqlServer.Management.IntegrationServices, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"

$sqlInstance = "xxxxxxx"
$sqlConnectionString = "Data Source=$sqlInstance;Initial Catalog=master;Integrated Security=SSPI"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString
$isNamespace = "Microsoft.SqlServer.Management.IntegrationServices"

$catalogPwd = "xxxxxx"


$ssisServer = New-Object $ISNamespace".IntegrationServices" $sqlConnection


if ($ssisServer.Catalogs.Count -gt 0)             
{             
    $ssisServer.Catalogs["SSISDB"].Drop()             
}            


$catalog = New-Object $isNamespace".Catalog" ($ssisServer, "SSISDB" ,$catalogPwd  )
$catalog.Create()

$ssisCatalog = $ssisServer.Catalogs["SSISDB"]

$ssisFolderName = "PowerShell Demos"
$ssisFolderDescription = "Created with PowerShell"
$ssisFolder = New-Object Microsoft.SqlServer.Management.IntegrationServices.CatalogFolder($ssisCatalog, $ssisFolderName, $ssisFolderDescription)
$ssisFolder.Create()

$ssisProjectName = "TestProject"
[byte[]] $ssisProjectFile = [System.IO.File]::ReadAllBytes("C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\DeployTest\TestProject\bin\Development\TestProject.ispac")
$ssisFolder.DeployProject($ssisProjectName, $ssisProjectFile)

Powershell Environment and Project Parameter setup script:

Add-Type -AssemblyName "Microsoft.SqlServer.Management.IntegrationServices, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"

$sqlInstance = "xxxxxxx"
$sqlConnectionString = "Data Source=$sqlInstance;Initial Catalog=master;Integrated Security=SSPI"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString
$ssisCatalog = "SSISDB"
$isNamespace = "Microsoft.SqlServer.Management.IntegrationServices"

$environmentName = "UPMVariables"
$projectName = "TestProject"
$folderName = "PowerShell Demos"

$integrationServices = New-Object "$isNamespace.IntegrationServices" $sqlConnection

$catalog = $integrationServices.Catalogs[$ssisCatalog]

$folder = $catalog.Folders.Item($folderName)

$environment = $folder.Environments[$environmentName]

if (!$environment)
{
    Write-Host "Creating environment ..." 
    $environment = New-Object "$isNamespace.EnvironmentInfo" ($folder, $environmentName, "testDescription")
    $environment.Create()             
}

# Adding variable to our environment
# Constructor args: variable name, type, default value, sensitivity, description

Write-Host "Adding environment variables ..." 
$eFlatFileConnectonString = $environment.Variables.Item("LocalHostAdventureWorksDW2012_ConnectionString")
if (!$eFlatFileConnectonString)
{

    $environment.Variables.Add("FlatFileConnectionManager_ConnectionString", [System.TypeCode]::String, "C:\newDestination\test.txt", $false, "FlatFile ConnectionString")

    $environment.Alter()

    $eFlatFileConnectonString = $environment.Variables.Item("LocalHostAdventureWorksDW2012_ConnectionString")        

}

$eAdvWorksConnectionString = $environment.Variables.Item("LocalHostAdventureWorksDW2012_ConnectionString")
if (!$eAdvWorksConnectionString)
{

     $environment.Variables.Add( "LocalHostAdventureWorksDW2012_ConnectionString", 
    [System.TypeCode]::String, "Data Source=xxxxxxx;Initial Catalog=AdventureWorksDW2012;Provider=SQLNCLI11.1;User Id=xxxx;Password=xxxxx;", $false, "LocalHostAdventureWorksDW2012 ConnectionString")

    $environment.Alter()

    $eAdvWorksConnectionString = $environment.Variables.Item("LocalHostAdventureWorksDW2012_ConnectionString")
} 


$project = $folder.Projects[$projectName]

$ref = $project.References.Item($environmentName, $folderName)

if (!$ref)
{
    # making project refer to this environment
    Write-Host "Adding environment reference to project ..."
    $project.References.Add($environmentName, $folder.Name)
    $project.Alter() 
}

$projectVariable = $project.Parameters[$eAdvWorksConnectionString.Name]
$project.Parameters[$eAdvWorksConnectionString.Name].Set("Referenced", $eAdvWorksConnectionString.Name) 

$projectVariable = $project.Parameters[$eFlatFileConnectonString.Name]           
$project.Parameters[$eFlatFileConnectonString.Name].Set("Referenced", $eFlatFileConnectonString.Name)            
$project.Alter()

I hoping someone on Stackoverflow understands what is causing this issue. Thanks in advance for any help and best regards!

******* Clarification of Question...More Information *******

I understand how to manually deploy, create Sql Server Environment Variables and configure Project parameters to reference the Sql Server Environment Variables via SSMS. I am trying to understand how to do so via Powershell script with no manual intervention. And, while I may be mistaken, I believe it is possible to automate this deployment / setup according to the research I have done.

When the scripts I included with my question execute, the following things occur successfully.

  • The SSIS project deploys successfully to SQL Server. Please see the following snapshot.

    SSMS - Project Deployed

  • The Environment Variables are present after the script runs. The Environment Variables can be seen by right clicking the Environment Variable object, in this case titled 'UPMVariables'. These Variables are present due to the following part of the Powershell script.

    $environment.Variables.Add( "LocalHostAdventureWorksDW2012_ConnectionString", [System.TypeCode]::String, "Data Source=xxxxxxxx;Initial Catalog=AdventureWorksDW2012;Provider=SQLNCLI11.1;User Id=xxxx;Password=xxxxxx;", $false, "LocalHostAdventureWorksDW2012 ConnectionString")

    $environment.Variables.Add("FlatFileConnectionManager_ConnectionString", [System.TypeCode]::String, "C:\newDestination\test.txt", $false, "FlatFile ConnectionString")

    $environment.Alter() $eAdvWorksConnectionString = $environment.Variables.Item("LocalHostAdventureWorksDW2012_ConnectionString") $eFlatFileConnectonString = $environment.Variables.Item("FlatFileConnectionManager_ConnectionString")

    Environment Variable Snapshot

  • Project Parameters are present, as they should be, after deployment. These Project Parameters are created during the SSIS Project creation.

    Project Parameters visible in SSMS

  • A relationship, between the Sql Server Environment Variables and the SSIS Project, should be established via the following code from the Powershell script I included with my question.

    $project.References.Add($environmentName, $folder.Name)

    $project.Alter()

  • Here is the challenge and the reason for my question. When you right click the SSIS pack and click execute (see first picture below), the Environment Variable checkbox has to manually be checked before the package can use the Environment Variable values.

    SSMS - Execute Package

    Environment Variable vs Parameter Dialogue

  • Once the Environment Variable check box is manually clicked, the Sql Server Environment Variables become available to the SSIS Project Parameters. This is the step the script is not establishing.

    Environment Variable And Parameter Relationship Manually Established

  • This is the step I am trying to automate as manually checking that Environment Variable checkbox can not be managed. These SSIS Packages are run via job scheduling.

  • As an aside, one of the tutorials I read said you can generate a T-SQL script that establishes the relationship between Project Parameters and Sql Server Environment Variables by clicking the script button in the upper left of the 'Execute Package' dialogue. Please see the snapshot of the script and the 'Execute Package' dialogue below. We could execute the T-SQL script from VSTS as a final step but unfortunately running this script did not result in the checkbox being checked.

TSQL

Declare @execution_id bigint

EXEC [SSISDB].[catalog].[create_execution] @package_name=N'Package.dtsx', 
@execution_id=@execution_id OUTPUT, @folder_name=N'PowerShell Demos', 
@project_name=N'TestProject', @use32bitruntime=False, @reference_id=1
Select @execution_id

DECLARE @var0 smallint = 1
EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id,  
@object_type=50, @parameter_name=N'LOGGING_LEVEL', @parameter_value=@var0

EXEC [SSISDB].[catalog].[start_execution] @execution_id
GO

Execute Dialogue Script Button

I hope this additional information will provide greater insight into the problem I am facing. I sincerely appreciate any help! Thank you!

****** UPDATE 03/06/2018 ******

May be my expectations are errant? It is my understanding that I should be able to:

  1. Create my SSIS project / package in SSDT (2012). This includes:

    A. Create the data flow task

    B. Create connection managers if needed

    C. Setup parameters with default values...and mark them required / sensitive if needed

    D. Ensure the created parameters are scoped correctly (i.e. project or package level)

  2. Deploy the SSIS Package via Powershell script. This includes:

    A. Package deployment to SQL Server

    B. Create SQL Server environment variables

    C. Create a relationship between the project folder and the Sql Server environment variables

    D. Create a relationship between the created SQL Server environment variables and the scoped parameters created in SSDT

  3. Run the package via a job scheduler with no concern for project parameters

My challenge is creating the relationship between SQL Server environment variables and scoped parameters. Looking back at the T-SQL I posted yesterday.

Declare @execution_id bigint

EXEC [SSISDB].[catalog].[create_execution] @package_name=N'Package.dtsx', 
@execution_id=@execution_id OUTPUT, @folder_name=N'PowerShell Demos', 
@project_name=N'TestProject', @use32bitruntime=False, @reference_id=1
Select @execution_id

DECLARE @var0 smallint = 1
EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id,  
@object_type=50, @parameter_name=N'LOGGING_LEVEL', @parameter_value=@var0

EXEC [SSISDB].[catalog].[start_execution] @execution_id
GO

This is to setup logging for a package execution and then executing the package. Good stuff but it does not setup the scoped parameters to use SQL Server environment variables.
What I found since my last post is a T-SQL script that first sets parameters before executing the package.

Execute Package T-SQL

I know you have to set the object_type to either 30 (package mode) or 20 (project mode) execution. That is simple enough.

Do you have to add Parameters and Values at the time of execution like the following?

@parameter_name=N'pSourceDir'
@parameter_value='c:\Temp'

This seems redundant considering we create scoped Parameters in SSDT with default values. Then Sql Server environment variables with values. This is what does not make sense to me and hence why I am confused.

I am hoping beyond hope someone will clarify this bit for me. Thanks again Stackoverflow!

3 Answers 3

3

Good morning Stackoverflow, I found the answer to my question yesterday. It did not have to do with Environment Variables nor does it have to do with T-SQL. The problem was the following statement in the Powershell script.

$projectVariable = $project.Parameters[$eAdvWorksConnectionString.Name]
$project.Parameters[$eAdvWorksConnectionString.Name].Set("Referenced", 
$eAdvWorksConnectionString.Name) 

$projectVariable = $project.Parameters[$eFlatFileConnectonString.Name]           

$project.Parameters[$eFlatFileConnectonString.Name].Set("Referenced", 
$eFlatFileConnectonString.Name)            
$project.Alter()

I thought the statements above established a relationship by some means between the Sql Server Environment Variables and the Project Parameters. I was WRONG. And the confusing part is I did not receive an error / feedback when the script ran saying something was amiss. This is why I reached out to Stackoverflow. Having never deployed an SSIS package via Powershell, I did not have a solid understanding of the expected result.
The above method establishes the Project Parameters but does not enable the utilization of the corresponding Sql Server Environment Variables without manual intervention. The following means of establishing Project Parameters does enable the utilization of Sql Server Environment Variables without manual intervention.

$projectVariable = $project.Parameters[$eAdvWorksConnectionString.Name]
$project.Parameters[$eAdvWorksConnectionString.Name].Set("Literal", 
$eAdvWorksConnectionString.value) 

$projectVariable = $project.Parameters[$eFlatFileConnectonString.Name] 
$project.Parameters[$eFlatFileConnectonString.Name].Set("Literal", 
$eFlatFileConnectonString.value)            
$project.Alter()

I am posting this information here for anyone else who is attempting the same exercise as I. By the way, the Powershell script in my initial question works perfectly without error except for the exception I am describing in this answer. Replace the means of establishing Project Parameters with the correction I am describing in this answer. The script will deploy the SSIS package, establish the relationship between Project Parameters and SQL Server Environment Variables. The SSIS package will then be ready to run without manual intervention. I hope this helps someone in the future.
Thanks to billinkc and postanote for trying to help...much appreciated! Best regards!

Sign up to request clarification or add additional context in comments.

Comments

0

In the greater number of cases. If you see things screaming about environment variables, this means only a few things in most cases.

In the Windows - Advanced system settings / Environment variables:

  1. You have not defined a user environment variable.
  2. You have not defined a system environment variable.

Think of this like trying to run Java stuff, without first install Java and Setting the JAVA_HOME system variable and forgetting add that to the system path. You can do this manually or programmatically in your script and pre-staging stuff.

[System.Environment]::SetEnvironmentVariable("EnvVarName", "EnvVarPath", "EnvVarTarget")

https://msdn.microsoft.com/en-us/library/system.environment.setenvironmentvariable(v=vs.110).aspx

See this article:

Environment Variables in SSIS Packages and Configuration Tables

Environment variables can hold the name of the server on each server. In the Configuration Manger you place in the name of the Environment Variable. The Environment Variable does not hold the value of the variable or connection that is passed to the package. The environment variable holds the name of the server. This value tells the package where to look for the configuration table and reads the configuration table for the configured values to pass into the package.

You can think of the Environment Variables as pointers for the package. When you move a package to another server it will look for an Environment Variable. It does not matter on which server your package is running. As long as the server has an Environment Variable named the proper name and it contains the name of the proper server, the package will run properly.

https://mikedavissql.com/2013/08/12/environment-variables-in-ssis-packages-and-configuration-tables

See also this post:

Powershell error setting Environment variable for an SSIS package execution

My Powershell script executes an SSIS package, but first over-rides an Environment variable. The Alter method on the EnvironmentInfo object fails with a generic error message: "Operation 'Alter' on object [EnvironmentInfo[@Name='MyVariable']' failed during execution."

I also tried removing the environment variable and changing the Project parameter, but received the same error on the Alter method for the Project object.

I suspect this is either 1) a shortcoming of using the 32-bit version of SQL Server 2012, or 2) a permissions issue.

I've made sure the executing Windows Account has full privileges on the SSISDB database and the SSIS Catalog project, and the child folder, environment, etc.

Powershell error setting Environment variable for an SSIS package execution

**** Update ****

IndyDev --

Since I don't have a SSIS deployment environment to use as I stated in my comment, I went back to the docs. This is more of an SSIS feature deal than anything specific to PowerShell.

Integration Services (SSIS) Package and Project Parameters

Environment Variables

If a parameter references an environment variable, the literal value from that variable is resolved through the specified environment reference and applied to the parameter. The final literal parameter value that is used for package execution is referred to as the execution parameter value. You specify the environment reference for an execution by using the Execute dialog box

> If a project parameter references an environment variable and the literal value from the variable cannot be resolved at execution, the design value is used. The server value is not used.

https://learn.microsoft.com/en-us/sql/integration-services/integration-services-ssis-package-and-project-parameters

One more thing about this. "You have to manually click the Environment variable check box when the package is executed.", is you can use sendkeys to interact with this dialog, though sendkeys can be kind of finicky.

From a former SQL class I took a while back, the notes I have on the SSIS topic show...

  1. SSIS doesn't presume to know which environment to use (even if only one exists). So, you want to check the box and choose the correct environment - this will always be true when a package is run on-demand like this.
  2. You should use a SQL Agent job for it's execution, where in the job config, you check off this box.

When doing the environment part ---

  1. It is required to close and reopen the SSIS solution otherwise environment variable will not reflect into SSIS package.
  2. User should have admin rights in order to add an environment variable.

7 Comments

Thanks for the quick response but I want to make sure I understand. The following URL points out the difference between system and sql server environment variables. I have the understanding that I should be using Sql Server environment variables. Your answer suggests Windows Environment variables. - mssqltips.com/sqlservertip/4810/…
The verbiage from the URL....Do not mistake the SSIS environment variables of the project deployment model with environment variables of the Windows operating system. In SQL Server 2005 and 2008 we could use those variables to configure our packages as well, in combination with configuration files or tables. This is still possible when you convert your project from the project deployment model to the legacy package deployment model. For the remainder of this tip, we will work with the project deployment model, as it is easier to work with, more flexible and more robust.
By the way...the deployment is utilizing the Project Deployment Model as well as Project Parameters.
I put the Windows in just as an example of common places folks would look for Env Var, but the article pointer is SQL specific, which is why I included it vs going down a long thread about Win var vs other var locations. When you get to the lower steps (23 specifically) in the article, the author also points out the Win var location.
Powershell error setting Environment variable for an SSIS package execution <- This is a different issue than the one I am experiencing. I am using the Project Deployment Model. My Sql Server Environment variables are setting up successfully. The Powershell script does not error at all. The problem is once the Package is deployed, the Project Parameters can not see the Sql Server Environment variables that are set up. You have to manually click the Environment variable check box when the package is executed. In doing so the Package executes successfully but not practical in production.
|
0

You're doing everything right for deployment, creating environment, all that.

It's the final step where you're attempting to run the package that is generating the error.

You have this tsql

Declare @execution_id bigint

EXEC [SSISDB].[catalog].[create_execution] @package_name=N'Package.dtsx', 
@execution_id=@execution_id OUTPUT, @folder_name=N'PowerShell Demos', 
@project_name=N'TestProject', @use32bitruntime=False, @reference_id=1
Select @execution_id

DECLARE @var0 smallint = 1
EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id,  
@object_type=50, @parameter_name=N'LOGGING_LEVEL', @parameter_value=@var0

EXEC [SSISDB].[catalog].[start_execution] @execution_id

We create a reference to the project/package for execution and identify the environment we want to reference (@reference_id = 1)

Then we set the logging level (set_execution_parameter_value) and run it. What remains is to set the execution parameter value for this execution to use the reference/environment value.

I don't have a project handy to provide an example of those set_execution_parameter_value but you can click through the execute package wizard in SSMS and after specifying the "use environment value" for those connection managers, click the script to new window option

2 Comments

First, thank you very much!! I am a little confused about where you are suggesting I generate the script. If you are saying generate the script once the checkbox is checked, that is where I got the T-SQL I documented in my question. It is the same T-SQL you posted in this answer. So there is another script I have not found? Sorry I am misunderstanding. I feel like we are very close to nailing this down. Thanks again!
billinkc...I thought hard about your response last night. I updated my question today hoping you'll understand and be able to correct my misunderstanding. I apologize if my pursuit has gotten frustrating. I am trying my best to be clear. Thank you Sir!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.