使用PowerShell删除SSIS变量

时间:2018-03-12 13:28:08

标签: sql-server powershell ssis devops

我正在使用SQL Server 2016为Microsoft BI项目实施devops管道,主要使用VSTS和PowerShell。对于SSIS的发布流程,我使用integration services assembly所述的here

我遇到问题让Remove method from the assembly正常工作。我希望开发人员能够在SSDT中的XML文件中定义变量,并且发布PowerShell脚本以删除变量(如果它存在(并且XML中的值不同)。然后将使用新值重新创建(我可以看到单个变量上没有alter方法)。

这是一个代码示例,它简化了我想要做的事情:

$FolderName = "VariablesTest1"
$EnvironmentName = "Development"

# Load the IntegrationServices Assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null;

# Store the IntegrationServices Assembly namespace
$ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"

# Create a connection to the server
$sqlConnectionString = "Data Source=localhost;Initial Catalog=master;Integrated Security=SSPI;"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString

# Create the Integration Services object
$integrationServices = New-Object $ISNamespace".IntegrationServices" $sqlConnection

$catalog = $integrationServices.Catalogs["SSISDB"]
$folder = $integrationServices.Catalogs["SSISDB"].Folders[$FolderName]

# Creates the folder
$folder = New-Object $ISNamespace".CatalogFolder" ($catalog, $FolderName, "Folder description")
      $folder.Create()

$environment = $folder.Environments[$EnvironmentName]

# Creates the Environment
$environment = New-Object $ISNamespace".EnvironmentInfo" ($folder, $EnvironmentName, "Description")
    $environment.Create()

# Add the variable to the environment
$environment.Variables.Add("Variable2", "String", "MyValue", $false, "This is my variable")
$environment.Alter()

# Print the variable to make sure it exists
Write-Host $environment.Variables.Name

# Delete the variable
$environment.Variables.Remove("Variable2")

这将在Integration Services目录中创建所需的对象,并应创建,然后删除变量,但不会。变量已创建但未删除。

我是在错误的地方调用remove方法还是其他的?

1 个答案:

答案 0 :(得分:0)

在您删除后,您似乎错过了额外的$ environment.Alter()!

# Delete the variable
$environment.Variables.Remove("Variable2")
$environment.Alter()