Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. Function Copy-SSISEnvironment {
  2. <#
  3. .SYNOPSIS
  4. Copies a source SSIS environment, including all it's variables.
  5. .DESCRIPTION
  6. This function copies an SSIS environment. You can use it to quickly copy an environment for
  7. manual editing of settings later. For example, copying a test environment to the production server.
  8. .NOTES
  9. You can create the new environment on the same server or a different server.
  10. .PARAMETER SourceServerName
  11. The server where the SSIS environment you wish to copy resides.
  12. .PARAMETER SourceCatalogFolder
  13. The SSIS Catalog folder where the the source SSIS environment exists.
  14. .PARAMETER TargetCatalogFolder
  15. The SSIS Catalog folder where the new environment will be created.
  16. .PARAMETER EnvironmentToCopy
  17. The name of the environment you wish to copy.
  18. .PARAMETER NewEnvironmentName
  19. The name of the new environment.
  20. .PARAMETER TargetServerName
  21. The server to copy the SSIS environment to.
  22. .PARAMETER NewEnvironmentDescription
  23. Can provide a description for the new environment if desired.
  24. .EXAMPLE
  25. PS> Copy-SSISEnvironment -SourceServerName "TESTSERVER" -SourceCatalogFolder "Sales" -TargetCatalogFolder "Sales" `
  26. -EnvironmentToCopy "Sales - Test" -NewEnvironmentName "Sales - Live" -TargetServerName "PRODSERVER"
  27.  
  28. Creates the environment 'Sales - Live' on the server 'PRODSERVER' using the 'Sales - Test' evironment
  29. on server 'TESTSERVER' as the base. Creates the Sales Catalog folder on the target server if not exists.
  30. #>[CmdletBinding()]
  31. Param (
  32. [Parameter(mandatory=$true)]
  33. [string]$SourceServerName,
  34.  
  35. [Parameter(mandatory=$true)]
  36. [string]$SourceCatalogFolder,
  37.  
  38. [Parameter(mandatory=$true)]
  39. [string]$TargetCatalogFolder,
  40.  
  41. [Parameter(mandatory=$true)]
  42. [string]$EnvironmentToCopy,
  43.  
  44. [Parameter(mandatory=$true)]
  45. [string]$NewEnvironmentName,
  46.  
  47. [Parameter(mandatory=$true)]
  48. [string]$TargetServerName,
  49.  
  50. [Parameter(mandatory=$false)]
  51. [string]$NewEnvironmentDescription = ""
  52. )
  53.  
  54. #Load SSIS Assembly
  55. $ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"
  56. [System.Reflection.Assembly]::LoadWithPartialName($ISNamespace) | Out-Null;
  57.  
  58. # Connect to source
  59. $sourceSQLConn = New-Object System.Data.SqlClient.SqlConnection
  60. $sourceSQLConn.ConnectionString = "Data Source=$SourceServerName;Initial Catalog=Master;Integrated Security=SSPI;"
  61. $sourceSQLConn.Open()
  62.  
  63. # Connect to target
  64. $targetSQLConn = New-Object System.Data.SqlClient.SqlConnection
  65. $targetSQLConn.ConnectionString = "Data Source=$TargetServerName;Initial Catalog=Master;Integrated Security=SSPI;"
  66. $targetSQLConn.Open()
  67.  
  68. # Create SSIS object
  69. $ISSource = New-Object "$ISNamespace.IntegrationServices"$sourceSQLConn
  70. $ISTarget = New-Object "$ISNamespace.IntegrationServices"$targetSQLConn
  71.  
  72. $sourceCatalog = $ISSource.Catalogs["SSISDB"]
  73. $targetCatalog = $ISTarget.Catalogs["SSISDB"]
  74.  
  75. # Get the folder that we are working with
  76. $sourceFolder = $sourceCatalog.Folders[$SourceCatalogFolder]
  77. $targetFolder = $targetCatalog.Folders[$TargetCatalogFolder]
  78.  
  79. if ($targetFolder.length -eq 0) {
  80. $targetFolder = New-Object $ISNamespace".CatalogFolder" ($targetCatalog, $TargetCatalogFolder, "")
  81. $targetFolder.Create()
  82. }
  83.  
  84. # Get the environment
  85. $sourceEnv = $sourceFolder.Environments[$EnvironmentToCopy]
  86. $targetEnv = New-Object "$ISNamespace.EnvironmentInfo" ($targetFolder, $NewEnvironmentName, $NewEnvironmentDescription)
  87.  
  88. # Copy each variable to the new environment
  89. foreach ($var in $sourceEnv.Variables)
  90. {
  91. if ($var.Value.ToString() -eq"")
  92. { $finalValue= ""}
  93. else{$finalValue =$var.Value}
  94. $targetEnv.Variables.Add($var.Name, $var.Type, $finalValue,$var.Sensitive, $var.Description)
  95. }
  96. $targetEnv.Create()
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement