Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Copy-SSISEnvironment {
- <#
- .SYNOPSIS
- Copies a source SSIS environment, including all it's variables.
- .DESCRIPTION
- This function copies an SSIS environment. You can use it to quickly copy an environment for
- manual editing of settings later. For example, copying a test environment to the production server.
- .NOTES
- You can create the new environment on the same server or a different server.
- .PARAMETER SourceServerName
- The server where the SSIS environment you wish to copy resides.
- .PARAMETER SourceCatalogFolder
- The SSIS Catalog folder where the the source SSIS environment exists.
- .PARAMETER TargetCatalogFolder
- The SSIS Catalog folder where the new environment will be created.
- .PARAMETER EnvironmentToCopy
- The name of the environment you wish to copy.
- .PARAMETER NewEnvironmentName
- The name of the new environment.
- .PARAMETER TargetServerName
- The server to copy the SSIS environment to.
- .PARAMETER NewEnvironmentDescription
- Can provide a description for the new environment if desired.
- .EXAMPLE
- PS> Copy-SSISEnvironment -SourceServerName "TESTSERVER" -SourceCatalogFolder "Sales" -TargetCatalogFolder "Sales" `
- -EnvironmentToCopy "Sales - Test" -NewEnvironmentName "Sales - Live" -TargetServerName "PRODSERVER"
- Creates the environment 'Sales - Live' on the server 'PRODSERVER' using the 'Sales - Test' evironment
- on server 'TESTSERVER' as the base. Creates the Sales Catalog folder on the target server if not exists.
- #>[CmdletBinding()]
- Param (
- [Parameter(mandatory=$true)]
- [string]$SourceServerName,
- [Parameter(mandatory=$true)]
- [string]$SourceCatalogFolder,
- [Parameter(mandatory=$true)]
- [string]$TargetCatalogFolder,
- [Parameter(mandatory=$true)]
- [string]$EnvironmentToCopy,
- [Parameter(mandatory=$true)]
- [string]$NewEnvironmentName,
- [Parameter(mandatory=$true)]
- [string]$TargetServerName,
- [Parameter(mandatory=$false)]
- [string]$NewEnvironmentDescription = ""
- )
- #Load SSIS Assembly
- $ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"
- [System.Reflection.Assembly]::LoadWithPartialName($ISNamespace) | Out-Null;
- # Connect to source
- $sourceSQLConn = New-Object System.Data.SqlClient.SqlConnection
- $sourceSQLConn.ConnectionString = "Data Source=$SourceServerName;Initial Catalog=Master;Integrated Security=SSPI;"
- $sourceSQLConn.Open()
- # Connect to target
- $targetSQLConn = New-Object System.Data.SqlClient.SqlConnection
- $targetSQLConn.ConnectionString = "Data Source=$TargetServerName;Initial Catalog=Master;Integrated Security=SSPI;"
- $targetSQLConn.Open()
- # Create SSIS object
- $ISSource = New-Object "$ISNamespace.IntegrationServices"$sourceSQLConn
- $ISTarget = New-Object "$ISNamespace.IntegrationServices"$targetSQLConn
- $sourceCatalog = $ISSource.Catalogs["SSISDB"]
- $targetCatalog = $ISTarget.Catalogs["SSISDB"]
- # Get the folder that we are working with
- $sourceFolder = $sourceCatalog.Folders[$SourceCatalogFolder]
- $targetFolder = $targetCatalog.Folders[$TargetCatalogFolder]
- if ($targetFolder.length -eq 0) {
- $targetFolder = New-Object $ISNamespace".CatalogFolder" ($targetCatalog, $TargetCatalogFolder, "")
- $targetFolder.Create()
- }
- # Get the environment
- $sourceEnv = $sourceFolder.Environments[$EnvironmentToCopy]
- $targetEnv = New-Object "$ISNamespace.EnvironmentInfo" ($targetFolder, $NewEnvironmentName, $NewEnvironmentDescription)
- # Copy each variable to the new environment
- foreach ($var in $sourceEnv.Variables)
- {
- if ($var.Value.ToString() -eq"")
- { $finalValue= ""}
- else{$finalValue =$var.Value}
- $targetEnv.Variables.Add($var.Name, $var.Type, $finalValue,$var.Sensitive, $var.Description)
- }
- $targetEnv.Create()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement