├── LICENSE ├── README.md ├── SSIS_Deployments_Config.xml ├── SSIS_Functions.ps1 └── Sample_SSIS_Deploy_Script.ps1 /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ben Sylve 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # powershell-for-ssis 2 | This code has a few functions that help automate some common SSIS tasks including: 3 | 4 | * Deploying an ispac file from an SSIS project "build" (deploySSISProject) 5 | * Creating folders in SSIS running on a SQL Server (createSSISFolder) 6 | * Creating an SSIS Environment (createSSISEnvironment) 7 | * Creating SSIS Environment variables (createEnvironmentVariable) 8 | * Adding a reference from an SSIS project to an SSIS Environment (addEnvironmentReferenceToSSISProject) 9 | * Mapping an SSIS Project Parameter to an SSIS Environement Variable (setProjectParameterToEnvironmentVariable) 10 | 11 | This would be used in a workflow such as: 12 | 13 | 1. Create a folder in SSIS to hold my project. 14 | 2. Create one or more environments that will hold the configuration options for my project. 15 | 3. Setup the environment variables to store the configured values for my project. 16 | 4. Deploy the project to the folder that was created. 17 | 5. Set the project to reference the environment. 18 | 6. Map the project parameters to the environment variables. 19 | 20 | These steps when done manually can be a pain. These scripts help to ease that pain by making the process repeatable. If you take a look at the [sample workflow](Sample_SSIS_Deploy_Script.ps1), it takes an [xml configuration file](SSIS_Deployments_Config.xml) and uses the [functions](SSIS_Functions.ps1) to automate the SSIS operations. 21 | -------------------------------------------------------------------------------- /SSIS_Deployments_Config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /SSIS_Functions.ps1: -------------------------------------------------------------------------------- 1 | function createEnvironmentVariable($variableName, $variableValue, $environmentName, $sensitive, $catalogName, $folderName, $serverConnectionString) 2 | { 3 | # Load assemblies 4 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; 5 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Management.Smo") | Out-Null; 6 | 7 | $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $serverConnectionString 8 | 9 | $integrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $sqlConnection 10 | 11 | $catalog = $integrationServices.Catalogs[$catalogName] 12 | 13 | $folder = $catalog.Folders[$folderName] 14 | 15 | $environment = $folder.Environments[$environmentName] 16 | 17 | $environmentVariable = $environment.Variables[$variableName]; 18 | 19 | if (!$environmentVariable) 20 | { 21 | $environment.Variables.Add( 22 | $variableName, 23 | [System.TypeCode]::String, $variableValue, [System.Convert]::ToBoolean($sensitive), "") 24 | $environment.Alter() 25 | $environmentVariable = $environment.Variables[$variableName]; 26 | } 27 | else 28 | { 29 | $environmentVariable.Value = $variableValue 30 | $environmentVariable.Sensitive = [System.Convert]::ToBoolean($sensitive) 31 | $environment.Alter() 32 | } 33 | } 34 | 35 | function setProjectParameterToEnvironmentVariable($parameterName, $environmentVariableName, $catalogName, $folderName, $projectName, $environmentName, $serverConnectionString) 36 | { 37 | # Load assemblies 38 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; 39 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Management.Smo") | Out-Null; 40 | 41 | # Create a connection and get the project 42 | $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $serverConnectionString 43 | $integrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $sqlConnection 44 | $catalog = $integrationServices.Catalogs[$catalogName] 45 | $folder = $catalog.Folders[$folderName] 46 | $project = $folder.Projects[$projectName] 47 | $environment = $folder.Environments[$environmentName] 48 | $environmentVariable = $environment.Variables[$environmentVariableName]; 49 | 50 | # Set the project parameter value 51 | $project.Parameters[$parameterName].Set( 52 | [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced, 53 | $environmentVariable.Name) 54 | 55 | $project.Alter() 56 | } 57 | 58 | function createSSISFolder($folderName, $catalogName, $ssisServerConnectionString) 59 | { 60 | # Load assemblies 61 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; 62 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Management.Smo") | Out-Null; 63 | 64 | $ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices" 65 | 66 | $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ssisServerConnectionString 67 | 68 | $integrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $sqlConnection 69 | 70 | $catalog = $integrationServices.Catalogs[$catalogName] 71 | 72 | $folder = $catalog.Folders[$folderName] 73 | 74 | if (!$folder) 75 | { 76 | $folder = New-Object "$ISNamespace.CatalogFolder" ($catalog, $folderName, $folderName) 77 | $folder.Create() 78 | } 79 | } 80 | 81 | function createSSISEnvironment($environmentName, $folderName, $catalogName, $ssisServerConnectionString) 82 | { 83 | # Load assemblies 84 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; 85 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Management.Smo") | Out-Null; 86 | 87 | $ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices" 88 | 89 | $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ssisServerConnectionString 90 | 91 | $integrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $sqlConnection 92 | 93 | $catalog = $integrationServices.Catalogs[$catalogName] 94 | 95 | $folder = $catalog.Folders[$folderName] 96 | 97 | $environment = $folder.Environments[$environmentName] 98 | 99 | if (!$environment) 100 | { 101 | $environment = New-Object "$ISNamespace.EnvironmentInfo" ($folder, $environmentName, $environmentName) 102 | $environment.Create() 103 | } 104 | } 105 | 106 | function deploySSISProject($projectName, $projectFilePath, $folderName, $catalogName, $ssisServerConnectionString) 107 | { 108 | # Load assemblies 109 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; 110 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Management.Smo") | Out-Null; 111 | 112 | $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ssisServerConnectionString 113 | 114 | $integrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $sqlConnection 115 | 116 | $catalog = $integrationServices.Catalogs[$catalogName] 117 | 118 | $folder = $catalog.Folders[$folderName] 119 | 120 | [byte[]] $projectFile = [System.IO.File]::ReadAllBytes($projectFilePath) 121 | $folder.DeployProject($projectName, $projectFile) | Out-Null 122 | } 123 | 124 | function addEnvironmentReferenceToSSISProject($projectName, $environmentName, $folderName, $catalogName, $ssisServerConnectionString) 125 | { 126 | # Load assemblies 127 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null; 128 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Management.Smo") | Out-Null; 129 | 130 | $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ssisServerConnectionString 131 | 132 | $integrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $sqlConnection 133 | 134 | $catalog = $integrationServices.Catalogs[$catalogName] 135 | 136 | $folder = $catalog.Folders[$folderName] 137 | 138 | $environment = $folder.Environments[$environmentName] 139 | 140 | $project = $folder.Projects[$projectName] 141 | 142 | $ref = $project.References[$environmentName, $folder.Name] 143 | 144 | if (!$ref) 145 | { 146 | $project.References.Add($environmentName, $folder.Name) 147 | $project.Alter() 148 | } 149 | } -------------------------------------------------------------------------------- /Sample_SSIS_Deploy_Script.ps1: -------------------------------------------------------------------------------- 1 | Write-Host "" 2 | Write-Host "*************** Beginning SSIS Deployment Process ***************" 3 | Write-Host "" 4 | 5 | # Assumes the powershell scripts are in a sub-folder called POWERSHELL_FUNCTIONS and the xml config is in a subfolder called CONFIG_FILES. 6 | # Also assumes the script being run is in the current powershell directory. 7 | 8 | # Import functions used by the procedure 9 | . '.\POWERSHELL_FUNCTIONS\SSIS_Functions.ps1' 10 | 11 | 12 | # Get xml config file 13 | [xml] $ssisDeploymentsXmlDoc = Get-Content .\CONFIG_FILES\SSIS_Deployments.xml 14 | $ssisDeployments = $ssisDeploymentsXmlDoc.SelectNodes("//SSISDeployment") 15 | 16 | 17 | # Loop through each deployment and setup environments and projects 18 | foreach($ssisDeployment in $ssisDeployments) 19 | { 20 | 21 | # Set connection string to server using windows credentials of the logged on user / process executing the script 22 | $ssisServerConnectionString = "Data Source=" + $ssisDeployment.server + ";Initial Catalog=master;Integrated Security=SSPI;" 23 | 24 | 25 | foreach($ssisFolder in $ssisDeployment.SSISFolder) 26 | { 27 | # Create the folder. It will only get created if it does not exist. 28 | Write-Host "Creating the folder" $ssisFolder.name "..." 29 | createSSISFolder $ssisFolder.name $ssisDeployment.catalog $ssisServerConnectionString 30 | 31 | # Setup the environments. They will only get created if they don't exist. 32 | foreach($ssisEnvironment in $ssisFolder.SSISEnvironment) 33 | { 34 | Write-Host "Updating the environment" $ssisEnvironment.name "..." 35 | createSSISEnvironment $ssisEnvironment.name $ssisFolder.name $ssisDeployment.catalog $ssisServerConnectionString 36 | 37 | # Setup environment variables 38 | Write-Host "Setting up environment variables for" $ssisEnvironment.name "..." 39 | foreach($ssisEnvironmentVariable in $ssisEnvironment.SSISEnvironmentVariable) 40 | { 41 | $sensitive = [System.Convert]::ToBoolean($ssisEnvironmentVariable.sensitive) 42 | createEnvironmentVariable $ssisEnvironmentVariable.name $ssisEnvironmentVariable.value $ssisEnvironment.name $sensitive $ssisDeployment.catalog $ssisFolder.name $ssisServerConnectionString 43 | } 44 | 45 | foreach($ssisSharedEnvironmentVariable in $ssisFolder.SSISEnvironmentSharedVariables.SSISSharedEnvironmentVariable) 46 | { 47 | $sensitive = [System.Convert]::ToBoolean($ssisSharedEnvironmentVariable.sensitive) 48 | createEnvironmentVariable $ssisSharedEnvironmentVariable.name $ssisSharedEnvironmentVariable.value $ssisEnvironment.name $sensitive $ssisDeployment.catalog $ssisFolder.name $ssisServerConnectionString 49 | } 50 | } 51 | 52 | # Setup the projects 53 | foreach($ssisProject in $ssisFolder.SSISProject) 54 | { 55 | Write-Host "Updating the project" $ssisProject.name "..." 56 | $resolvedPath = Resolve-Path $ssisProject.deploymentFile 57 | deploySSISProject $ssisProject.name $resolvedPath $ssisFolder.name $ssisDeployment.catalog $ssisServerConnectionString 58 | 59 | Write-Host "Setting project parameter / environment variable references for" $ssisProject.name "..." 60 | foreach($ssisProjectEnvironmentReference in $ssisProject.SSISProjectEnvironmentReference) 61 | { 62 | addEnvironmentReferenceToSSISProject $ssisProject.name $ssisProjectEnvironmentReference.environment $ssisFolder.name $ssisDeployment.catalog $ssisServerConnectionString 63 | foreach($ssisProjectParameterEnvironmentVariableReference in $ssisProjectEnvironmentReference.SSISProjectParameterEnvironmentVariableReference) 64 | { 65 | setProjectParameterToEnvironmentVariable $ssisProjectParameterEnvironmentVariableReference.projectParameter $ssisProjectParameterEnvironmentVariableReference.environmentVariable $ssisDeployment.catalog $ssisFolder.name $ssisProject.name $ssisProjectEnvironmentReference.environment $ssisServerConnectionString 66 | } 67 | } 68 | } 69 | } 70 | 71 | } 72 | 73 | 74 | Write-Host "" 75 | Write-Host "*************** Deployment Complete ***************" 76 | Write-Host "" --------------------------------------------------------------------------------