$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.
1 Answer
$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)
2 Comments
xhr489
You use the word "instance" above. Can you please explain when something is an instance?
mklement0
@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).
Select-Object -Property *seems redundant). You're already filtering the list of services withWhere-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).$p.property.Get-Serviceis only an example of how the output looks when I have$P.propertyloaded from a file.$P.propertyalready is an array, apply aWhere-Objectfilter to it and capture the result in a(n invariably new) array.