0
$ErrorActionPreference = "Stop";

# Not sure if I need to load all these assemblies
 Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.ManagedDTS.dll';
 Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.DTSPipelineWrap\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.DTSPipelineWrap.dll';
 Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.PipelineHost\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.PipelineHost.dll';
 # Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.DTSPipelineWrap\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.DTSPipelineWrap.dll';

$App = New-Object -TypeName Microsoft.SqlServer.Dts.Runtime.Application; 

$PackageFullPath = 'C:\SSISPackage.dtsx'; # This is the SSIS package and has some connection managers. 

$Package = $App.LoadPackage($PackageFullPath, $null, 0);

$Package.Connections # this gives an array of objects with connection managers and I would like to remove one of them. 

$Package.Connections | Get-member # Object: Microsoft.SqlServer.Dts.Runtime.ConnectionManager

$App.SaveToXml($PackageFullPath, $Package, $null) # Here I would like to save the package again and when I open the solution i Visual Studio I would like it to be gone. 
7
  • 1
    I don't understand how both code samples relate to each other. Can you clarify? Also, a short example output of the pipeline that you want to filter, would be helpful. Commented Mar 12, 2022 at 21:21
  • 1
    As for the PowerShell command: If you capture its output in a variable, you'll get an array of objects (the Select-Object -Property * seems redundant). You're already filtering the list of services with Where-Object, so you can add another filter criterion to exclude the service you want to delete - rather than trying to delete it from the array after the fact (arrays don't support deletion). Commented Mar 12, 2022 at 21:22
  • @zett42: The code sample that can run is just an example of the shape of the result when I have $p.property. Commented Mar 12, 2022 at 21:30
  • @mklement0: The code with Get-Service is only an example of how the output looks when I have $P.property loaded from a file. Commented Mar 12, 2022 at 21:32
  • 1
    Understood, @xhr489, but my point was that you technically cannot delete an element from an array, because it is a fixed-size data structure. Your only option is to create a new array with the element of interest omitted. If $P.property already is an array, apply a Where-Object filter to it and capture the result in a(n invariably new) array. Commented Mar 12, 2022 at 22:03

1 Answer 1

1

$Package.Connections contains a Microsoft.SqlServer.Dts.Runtime.Connections instance, which has a .Remove() method.

Therefore - assuming you know the index, name, or ID of the connection to remove - do the following:

# Determine the the connection to remove, which can be specified
# as:
# "name, index, ID, or identity of the ConnectionManager object to remove."
$toRemove = 42 # sample index

$Package.Connections.Remove($toRemove)
Sign up to request clarification or add additional context in comments.

2 Comments

You use the word "instance" above. Can you please explain when something is an instance?
@xhr489, thanks for pointing out the typo - fixed now. Instance is another way of saying object, and is typically used to refer to an object as a concrete embodiment of its type (class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.