Home > Blockchain >  How to Delete one object in an array of objects in PowerShell
How to Delete one object in an array of objects in PowerShell

Time:03-14

$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. 

CodePudding user response:

$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)
  • Related