Octopus.Script exported 2019-10-29 by Zogamorph belongs to ‘SQL Server’ category.
A Template which will deploy any .ISPAC files from the published package step selected to the SQL Server catalog database.
The template will create a environment config and use variables that are wrapped in SSIS[
When the SSIS Parameter Path is at the Package level then SSIS Parameter will need to have the Package Name (without .dtsx) added to front of the SSIS Parameter path.
To remove and environment config delete the variable or remove the SSIS[].
Parameters
When steps based on the template are included in a project’s deployment process, the parameters below can be set.
NuGet package step
NugetPackageStepName =
The step that uploaded the NuGet package to the server.
SSIS SQL Server Name
ServerName =
The name of SSIS SQL Server
SSISDB Catlog Name
CatalogName = SSISDB
The name of the SSISDB Catlog
SSIS Folder
FolderName =
The SSIS folder to deploy to project to
Environment Tag
EnvironmentTag = #{Octopus.Environment.Name}
The text to add at the end Environment configuration name. Currently default to append the Environment Value begin deploy to.
Script body
Steps based on this template will execute the following PowerShell script.
# Define PowerShell Modules path
$LocalModules = (New-Item "$PSScriptRoot\Modules" -ItemType Directory -Force).FullName
$env:PSModulePath = "$LocalModules;$env:PSModulePath"
#region Functions
#region SQLPS functions
function Get-NugetPackageProviderNotInstalled {
# See if the nuget package provider has been installed
return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))
}
function Get-SqlModuleInstalled {
# Define parameters
param(
$PowerShellModuleName
)
# Check to see if the module is installed
if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName)) {
# It is installed
return $true
}
else {
# Module not installed
return $false
}
}
function Install-SqlServerPowerShellModule {
# Define parameters
param(
$PowerShellModuleName,
$LocalModulesPath
)
# Check to see if the package provider has been installed
if ((Get-NugetPackageProviderNotInstalled) -ne $false) {
# Display that we need the nuget package provider
Write-Host "Nuget package provider not found, installing ..."
# Install Nuget package provider
Install-PackageProvider -Name Nuget -Force
}
# Save the module in the temporary location
Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force
# Display
Write-Output "Importing module $PowerShellModuleName ..."
# Import the module
Import-Module -Name $PowerShellModuleName
}
Function Get-SqlServerAssmblies {
# Declare parameters
# Get the folder where the SqlServer module ended up in
$sqlServerModulePath = [System.IO.Path]::GetDirectoryName((Get-Module -ListAvailable -Name "SqlServer").Path)
# Loop through the assemblies
foreach ($assemblyFile in (Get-ChildItem -Path $sqlServerModulePath -Exclude msv*.dll | Where-Object { $_.Extension -eq ".dll" })) {
# Load the assembly
[Reflection.Assembly]::LoadFile($assemblyFile.FullName) | Out-Null
}
}
#endregion SQLPS functions
#region SQL Server functions
function Get-ConnectionObject {
param
(
[parameter(ParameterSetName = 'Default', HelpMessage = 'Name of the server', Mandatory = $true)]
[parameter(ParameterSetName = 'SQLAuth', HelpMessage = 'Name of the server', Mandatory = $true)]
[string]
$SQLServerName,
[parameter(ParameterSetName = 'SQLAuth')]
[switch] $UseSQLAuth,
[parameter(ParameterSetName = 'SQLAuth', HelpMessage = 'SQL Server Login', Mandatory = $true)]
[string] $SQLLogin,
[parameter(ParameterSetName = 'SQLAuth', HelpMessage = 'SQL Server Password', Mandatory = $true)]
[string] $Password
)
# Create a connection to the server
$sqlConnectionString = "Data Source=$ServerName;Initial Catalog=SSISDB;"
if ($UseSQLAuth) {
# Add username and password to connection string
$sqlConnectionString += "User ID=$SQLLogin; Password=$Password;"
}
else {
# Use integrated
$sqlConnectionString += "Integrated Security=SSPI;"
}
# Create new connection object with connection string
return (New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString)
}
#endregion SQL Server functions
#region SQL SSIS functions
Function Get-Catalog {
# define parameters
Param ($CatalogName)
# define working varaibles
$Catalog = $null
# check to see if there are any catalogs
if ($integrationServices.Catalogs.Count -eq 0 -or -not $integrationServices.Catalogs[$CatalogName]) {
throw "SSIS Catalog not found $CatalogName"
# get reference to catalog
}
$Catalog = $integrationServices.Catalogs[$CatalogName]
# return the catalog
return $Catalog
}
Function Get-Folder {
# parameters
Param($FolderName, $Catalog)
$Folder = $null
# try to get reference to folder
if (!($null -eq $Catalog.Folders)) {
$Folder = $Catalog.Folders[$FolderName]
}
# check to see if $Folder has a value
if ($null -eq $Folder) {
# display
Write-Host "Folder $FolderName doesn't exist, creating folder..."
# create the folder
$Folder = New-Object "$ISNamespace.CatalogFolder" ($Catalog, $FolderName, $FolderName)
$Folder.Create()
}
# return the folde reference
return $Folder
}
Function Get-Environment {
# define parameters
Param($Folder, $EnvironmentName)
$Environment = $null
# get reference to Environment
if (!($null -eq $Folder.Environments) -and $Folder.Environments.Count -gt 0) {
$Environment = $Folder.Environments[$EnvironmentName]
}
# check to see if it's a null reference
if ($null -eq $Environment) {
# display
Write-Host "Environment $EnvironmentName doesn't exist, creating environment..."
# create environment
$Environment = New-Object "$ISNamespace.EnvironmentInfo" ($Folder, $EnvironmentName, $EnvironmentName)
$Environment.Create()
}
# return the environment
return $Environment
}
Function Set-PojectEnvironmentReference {
# define parameters
Param($Project, $Environment, $Folder)
# get reference
$Reference = $null
if (!($null -eq $Project.References)) {
$Reference = $Project.References[$Environment.Name, $Folder.Name]
}
# check to see if it's a null reference
if ($null -eq $Reference) {
# display
Write-Host "Removeing old Project reference environment creating reference..."
foreach ( $Reference in $Project.References) {
$Project.References.Remove($Reference.Name, $Reference.EnvironmentFolderName)
}
Write-Host "Project does not reference environment $($Environment.Name), creating reference..."
# create reference
$Project.References.Add($Environment.Name, $Folder.Name)
$Project.Alter()
}
}
Function Add-EnvironmentVariable {
# define parameters
Param($Environment, $Parameter, $ParameterName, $EnvironmentValue )
# display
Write-Host "- Adding environment variable $($ParameterName)"
# add variable with an initial value
$Environment.Variables.Add($ParameterName, $Parameter.DataType, $EnvironmentValue, $Parameter.Sensitive, $Parameter.Description)
$Parameter.Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced, $ParameterName)
}
Function Remove-ReferencedValue {
Param($Project
, $ParameterName
)
if ($ParameterName -match "\|") {
$ParameterPath = $ParameterName.Split("|")
$Project.Packages[$ParameterPath[0]].Parameters[$ParameterPath[1]].Clear()
$Project.Packages[$ParameterPath[0]].Alter()
}
else {
$Project.Parameters[$ParameterName].Clear()
}
}
Function Sync-EnvironmentVariables {
# define parameters
Param($Environment
, $Project
, $ReferencedVariables
, $OctopusParameters
, $OctopusSSISParameterKey )
foreach ($VariableToEvaluate in $Environment.Variables) {
$OctopusParameterKey = $OctopusSSISParameterKey -f $VariableToEvaluate.Name
` if (-not $OctopusParameters.Keys.Contains($OctopusParameterKey)) {
Write-Host "- Removing environment variable: $($VariableToEvaluate.Name)"
$Environment.Variables.Remove($VariableToEvaluate) | Out-Null
if ($ReferencedVariables.Keys.Contains($VariableToEvaluate.Name)) {
Remove-ReferencedValue -Project $Project -ParameterName $ReferencedVariables[$VariableToEvaluate.Name]
$ReferencedVariables.Remove($VariableToEvaluate.Name)
}
}
}
foreach ($ReferencedKeyName in $ReferencedVariables.Keys) {
` if (-not $Environment.Variables.Contains($ReferencedKeyName)) {
Write-Host "- Removing Prodect referenace $($ReferencedVariables[$ReferencedKeyName] -replace "\|", " ")"
Remove-ReferencedValue -Project $Project -ParameterName $ReferencedVariables[$ReferencedKeyName]
}
}
$Project.Alter()
# alter the environment
$Environment.Alter()
}
Function Set-ProjectParametersToEnvironmentVariablesReference {
# define parameters
Param($Project
, $Environment
, $OctopusParameters
, $OctopusSSISParameterKey
)
$ReferencedVariables = @{ }
if ($null -eq $Project.Parameters) {
Write-Host "No project parameters exist"
return
}
# loop through project parameters
foreach ($Parameter in $Project.Parameters) {
$OctopusParameterKey = $OctopusSSISParameterKey -f $Parameter.Name
# Add variable to list of variable
if ($OctopusParameters.Keys.Contains($OctopusParameterKey)) {
Write-Host $("Updating Project / Environment {0} Parameter Value ..." -f $Parameter.Name)
$EnvironmentValue = $OctopusParameters[$OctopusParameterKey]
$Variable = $null
if (!($null -eq $Environment.Variables)) {
# get reference to variable
$Variable = $Environment.Variables[$Parameter.Name]
}
# check to see if variable exists
if ($null -eq $Variable) {
# add the environment variable
Add-EnvironmentVariable -Environment $Environment -Parameter $Parameter -ParameterName $Parameter.Name -EnvironmentValue $EnvironmentValue
}
else {
$Variable.value = $EnvironmentValue
}
}
if ($Parameter.ValueType -eq [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced) {
$ReferencedVariables.Add($Parameter.ReferencedVariableName, $Parameter.Name)
}
}
# alter the environment
$Environment.Alter()
$Project.Alter()
return $ReferencedVariables
}
Function Set-PackageVariablesToEnvironmentVariablesReference {
# define parameters
Param($Project
, $Environment
, $OctopusParameters
, $OctopusSSISParameterKey)
$ReferencedVariables = @{ }
# loop through packages in project
foreach ($Package in $Project.Packages) {
# loop through parameters of package
foreach ($Parameter in $Package.Parameters) {
$ParameterName = $Parameter.ObjectName.Replace(".dtsx", "") + "." + $Parameter.Name
$OctopusParameterKey = $OctopusSSISParameterKey -f $ParameterName
# Add variable to list of variable
if ($OctopusParameters.Keys.Contains($OctopusParameterKey)) {
Write-Host $("Updating {0} Package / Environment {1} Parameter Value ..." -f $Parameter.ObjectName, $ParameterName)
$EnvironmentValue = $OctopusParameters[$OctopusParameterKey]
$Variable = $null
if (!($null -eq $Environment.Variables)) {
# get reference to variable
$Variable = $Environment.Variables[$ParameterName]
}
# check to see if the parameter exists
if (!$Variable) {
# add the environment variable
Add-EnvironmentVariable -Environment $Environment -Parameter $Parameter -ParameterName $ParameterName -EnvironmentValue $EnvironmentValue
}
else {
$Variable.value = $EnvironmentValue
}
}
if ($Parameter.ValueType -eq [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced) {
$ReferencedVariables.Add($Parameter.ReferencedVariableName, $("{0}|{1}" -f $Package.Name, $Parameter.Name))
}
}
# alter the package
$Package.Alter()
}
# alter the environment
$Environment.Alter()
return $ReferencedVariables
}
#endregion SQL SSIS functions
#endregion Functions
#region Script Main
#region Get enviroment configured
if ((Get-SqlModuleInstalled -PowerShellModuleName "SqlServer") -ne $true) {
# Display message
Write-Output "PowerShell module SqlServer not present, downloading temporary copy ..."
# Download and install temporary copy
Install-SqlServerPowerShellModule -PowerShellModuleName "SqlServer" -LocalModulesPath $LocalModules
}
# Dependent assemblies
Get-SqlServerAssmblies
# add snapins-- applies to sql server 2008 r2, newer version of SQL do not require this.
Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue
Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue
#endregion Get enviroment configured
$ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"
$OctopusSSISParameterKey = "SSIS[{0}]"
$DeployedPath = $OctopusParameters["Octopus.Action[$NugetPackageStepName].Output.Package.InstallationDirectoryPath"]
Write-Host "Connecting to server ..."
if ([string]::IsNullOrEmpty($sqlAccountUsername) -and [string]::IsNullOrEmpty($sqlAccountPassword)) {
# Add username and password to connection string
$sqlConnection = Get-ConnectionObject -SQLServerName $ServerName
}
else {
# Use integrated
$sqlConnection = Get-ConnectionObject -SQLServerName $ServerName -UseSQLAuth -SQLLogin $sqlAccountUsername -Password=$sqlAccountPassword
}
$integrationServices = New-Object "$ISNamespace.IntegrationServices" $sqlConnection
$IsPacFiles = Get-ChildItem -Recurse -Path $DeployedPath | Where-Object { $_.Extension.ToLower() -eq ".ispac" }
Write-Host "$($IsPacFiles.Count) .ispac file(s) found."
# get reference to the catalog
Write-Host "Getting reference to catalog $CatalogName"
$Catalog = Get-Catalog -CatalogName $CatalogName
$Folder = Get-Folder -FolderName $FolderName -Catalog $Catalog
$ReferencedVariables = @{ }
foreach ($IsPacFile in $IsPacFiles) {
$ProjectFile = [System.IO.File]::ReadAllBytes($IsPacFile.FullName)
$ProjectName = $IsPacFile.Name -replace $IsPacFile.Extension , ""
$EnvironmentName = "{0}_{1}" -f $ProjectName , $EnvironmentTag
# deploy project
Write-Host "Deploying project $($IsPacFile.Name)..."
$Folder.DeployProject($ProjectName, $ProjectFile) | Out-Null
# get reference to deployed project
$Project = $Folder.Projects[$ProjectName]
# get environment reference
$Environment = Get-Environment -Folder $Folder -EnvironmentName $EnvironmentName
Set-PojectEnvironmentReference -Project $Project -Environment $Environment -Folder $Folder
Write-Host "Referencing Project Parameters to Environment Variables..."
$ReferencedVariables += Set-ProjectParametersToEnvironmentVariablesReference -Project $Project -Environment $Environment -OctopusParameters $OctopusParameters -OctopusSSISParameterKey $OctopusSSISParameterKey
Write-Host "Referencing Project Parameters to Environment Variables..."
$ReferencedVariables += Set-PackageVariablesToEnvironmentVariablesReference -Project $Project -Environment $Environment -OctopusParameters $OctopusParameters -OctopusSSISParameterKey $OctopusSSISParameterKey
Write-Host "Sync package environment variables..."
Sync-EnvironmentVariables -Environment $Environment -Project $Project -ReferencedVariables $ReferencedVariables -OctopusParameters $OctopusParameters -OctopusSSISParameterKey $OctopusSSISParameterKey
}
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "b791c0c2-03ce-40ef-9446-78beb585c0b8",
"Name": "SSIS deploy ISPAC With Environment",
"Description": "A Template which will deploy any .ISPAC files from the published package step selected to the SQL Server catalog database. \n\nThe template will create a environment config and use variables that are wrapped in SSIS[<SSIS Parameter Path>] extracting the SSIS Parameter Path using that as the name and value linked the Octopus Variable and then assign parameter in the SSIS project to the name environment configuration. \n\nWhen the SSIS Parameter Path is at the Package level then SSIS Parameter will need to have the Package Name (without .dtsx) added to front of the SSIS Parameter path.\n\nTo remove and environment config delete the variable or remove the SSIS[].\n",
"Version": 18,
"ExportedAt": "2019-10-29T18:58:17.799Z",
"ActionType": "Octopus.Script",
"Author": "Zogamorph",
"Packages": [],
"Parameters": [
{
"Id": "4d49a1f7-f7bf-40e2-b845-8c9c8e4deda9",
"Name": "NugetPackageStepName",
"Label": "NuGet package step",
"HelpText": "The step that uploaded the NuGet package to the server.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "StepName"
}
},
{
"Id": "6e97bd67-aeb5-4a92-b4d8-9e5b9a13ad93",
"Name": "ServerName",
"Label": "SSIS SQL Server Name",
"HelpText": "The name of SSIS SQL Server ",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "6b0007eb-81eb-459f-b8c6-2f8c9465beb2",
"Name": "CatalogName",
"Label": "SSISDB Catlog Name",
"HelpText": "The name of the SSISDB Catlog",
"DefaultValue": "SSISDB",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "00cadc4e-17bd-41bb-b4bd-87ca01b483eb",
"Name": "FolderName",
"Label": "SSIS Folder",
"HelpText": "The SSIS folder to deploy to project to",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "5e88fe40-d502-4e2e-8c96-c7ed8f2d82c4",
"Name": "EnvironmentTag",
"Label": "Environment Tag",
"HelpText": "The text to add at the end Environment configuration name. Currently default to append the Environment Value begin deploy to.",
"DefaultValue": "#{Octopus.Environment.Name}",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"Properties": {
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "# Define PowerShell Modules path\n$LocalModules = (New-Item \"$PSScriptRoot\\Modules\" -ItemType Directory -Force).FullName\n$env:PSModulePath = \"$LocalModules;$env:PSModulePath\"\n\n#region Functions\n\n#region SQLPS functions \nfunction Get-NugetPackageProviderNotInstalled {\n # See if the nuget package provider has been installed\n return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\nfunction Get-SqlModuleInstalled {\n # Define parameters\n param(\n $PowerShellModuleName\n )\n\n # Check to see if the module is installed\n if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName)) {\n # It is installed\n return $true\n }\n else {\n # Module not installed\n return $false\n }\n}\n\nfunction Install-SqlServerPowerShellModule {\n # Define parameters\n param(\n $PowerShellModuleName,\n $LocalModulesPath\n )\n\n # Check to see if the package provider has been installed\n if ((Get-NugetPackageProviderNotInstalled) -ne $false) {\n # Display that we need the nuget package provider\n Write-Host \"Nuget package provider not found, installing ...\"\n \n # Install Nuget package provider\n Install-PackageProvider -Name Nuget -Force\n }\n\n # Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\n\n # Display\n Write-Output \"Importing module $PowerShellModuleName ...\"\n\n # Import the module\n Import-Module -Name $PowerShellModuleName\n}\n\nFunction Get-SqlServerAssmblies {\n # Declare parameters\n \n # Get the folder where the SqlServer module ended up in\n $sqlServerModulePath = [System.IO.Path]::GetDirectoryName((Get-Module -ListAvailable -Name \"SqlServer\").Path)\n \n # Loop through the assemblies\n foreach ($assemblyFile in (Get-ChildItem -Path $sqlServerModulePath -Exclude msv*.dll | Where-Object { $_.Extension -eq \".dll\" })) {\n # Load the assembly\n [Reflection.Assembly]::LoadFile($assemblyFile.FullName) | Out-Null\n } \n}\n\n#endregion SQLPS functions \n\n#region SQL Server functions \nfunction Get-ConnectionObject {\n param\n (\n [parameter(ParameterSetName = 'Default', HelpMessage = 'Name of the server', Mandatory = $true)]\n [parameter(ParameterSetName = 'SQLAuth', HelpMessage = 'Name of the server', Mandatory = $true)]\n [string]\n $SQLServerName,\n [parameter(ParameterSetName = 'SQLAuth')]\n [switch] $UseSQLAuth,\n [parameter(ParameterSetName = 'SQLAuth', HelpMessage = 'SQL Server Login', Mandatory = $true)]\n [string] $SQLLogin,\n [parameter(ParameterSetName = 'SQLAuth', HelpMessage = 'SQL Server Password', Mandatory = $true)]\n [string] $Password\n \n ) \n # Create a connection to the server\n $sqlConnectionString = \"Data Source=$ServerName;Initial Catalog=SSISDB;\"\n \n if ($UseSQLAuth) {\n # Add username and password to connection string\n $sqlConnectionString += \"User ID=$SQLLogin; Password=$Password;\"\n }\n else {\n # Use integrated\n $sqlConnectionString += \"Integrated Security=SSPI;\"\n }\n \n # Create new connection object with connection string\n return (New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString)\n}\n#endregion SQL Server functions \n\n#region SQL SSIS functions\nFunction Get-Catalog {\n # define parameters\n Param ($CatalogName)\n \n # define working varaibles\n $Catalog = $null\n # check to see if there are any catalogs\n if ($integrationServices.Catalogs.Count -eq 0 -or -not $integrationServices.Catalogs[$CatalogName]) {\n throw \"SSIS Catalog not found $CatalogName\"\n # get reference to catalog\n }\n\n $Catalog = $integrationServices.Catalogs[$CatalogName]\n \n # return the catalog\n return $Catalog\n}\n\nFunction Get-Folder {\n # parameters\n Param($FolderName, $Catalog)\n \n $Folder = $null\n # try to get reference to folder\n \n if (!($null -eq $Catalog.Folders)) {\n $Folder = $Catalog.Folders[$FolderName]\n }\n \n # check to see if $Folder has a value\n if ($null -eq $Folder) {\n # display\n Write-Host \"Folder $FolderName doesn't exist, creating folder...\"\n \n # create the folder\n $Folder = New-Object \"$ISNamespace.CatalogFolder\" ($Catalog, $FolderName, $FolderName) \n $Folder.Create() \n }\n \n # return the folde reference\n return $Folder\n}\n\nFunction Get-Environment {\n # define parameters\n Param($Folder, $EnvironmentName)\n \n $Environment = $null\n # get reference to Environment\n if (!($null -eq $Folder.Environments) -and $Folder.Environments.Count -gt 0) {\n $Environment = $Folder.Environments[$EnvironmentName]\n }\n \n # check to see if it's a null reference\n if ($null -eq $Environment) {\n # display\n Write-Host \"Environment $EnvironmentName doesn't exist, creating environment...\"\n \n # create environment\n $Environment = New-Object \"$ISNamespace.EnvironmentInfo\" ($Folder, $EnvironmentName, $EnvironmentName)\n $Environment.Create() \n }\n \n # return the environment\n return $Environment\n}\nFunction Set-PojectEnvironmentReference {\n # define parameters\n Param($Project, $Environment, $Folder)\n \n # get reference\n $Reference = $null\n \n if (!($null -eq $Project.References)) {\n $Reference = $Project.References[$Environment.Name, $Folder.Name]\n \n }\n\n # check to see if it's a null reference\n if ($null -eq $Reference) {\n # display\n Write-Host \"Removeing old Project reference environment creating reference...\"\n foreach ( $Reference in $Project.References) {\n $Project.References.Remove($Reference.Name, $Reference.EnvironmentFolderName)\n }\n Write-Host \"Project does not reference environment $($Environment.Name), creating reference...\"\n # create reference\n $Project.References.Add($Environment.Name, $Folder.Name)\n $Project.Alter() \n }\n}\n\nFunction Add-EnvironmentVariable {\n # define parameters\n Param($Environment, $Parameter, $ParameterName, $EnvironmentValue )\n \n # display \n Write-Host \"- Adding environment variable $($ParameterName)\"\n \n # add variable with an initial value\n $Environment.Variables.Add($ParameterName, $Parameter.DataType, $EnvironmentValue, $Parameter.Sensitive, $Parameter.Description)\n $Parameter.Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced, $ParameterName)\n}\n\nFunction Remove-ReferencedValue {\n Param($Project\n , $ParameterName\n )\n \n if ($ParameterName -match \"\\|\") {\n $ParameterPath = $ParameterName.Split(\"|\")\n $Project.Packages[$ParameterPath[0]].Parameters[$ParameterPath[1]].Clear()\n $Project.Packages[$ParameterPath[0]].Alter()\n }\n else {\n $Project.Parameters[$ParameterName].Clear() \n }\n}\n\nFunction Sync-EnvironmentVariables {\n # define parameters\n Param($Environment\n , $Project\n , $ReferencedVariables\n , $OctopusParameters\n , $OctopusSSISParameterKey )\n\n foreach ($VariableToEvaluate in $Environment.Variables) {\n $OctopusParameterKey = $OctopusSSISParameterKey -f $VariableToEvaluate.Name\n` if (-not $OctopusParameters.Keys.Contains($OctopusParameterKey)) {\n Write-Host \"- Removing environment variable: $($VariableToEvaluate.Name)\" \n $Environment.Variables.Remove($VariableToEvaluate) | Out-Null\n if ($ReferencedVariables.Keys.Contains($VariableToEvaluate.Name)) {\n Remove-ReferencedValue -Project $Project -ParameterName $ReferencedVariables[$VariableToEvaluate.Name]\n $ReferencedVariables.Remove($VariableToEvaluate.Name)\n }\n }\n }\n\n foreach ($ReferencedKeyName in $ReferencedVariables.Keys) {\n` if (-not $Environment.Variables.Contains($ReferencedKeyName)) {\n Write-Host \"- Removing Prodect referenace $($ReferencedVariables[$ReferencedKeyName] -replace \"\\|\", \" \")\"\n Remove-ReferencedValue -Project $Project -ParameterName $ReferencedVariables[$ReferencedKeyName] \n }\n }\n\n $Project.Alter()\n # alter the environment\n $Environment.Alter()\n}\n\nFunction Set-ProjectParametersToEnvironmentVariablesReference {\n # define parameters\n Param($Project\n , $Environment\n , $OctopusParameters\n , $OctopusSSISParameterKey\n )\n \n $ReferencedVariables = @{ }\n\n if ($null -eq $Project.Parameters) {\n Write-Host \"No project parameters exist\"\n return\n }\n\n # loop through project parameters\n foreach ($Parameter in $Project.Parameters) {\n $OctopusParameterKey = $OctopusSSISParameterKey -f $Parameter.Name\n # Add variable to list of variable\n if ($OctopusParameters.Keys.Contains($OctopusParameterKey)) {\n Write-Host $(\"Updating Project / Environment {0} Parameter Value ...\" -f $Parameter.Name)\n $EnvironmentValue = $OctopusParameters[$OctopusParameterKey]\n $Variable = $null\n if (!($null -eq $Environment.Variables)) {\n # get reference to variable\n $Variable = $Environment.Variables[$Parameter.Name]\n }\n # check to see if variable exists\n if ($null -eq $Variable) {\n # add the environment variable\n Add-EnvironmentVariable -Environment $Environment -Parameter $Parameter -ParameterName $Parameter.Name -EnvironmentValue $EnvironmentValue \n }\n else {\n $Variable.value = $EnvironmentValue\n } \n }\n if ($Parameter.ValueType -eq [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced) {\n $ReferencedVariables.Add($Parameter.ReferencedVariableName, $Parameter.Name)\n }\n }\n \n # alter the environment\n $Environment.Alter()\n $Project.Alter()\n return $ReferencedVariables\n}\n\nFunction Set-PackageVariablesToEnvironmentVariablesReference {\n # define parameters\n Param($Project\n , $Environment \n , $OctopusParameters\n , $OctopusSSISParameterKey)\n\n $ReferencedVariables = @{ }\n\n # loop through packages in project\n foreach ($Package in $Project.Packages) {\n # loop through parameters of package\n foreach ($Parameter in $Package.Parameters) {\n \n $ParameterName = $Parameter.ObjectName.Replace(\".dtsx\", \"\") + \".\" + $Parameter.Name\n $OctopusParameterKey = $OctopusSSISParameterKey -f $ParameterName\n \n # Add variable to list of variable\n if ($OctopusParameters.Keys.Contains($OctopusParameterKey)) {\n Write-Host $(\"Updating {0} Package / Environment {1} Parameter Value ...\" -f $Parameter.ObjectName, $ParameterName)\n $EnvironmentValue = $OctopusParameters[$OctopusParameterKey]\n $Variable = $null\n if (!($null -eq $Environment.Variables)) {\n # get reference to variable\n $Variable = $Environment.Variables[$ParameterName]\n }\n # check to see if the parameter exists\n if (!$Variable) {\n # add the environment variable\n Add-EnvironmentVariable -Environment $Environment -Parameter $Parameter -ParameterName $ParameterName -EnvironmentValue $EnvironmentValue\n } \n else {\n $Variable.value = $EnvironmentValue\n } \n }\n if ($Parameter.ValueType -eq [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced) {\n $ReferencedVariables.Add($Parameter.ReferencedVariableName, $(\"{0}|{1}\" -f $Package.Name, $Parameter.Name))\n } \n }\n \n # alter the package\n $Package.Alter()\n }\n \n # alter the environment\n $Environment.Alter()\n\n return $ReferencedVariables\n}\n\n#endregion SQL SSIS functions\n\n#endregion Functions\n\n#region Script Main\n\n#region Get enviroment configured\nif ((Get-SqlModuleInstalled -PowerShellModuleName \"SqlServer\") -ne $true) {\n # Display message\n Write-Output \"PowerShell module SqlServer not present, downloading temporary copy ...\"\n \n # Download and install temporary copy\n Install-SqlServerPowerShellModule -PowerShellModuleName \"SqlServer\" -LocalModulesPath $LocalModules\n}\n\n# Dependent assemblies\nGet-SqlServerAssmblies \n\n\n# add snapins-- applies to sql server 2008 r2, newer version of SQL do not require this.\nAdd-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue\nAdd-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue\n#endregion Get enviroment configured\n\n\n$ISNamespace = \"Microsoft.SqlServer.Management.IntegrationServices\"\n$OctopusSSISParameterKey = \"SSIS[{0}]\"\n$DeployedPath = $OctopusParameters[\"Octopus.Action[$NugetPackageStepName].Output.Package.InstallationDirectoryPath\"]\n\nWrite-Host \"Connecting to server ...\"\n\nif ([string]::IsNullOrEmpty($sqlAccountUsername) -and [string]::IsNullOrEmpty($sqlAccountPassword)) {\n # Add username and password to connection string\n $sqlConnection = Get-ConnectionObject -SQLServerName $ServerName \n}\nelse {\n # Use integrated\n $sqlConnection = Get-ConnectionObject -SQLServerName $ServerName -UseSQLAuth -SQLLogin $sqlAccountUsername -Password=$sqlAccountPassword\n}\n\n$integrationServices = New-Object \"$ISNamespace.IntegrationServices\" $sqlConnection\n\n\n$IsPacFiles = Get-ChildItem -Recurse -Path $DeployedPath | Where-Object { $_.Extension.ToLower() -eq \".ispac\" }\nWrite-Host \"$($IsPacFiles.Count) .ispac file(s) found.\"\n\n# get reference to the catalog\nWrite-Host \"Getting reference to catalog $CatalogName\"\n$Catalog = Get-Catalog -CatalogName $CatalogName\n$Folder = Get-Folder -FolderName $FolderName -Catalog $Catalog\n\n$ReferencedVariables = @{ }\nforeach ($IsPacFile in $IsPacFiles) {\n $ProjectFile = [System.IO.File]::ReadAllBytes($IsPacFile.FullName)\n $ProjectName = $IsPacFile.Name -replace $IsPacFile.Extension , \"\"\n $EnvironmentName = \"{0}_{1}\" -f $ProjectName , $EnvironmentTag\n # deploy project\n Write-Host \"Deploying project $($IsPacFile.Name)...\"\n $Folder.DeployProject($ProjectName, $ProjectFile) | Out-Null\n\n # get reference to deployed project\n $Project = $Folder.Projects[$ProjectName]\n # get environment reference\n $Environment = Get-Environment -Folder $Folder -EnvironmentName $EnvironmentName \n Set-PojectEnvironmentReference -Project $Project -Environment $Environment -Folder $Folder \n Write-Host \"Referencing Project Parameters to Environment Variables...\"\n $ReferencedVariables += Set-ProjectParametersToEnvironmentVariablesReference -Project $Project -Environment $Environment -OctopusParameters $OctopusParameters -OctopusSSISParameterKey $OctopusSSISParameterKey\n Write-Host \"Referencing Project Parameters to Environment Variables...\"\n $ReferencedVariables += Set-PackageVariablesToEnvironmentVariablesReference -Project $Project -Environment $Environment -OctopusParameters $OctopusParameters -OctopusSSISParameterKey $OctopusSSISParameterKey\n Write-Host \"Sync package environment variables...\"\n Sync-EnvironmentVariables -Environment $Environment -Project $Project -ReferencedVariables $ReferencedVariables -OctopusParameters $OctopusParameters -OctopusSSISParameterKey $OctopusSSISParameterKey\n} "
},
"Category": "SQL Server",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/ssis-deploy-ispac-with-enviroment.json",
"Website": "/step-templates/b791c0c2-03ce-40ef-9446-78beb585c0b8",
"Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD9QTFRFlZ+r3DAr6p+dy8/V4G9t////5efp9M7NrLS+wCYm8/T1vcPK1tnd10xK+fn6/PLyUU5O+eXk3+Hk7O3u7/DxS2XoPwAADb9JREFUeNrsnYl6nbgOgMEYDHghGN7/Wa8k70B6s3AOZD5o2umcSaf+0S4bUbX/kat6QB6QB+QBeUAekAfkAXlAHpAH5AF5QB6QB+QBeUAekAckXMv4XwBZVCPVnwcZlZSNXRrzp0HGTkqplrY1zfKHQboGMZwoGvVXQUbVy152QaPUu3XrJJCl6Xsp1/SBfbdunQJiZd/3zVqqmfprIEb1iLHRpLF5s279FsQ0iCH3etQ03R8CQYyq74/MwTbN3wGxQFGRRJTaJiVL815z/wXIIiviQEunq2lsNyZhvdfcfw6iCMPavl9H20jkgV8gP1F2NRRJmvEvgIA4gAS0B8xkpexEYWB3F0ijAyOxfwAkcsBvHQk53QWW71HwGm8PIhJHazIS98HYdUqBar1TJD8EYQOABGNe+w0J0dj3iuSHIOMw6PRHOyDpdhggE2XvDmLYAChsDh4MSPI1g92DWkGaosbbey0kARbOyFCaTCYgDemioQWp3D+O9EO4NGNCRpIFMKQzjlG9TyS/iOwoE64jjeaVwICOzjeoGfgue38QshPRMV57lhpVjbNemZTMK7X+gaQRSRgQzaz2JDX9CjRiDvWV+gMgRniSltWMMV0TSo1fcIEjEAKUa7k/CDiomkjaeeAU8JEmoRAOuoLp/hWidTJp9RBiipkF07our9fj/Lpmn51MeM2TnAx5gnp/cRZj6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mXjVOjYRTFXaC0G8SIKb2lbvnYzlFU2PX7y977TotZr1FZDFk7ipnoWhLzJUJqBO1BmiXpYfxVyuGzdNzKUglMgHmWQRfWloSDmkYW6BaZwppryeJenYi8eBfqn50ESZNMFARuUyYhnbV2qbBVuXpjQuczdF+nhVO6j3JIszENO4MCkzmx59C3VbpvuWtrUvHr/+9QZdcMPGyUJu2gtyN4U5erV1wZHlLx7H/NWWaRNAKK3fh2572IaIFkNiMXcACb4LKI5KCih8q+PH7QxVV0v36pHlX99WMLLaBfmi8D2I5ytOlZYY6ZtXv2rhOztWNghlp1gdvpxgr1ApnR9f/qaFb+0hRqFsh6tjMNmJIo+J9uWvI/nm9vQaUfIb3JQG0imXz2fRsHn5C2K+e2DArH1QsNhvGKuUR462OWhsr/Llbyf4yaEaGR2Yu83gsVaftLgMUtqN4b/hFR4/O69lk1iUsVTTG+VFofbbz+YN73776VFAH99dG1Iu7l09Uh1bdCdf/wqlXxyXHRML5sD/GBD/jpfx/fJsvOttu589vnXv2KhAIBgYQQNfNg//hBdyQcio+vCjxxpks1gLApmqj+rjox0/5G1BgteVfbaPhTjR6Okwl/kAFtl/9PcGyWqpPutEYFW1dM5CAARkcneJlDwLlVP+dVDhMNdHW8mP45TzriBZ7k+Xi4W9kbMS0v5JkDdeD8gD8oA8IA/IA/KAPCAPyAPygDwgD8gD8oA8IA/IA/IXr/8JMAAhf0RDrOWy2QAAAABJRU5ErkJggg==",
"$Meta": {
"Type": "ActionTemplate"
}
}
Page updated on Tuesday, October 29, 2019