Xml 列出SqlStatementSource的“全部”;执行SQL任务“;在SSIS包中

Xml 列出SqlStatementSource的“全部”;执行SQL任务“;在SSIS包中,xml,powershell,ssis,Xml,Powershell,Ssis,我想列出SSIS包中所有“执行SQL任务”的SqlStatementSource。通过参考相关内容,可以看出SQLPSX的ssis包可能有助于解决此任务。 但是,当我尝试执行以下步骤时: import-module SSIS $package = Get-ISPackage -path "xxx.dtsx" My powershell返回以下错误消息: “无法删除包保护,错误为0xC0014037”包已使用密码加密。未指定密码或密码不正确。“。这发生在CPackage::LoadFromXML

我想列出SSIS包中所有“执行SQL任务”的SqlStatementSource。通过参考相关内容,可以看出SQLPSX的ssis包可能有助于解决此任务。 但是,当我尝试执行以下步骤时:

import-module SSIS
$package = Get-ISPackage -path "xxx.dtsx"
My powershell返回以下错误消息:

“无法删除包保护,错误为0xC0014037”包已使用密码加密。未指定密码或密码不正确。“。这发生在CPackage::LoadFromXML方法中。”

它显示我应该导入密码来解密包以检索数据,但我应该将密码放在哪里?或者有没有其他方便我解决这个问题的方法

最好的,
David

您通常使用DTEXEC运行加密包,如下所示:

DTExec.exe /FILE "C:\Package1.dtsx" /DECRYPT password@1.
/文件表示包位于文件系统上。您可以使用/SQL在SQLServer数据库上创建一个包,如果它在文件存储中,则使用/DT创建一个包


此外,如果您在投标时打开软件包,系统会提示您输入密码,这不是最好的解决方案,而是值得尝试的。与其尝试通过SSI获取信息,不如通过文件本身。DTSX文件是XML格式,PowerShell可以很好地处理这些类型的文件

我在我的一个dtsx文件上尝试了此操作,并能够返回信息:


[xml]$package = Get-Content C:\Myfile.dtsx
$package.Executable.Executable | 
   Select -ExpandProperty ObjectData | 
      Select -ExpandProperty SqlTaskData | 
         Select SqlStatementSource
由于某些原因,我确实收到一个InvalidArgument错误,表示它找不到属性“SqlTaskData”。我相信这是因为它击中了包中的数据流任务,而它没有属性/属性。这就是我的意思,因为它可能不是完美的解决方案,所以我不提供任何保证。需要指出的一点是,我没有将我的包设置为使用密码加密

更新


您可以尝试SSI。

我没有安装SQLPSX,但我可以告诉您如何在没有它的情况下解密包。要做的重要事情是将包密码分配给应用程序,以便应用程序能够解密包

给定这样一个包,其中每个执行sql任务都有一条语句
selectn astest

下面的脚本将解密另存为EncryptAllWithPassword的包,并包含各种任务,其中一些任务嵌入到各种容器中。虽然它并不漂亮,但它完成了任务

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.ManagedDTS") | out-null

Function ProcessExecutable
{
    param
    (
    [Microsoft.SqlServer.Dts.Runtime.Executable]$item
    )

    $t = $item.GetType()
    if ($t.Name -eq "TaskHost")
    {
        #$th = New-Object Microsoft.SqlServer.Dts.Runtime.Task
        #$es = New-Object Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask
        $th = [Microsoft.SqlServer.Dts.Runtime.TaskHost]$item
        try
        {
            $es = [Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask]$th.InnerObject
            Write-Host($es.SqlStatementSource)
        }
        catch
        {
        }
    }
    elseif ($t.Name -eq "Sequence")
    {
        $sequence = [Microsoft.SqlServer.Dts.Runtime.Sequence]$item
        foreach ($subitem in $sequence.Executables)
        {
            ProcessExecutable $subitem
        }
    }
    elseif ($t.Name -eq "ForLoop")
    {
        $sequence = [Microsoft.SqlServer.Dts.Runtime.ForLoop]$item
        foreach ($subitem in $sequence.Executables)
        {
            ProcessExecutable $subitem
        }
    }
    elseif ($t.Name -eq "ForEachLoop")
    {
        $sequence = [Microsoft.SqlServer.Dts.Runtime.ForEachLoop]$item
        foreach ($subitem in $sequence.Executables)
        {
            ProcessExecutable $subitem
        }
    }    
}


$app = New-Object Microsoft.SqlServer.Dts.Runtime.Application
$app.PackagePassword = "password"
$packagePath = "C:\sandbox\SSISHackAndSlash\SSISHackAndSlash\Encrypted.dtsx"
$package = $app.LoadPackage($packagePath, $null)

foreach($item in $package.Executables)
{
    ProcessExecutable $item
}
输出


在搜索了有关如何列出所有SSI组件的在线信息后,我发现编写C#程序可能是解决此问题的最佳解决方案。因此,下面我编写了一个VS2008兼容程序来完成我的任务。 下面编写的程序将列出所有SSIS相关组件,并将结果写入excel文件。该解决方案将为您节省大量时间单击VS 2008中显示的组件以逐个查看组件属性。由于我不是C#专家,程序可能编码不好。但至少它起作用了

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;


