Last active
January 2, 2017 15:23
-
-
Save samskolli/974eaaf9d1bd72fb3ee8bb202e49a058 to your computer and use it in GitHub Desktop.
A c# script to iterate over all the variables in a given SSIS ispac file / package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.SqlServer.Dts.Runtime; | |
namespace ObjectModelTests | |
{ | |
/// <summary> | |
/// Helps in iterating over all the variables in a given SSIS ispac file / package | |
/// Accepts a ispac file path or dtsx file path. | |
/// Modify/Add-a-new ctor if the storage is not in a file system | |
/// The scripts simply prints some variable info to the console. | |
/// Change that part of code as per your needs; including storing the variable information in your own custom collection | |
/// </summary> | |
public class VariableIterator | |
{ | |
private string _ispacFile; | |
private Project _ssisProject; | |
#region ctor | |
/// <summary> | |
/// ctor accepts either a ispac file path or a dts file path | |
/// </summary> | |
/// <param name="ispacFilePath"></param> | |
/// <param name="packagePath"></param> | |
public VariableIterator(string ispacFilePath, string packagePath) | |
{ | |
if (!(String.IsNullOrEmpty(ispacFilePath))) | |
{ | |
_ispacFile = ispacFilePath; | |
_ssisProject = Project.OpenProject(_ispacFile); | |
GetAllVariablesInProject(); | |
} | |
else | |
{ | |
if (!(String.IsNullOrEmpty(packagePath))) | |
{ | |
Application app = new Application(); | |
Package pkg = app.LoadPackage(packagePath, null); | |
GetAllVariablesInPackage(pkg); | |
} | |
else | |
{ | |
Console.WriteLine("please provide a ispac file path or a dtsx file path"); | |
} | |
} | |
} | |
#endregion | |
#region Variables in a Project | |
/// <summary> | |
/// Iterate thru all the variables in a project | |
/// </summary> | |
public void GetAllVariablesInProject() | |
{ | |
for(int p = 0; p < _ssisProject.PackageItems.Count; p++) | |
{ | |
Package pkg = _ssisProject.PackageItems[p].Package; | |
GetAllVariablesInPackage(pkg); | |
} | |
} | |
#endregion | |
#region Variables in a Package | |
/// <summary> | |
/// Iterate thru all variables in a dtsx Package | |
/// </summary> | |
/// <param name="pkg"></param> | |
public void GetAllVariablesInPackage(Package pkg) | |
{ | |
Console.WriteLine("Package: " + pkg.Name); | |
List<DtsContainer> containerCollection = new List<DtsContainer>(); | |
GetChildContainers((pkg as DtsContainer), containerCollection); | |
foreach (DtsContainer container in containerCollection) | |
{ | |
DoSomethingWithVariables(container, pkg); | |
} | |
} | |
#endregion | |
#region Containers in a Package | |
/// <summary> | |
/// iterates thru all containers recursively and get's all the control flow tasks. | |
/// </summary> | |
/// <param name="container"></param> | |
/// <param name="containerCollection"></param> | |
private void GetChildContainers(DtsContainer container, List<DtsContainer> containerCollection) | |
{ | |
containerCollection.Add(container); | |
if (container is IDTSSequence) | |
{ | |
foreach (Executable e in (container as IDTSSequence).Executables) | |
{ | |
if (e is IDTSSequence) | |
{ | |
GetChildContainers((e as DtsContainer), containerCollection); | |
} | |
else | |
{ | |
containerCollection.Add(e as DtsContainer); | |
} | |
} | |
} | |
} | |
#endregion | |
#region Variables in a Container | |
/// <summary> | |
/// Iterates thru all variables in a container | |
/// </summary> | |
/// <param name="container"></param> | |
/// <param name="parentPackage"></param> | |
private void DoSomethingWithVariables(DtsContainer container, Package parentPackage) | |
{ | |
foreach(Variable variable in container.Variables) | |
{ | |
string name = variable.Name; | |
bool readOnly = variable.ReadOnly; | |
string nameSpace = variable.Namespace; | |
string dataType = variable.DataType.ToString(); | |
object value; | |
if (dataType == "Empty") // for variables that are not of primitive types such as string, date, int etc. | |
{ | |
value = "<This Variable is not of Primitive Type; but of Object Type>"; // specifying a informational message for end user | |
}else | |
{ | |
if (variable.Namespace == "$Package") // for those that are actually Parameters | |
{ | |
// if sensitive use GetSensitive; | |
value = parentPackage.Parameters[variable.Name].Sensitive ? variable.GetSensitiveValue().ToString() : variable.Value.ToString(); | |
} | |
else | |
{ | |
value = variable.Value; | |
} | |
} | |
string expression = variable.Expression; | |
string creationName = variable.CreationName; | |
string description = variable.Description; | |
bool evaluateAsExpression = variable.EvaluateAsExpression; | |
DtsContainer variableParent = variable.Parent; | |
string immediateScope = variableParent.Name; | |
string qualifiedName = variable.QualifiedName; | |
bool raiseChangedEvent = variable.RaiseChangedEvent; | |
bool systemVariable = variable.SystemVariable; | |
// write the above info to console (or write to a database or external file, or store in a custom collection etc. as needed) . | |
Console.WriteLine("Name: " + name + ": Scope: " + immediateScope + "; DataType: " + dataType + "; Expression:" + expression + "; Value: " + value.ToString()); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment