Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Deploying Multiple SSIS Projects via PowerShell

$
0
0
Overview

This article describes a way of using PowerShell to deploy multiple SSIS projects as part of a database or data warehouse deployment. It contains code snippets to illustrate some of the details, and so you can amend it for similar requirements if necessary.

System Integration Test

This set of scripts were developed in order to automate the process of setting up a test cell to verify the build via a System integration test. It was done to ensure that all the dependencies between several SSIS projects for the Data Warehouse systems were all accounted for.

Before each test run, the entire Data Warehouse system was provisioned afresh within the SIT test environment.

Solution Overview

In this solution, I will explain how to develop a PowerShell script to deploy multiple SSIS projects / packages. You can use the script straight away to deploy the SSIS artefacts in a test environment. I recommend that you to read all the steps detailed in the article before you do so, and understand the consequence. If required, you may modify the script based on usage. This script has six steps to complete.

What is expected from the script ?

The deployment script will read the Server configuration details (XML) and then deploy the Integration Services Project Deployment (ISPAC) files to the target SSIS server. The server configuration file is an XML file which contains server names for several test environments. This script can be used to deploy the ISPAC files to other test environments such as System Unit Test, System Integration test and Regression Test environments.


Deploying Multiple SSIS Projects via PowerShell
Solution in Detail

The screenshot below represents the steps involved at very high level.


Deploying Multiple SSIS Projects via PowerShell
Assumptions

We’ll assume that the reader is already aware of the SSIS build automation which is detailedhere, and that the reader has got fair understanding of PowerShell.

Step 1: Reading Server Details from a Configuration file

It is always a good idea to maintain the server details in a separate configuration file (an XML). This configuration file can keep the details that are specific to each environment. A sample XML configuration file has been created for demonstration purpose.

<Environments> <EnvironmentName="SIT" ServerInstance="Localhost\SQL2012" SourceFileLocation="\\Server\Data\Files"></Environment> <EnvironmentName="TST" ServerInstance="Localhost\TST2012" SourceFileLocation="\\TSTServer\Data\Files"></Environment> <EnvironmentName="RST" ServerInstance="Localhost\RST2012" SourceFileLocation="\\RSTServer\Data\Files"></Environment> </Environments>

The file maintains the details for SIT, System test and regression test environments. For each environment, we are maintaining such details as the Environment name, Server Instance Name and Source File Location.

There are various advantages to maintaining the details in a separate file. The configuration file can be easily updated if there are any changes to the name of the server or any environment specific values. Also there is no need to change the code to update the environment-specific values.

This PowerShell script will read an XML configuration file and, based on the given target environment, will print the values.

$ConfigPath = "E: \Scripts\ServerConfig.xml"; $TargetEnvironment = "SIT" [xml]$file = Get-Content $ConfigPath $Environments = $file.SelectNodes("/Environments/Environment") Foreach($Environment in $Environments) { If ($Environment.Name -eq $TargetEnvironment) { Write-Host $Environment.Name Write-Host $Environment.ServerInstance Write-Host $Environment.SourceFileLocation } Step 2: Enabling CLR in the server

You must enable the CLR on the SQL Server before you create the catalog on the SSIS server. The following section of the PowerShell script will do this.

$SQLServer = New-Object Microsoft.SQLServer.Management.SMO.Server .\SQL2012 $configuration = $SQLServer.Configuration $CLRValue = $SQLServer.Configuration.IsSqlClrEnabled Write-Host $CLRValue.ConfigValue If ($CLRValue.ConfigValue -eq 0) { Write-Host "Enabling CLR....." $CLRValue.ConfigValue = 1 $configuration.Alter() Step 3: Creating SSIS Catalog in SQL Server

As we are setting up a System Integration Test environment, we should first drop the existing SSIS catalog so as to ensure that the existing projects, packages and environment details are deleted completely.

However if we are planning to reuse the same script for other environments, we might want to comment out the relevant sections. This will help to keep the existing Catalog and deploy the SSIS projects.

This script has three variables that are used to store the values of the password for SQL Server and the SSIS Catalog. The SSIS catalog stores sensitive information such as the package password in the SSIS Catalog. Therefore we must protect the Catalog with a password.

This script will first check whether the CLR is enabled in the server. If not, then the PowerShell script will enable the CLR: Then it will connect to the SQL server. Once the connection has been established, the script will check for the presence of Catalogues in the server.

If the catalog is not available in the first place, then the script will continue to create a catalog on the target server, using the password supplied.

If the catalog is available, then it will raise a warning and it will drop the entire catalog. This means that all the available SSIS projects, packages and environments will be completely removed. You must decide whether this is appropriate in your environment. Once the catalog has been dropped, then it will recreate the Catalog.

Script to create SSIS catalog $SQLServerInstance = "localhost\SQL2012" $SSISCatalogPassword = "PW0Rd4sSsIsCAt10g" $SQLServerConnectionString = "Data Source=$SQLServerInstance;Initial Catalog=master;Integrated Security=SSPI;" # Pre-Requisite - Enable CLR before creating catalog #Connect to SQL server and enable CLR, if this is not already enabled Write-Host "Enabling CLR on the SQL Server ...." $SQLServer = New-Object Microsoft.SQLServer.Management.SMO.Server $SQLServerInstance $configuration = $SQLServer.Configuration $CLRValue = $SQLServer.Configuration.IsSqlClrEnabled Write-Host $CLRValue.ConfigValue If ($CLRValue.ConfigValue -eq 0) { Write-Host "Enabling CLR on the server $SQLServerInstance" $CLRValue.ConfigValue = 1 $configuration.Alter() Write-Host "CLR enabled on the server $SQLServerInstance successfully" } # Loading IntegrationServices Assembly ......... [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; # Store the value of IntegrationServices Assembly name in a string # This will avoid to re type the same string again and again $SSISNamespace = "Microsoft.SqlServer.Management.IntegrationServices" Write-Host "Trying to connect SQL Server...." # Create SQL Server connection based on the connection string $SQLServerConnection = New-Object System.Data.SqlClient.SqlConnection $SQLServerConnectionString Write-Host "SQL Server connection has been enabled successfully" # Create Integration Services object based on the SQL Server connection Write-Host "Trying to create a object for Integration Service...." $IntegrationServices = New-Object $SSISNamespace".IntegrationServices" $SQLServerConnection Write-Host "Integration service object has been created successfully" Write-Host "Dropping existing SSIS Catalog on the server $SQLServerInstance" # Drop the existing catalog if it exists if ($Integr

Viewing all articles
Browse latest Browse all 3160

Trending Articles