namespace Project1
{
    public class SSISFinder
    {

        public static void Main()
        {
            // Set ssis app
            Microsoft.SqlServer.Dts.Runtime.Application ssisApp = new Microsoft.SqlServer.Dts.Runtime.Application();
            ssisApp.PackagePassword = "admin_monkey4ccc";

            // Loading dtsx package
            Package pkg = ssisApp.LoadPackage("D:\\SummaryETL.dtsx", null);

            // Open Excel Sheet
            Excel.Application oXL = new Excel.Application();
            Excel.Workbook oWB;
            Excel.Worksheet oSheet;            
            string fileName = "D:\\test.xls";
            oWB = oXL.Workbooks.Add(Missing.Value);
            oSheet = (Excel.Worksheet)oWB.ActiveSheet;

            // List data flow package
            List<DtsContainer> containers = FindExecutablesByType((IDTSSequence)pkg, "PIPELINE");
            int counter = 1;

            foreach (DtsContainer exec in containers)
            {
                TaskHost th = exec as TaskHost;
                MainPipe pipe = (MainPipe)th.InnerObject;

                foreach (IDTSComponentMetaData100 comp in pipe.ComponentMetaDataCollection)
              {
                  if (comp.Description == "OLE DB Source")
                  {
                      oSheet.Cells[counter, 1] = comp.Description;
                      oSheet.Cells[counter, 2] = th.Properties["Name"].GetValue(th).ToString();
                      oSheet.Cells[counter, 3] = comp.Name;                      
                      oSheet.Cells[counter, 4] = comp.CustomPropertyCollection["SqlCommand"].Value;
                      Console.WriteLine("  Component Name = " + comp.Name);
                      counter++;
                  }
                  else if (comp.Description == "OLE DB Destination")
                  {
                      oSheet.Cells[counter, 1] = comp.Description;
                      oSheet.Cells[counter, 2] = th.Properties["Name"].GetValue(th).ToString();
                      oSheet.Cells[counter, 3] = comp.Name;                      
                      oSheet.Cells[counter, 4] = comp.CustomPropertyCollection["OpenRowset"].Value;
                      Console.WriteLine("  Component Name = " + comp.Name);
                      counter++;
                  }
              }

            }

            oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Excel.XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            oWB = null;

            oXL.Quit();
            oXL = null;

        }

        static List<DtsContainer> FindExecutablesByType(IDTSSequence sequence, string typeName)
        {
            string typeNameUpper = typeName.ToUpper();
            List<DtsContainer> matchingExecutable = new List<DtsContainer>();
            foreach (Executable e in sequence.Executables)
            {
                if (e.GetType().ToString().ToUpper().Contains(typeNameUpper))
                {


                    matchingExecutable.Add((DtsContainer)e);
                }
                if (e is TaskHost)
                {
                    TaskHost taskHost = (TaskHost)e;

                    if ((typeNameUpper.Contains("DATA FLOW")
                                || typeNameUpper.Contains("DATAFLOW")
                                || typeNameUpper.Contains("MAINPIPE")
                                || typeNameUpper.Contains("PIPELINE")
                                ) && taskHost.InnerObject is IDTSPipeline100
                       )
                    {
                        matchingExecutable.Add((DtsContainer)e);
                    }
                    else if (taskHost.InnerObject.GetType().ToString().ToUpper().Contains(typeNameUpper))
                    {
                        matchingExecutable.Add((DtsContainer)e);
                    }
                }
                if (e is IDTSSequence)
                {
                    matchingExecutable.AddRange(FindExecutablesByType((IDTSSequence)e, typeNameUpper));
                }
            }
            return matchingExecutable;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.IO;
使用Excel=Microsoft.Office.Interop.Excel;
运用系统反思;
使用Microsoft.SqlServer.Dts.Runtime;
使用Microsoft.SqlServer.Dts.Pipeline;
使用Microsoft.SqlServer.Dts.Pipeline.Wrapper;
命名空间项目1
{
公共类查找器
{
公共静态void Main()
{
//设置ssis应用程序
Microsoft.SqlServer.Dts.Runtime.Application SSIApp=新的Microsoft.SqlServer.Dts.Runtime.Application();
ssisApp.PackagePassword=“admin\u monkey4ccc”;
//加载dtsx包
Package pkg=ssisApp.LoadPackage(“D:\\SummaryETL.dtsx”,null);
//打开Excel工作表
Excel.Application oXL=新的Excel.Application();
Excel.oWB工作手册;
Excel.oSheet工作表;
字符串fileName=“D:\\test.xls”;
oWB=oXL.Workbooks.Add(缺少.Value);
oSheet=(Excel.Worksheet)oWB.ActiveSheet;
//列出数据流包
列表容器=FindExecutablesByType((IDTSSequence)pkg,“管道”);
int计数器=1;
foreach(容器中的DtsContainer exec)
{
TaskHost th=作为TaskHost的exec;
MainPipe=(MainPipe)th.InnerObject;
foreach(IDTSComponentMetaData100管道中的组件。组件MetadataCollection)
{
if(组件描述==“OLE DB源”)
{
oSheet.Cells[计数器,1]=组件说明;
oSheet.Cells[counter,2]=th.Properties[“Name”].GetValue(th.ToString();
oSheet.Cells[计数器,3]=公司名称;
oSheet.Cells[counter,4]=组件CustomPropertyCollection[“SqlCommand”].Value;
Console.WriteLine(“组件名称=“+comp.Name”);
计数器++;
}
else if(comp.Description==“OLE DB目标”)
{
oSheet.Cells[计数器,1]=组件说明;
oSheet.Cells[counter,2]=th.Properties[“Name”].GetValue(th.ToString();
oSheet.Cells[计数器,3]=公司名称;
oSheet.Cells[counter,4]=组件CustomPropertyCollection[“OpenRowset”]值;
Console.WriteLine(“组件名称=“+comp.Name”);
计数器++;
}
}
}
oWB.SaveAs(文件名,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Excel.XlSaveAsAccessMode.xlShared,类型.缺失,类型.缺失,类型.缺失,类型.缺失,类型.缺失,类型.缺失);
oWB=null;
oXL.Quit();
oXL=null;
}
静态列表FindExecutablesByType(IDTSSequence序列,字符串类型名)
{
斯特林
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;


namespace Project1
{
    public class SSISFinder
    {

        public static void Main()
        {
            // Set ssis app
            Microsoft.SqlServer.Dts.Runtime.Application ssisApp = new Microsoft.SqlServer.Dts.Runtime.Application();
            ssisApp.PackagePassword = "admin_monkey4ccc";

            // Loading dtsx package
            Package pkg = ssisApp.LoadPackage("D:\\SummaryETL.dtsx", null);

            // Open Excel Sheet
            Excel.Application oXL = new Excel.Application();
            Excel.Workbook oWB;
            Excel.Worksheet oSheet;            
            string fileName = "D:\\test.xls";
            oWB = oXL.Workbooks.Add(Missing.Value);
            oSheet = (Excel.Worksheet)oWB.ActiveSheet;

            // List data flow package
            List<DtsContainer> containers = FindExecutablesByType((IDTSSequence)pkg, "PIPELINE");
            int counter = 1;

            foreach (DtsContainer exec in containers)
            {
                TaskHost th = exec as TaskHost;
                MainPipe pipe = (MainPipe)th.InnerObject;

                foreach (IDTSComponentMetaData100 comp in pipe.ComponentMetaDataCollection)
              {
                  if (comp.Description == "OLE DB Source")
                  {
                      oSheet.Cells[counter, 1] = comp.Description;
                      oSheet.Cells[counter, 2] = th.Properties["Name"].GetValue(th).ToString();
                      oSheet.Cells[counter, 3] = comp.Name;                      
                      oSheet.Cells[counter, 4] = comp.CustomPropertyCollection["SqlCommand"].Value;
                      Console.WriteLine("  Component Name = " + comp.Name);
                      counter++;
                  }
                  else if (comp.Description == "OLE DB Destination")
                  {
                      oSheet.Cells[counter, 1] = comp.Description;
                      oSheet.Cells[counter, 2] = th.Properties["Name"].GetValue(th).ToString();
                      oSheet.Cells[counter, 3] = comp.Name;                      
                      oSheet.Cells[counter, 4] = comp.CustomPropertyCollection["OpenRowset"].Value;
                      Console.WriteLine("  Component Name = " + comp.Name);
                      counter++;
                  }
              }

            }

            oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Excel.XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            oWB = null;

            oXL.Quit();
            oXL = null;

        }

        static List<DtsContainer> FindExecutablesByType(IDTSSequence sequence, string typeName)
        {
            string typeNameUpper = typeName.ToUpper();
            List<DtsContainer> matchingExecutable = new List<DtsContainer>();
            foreach (Executable e in sequence.Executables)
            {
                if (e.GetType().ToString().ToUpper().Contains(typeNameUpper))
                {


                    matchingExecutable.Add((DtsContainer)e);
                }
                if (e is TaskHost)
                {
                    TaskHost taskHost = (TaskHost)e;

                    if ((typeNameUpper.Contains("DATA FLOW")
                                || typeNameUpper.Contains("DATAFLOW")
                                || typeNameUpper.Contains("MAINPIPE")
                                || typeNameUpper.Contains("PIPELINE")
                                ) && taskHost.InnerObject is IDTSPipeline100
                       )
                    {
                        matchingExecutable.Add((DtsContainer)e);
                    }
                    else if (taskHost.InnerObject.GetType().ToString().ToUpper().Contains(typeNameUpper))
                    {
                        matchingExecutable.Add((DtsContainer)e);
                    }
                }
                if (e is IDTSSequence)
                {
                    matchingExecutable.AddRange(FindExecutablesByType((IDTSSequence)e, typeNameUpper));
                }
            }
            return matchingExecutable;
        }
    }
}