2

我想列出 SSIS 包中所有“执行 SQL 任务”的 SqlStatementSource。通过引用相关,它表明 SQLPSX 的 ssis 包可能有助于解决此任务。但是,当我尝试执行以下过程时:

import-module SSIS
$package = Get-ISPackage -path "xxx.dtsx"

我的 powershell 返回以下错误消息:

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

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

最好的,大卫

4

4 回答 4

1

并不是说这是最好的解决方案,而是可以尝试的东西。而不是试图通过 SSIS 来获取信息,为什么不通过文件本身。DTSX 文件是 XML 格式,PowerShell 可以很好地处理这些类型的文件。

我对我的一个 dtsx 文件进行了尝试,并能够返回信息:


[xml]$package = Get-Content C:\Myfile.dtsx
$package.Executable.Executable | 
   Select -ExpandProperty ObjectData | 
      Select -ExpandProperty SqlTaskData | 
         Select SqlStatementSource

出于某种原因,我确实收到了一个 InvalidArgument 错误,说它找不到属性“SqlTask​​Data”。我相信这是因为它击中了我在包中的数据流任务,并且它没有属性/属性。这就是我的意思,因为它可能不是完美的解决方案,所以我不提供任何保证。需要指出的一件事是我没有将我的包设置为通过密码加密。

更新

您可以尝试包含SSIS 库的 SQLPSX。

于 2012-04-18T15:26:52.017 回答
1

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

给定一个像这样的包,其中每个执行 sql 任务都有一个语句SELECT N AS test 随时随地执行sql

以下脚本将解密保存为 EncryptAllWithPassword 的包,并具有各种任务,其中一些嵌入在各种容器中。无论如何,它都不是漂亮的 PowerShell,但它可以完成工作。

[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
}

输出

SELECT 1 AS test
SELECT 2 As test
SELECT 5 AS test
SELECT 4 AS test
SELECT 3 AS test
于 2012-04-18T18:28:08.233 回答
0

你通常使用 DTEXEC 来运行这样的加密包:

DTExec.exe /FILE "C:\Package1.dtsx" /DECRYPT password@1.

/FILE 表示包在文件系统上。您可以将 /SQL 用于 SQl Server 数据库上的包或 /DT(如果它在文件存储上)

此外,如果您在 BIDS 上打开包裹,系统会提示您输入密码

于 2012-04-18T10:07:31.137 回答
0

在网上搜索了有关如何列出所有 SSIS 组件的信息后,我发现编写 C# 程序可能是解决该问题的最佳解决方案。因此,下面我写了一个 VS 2008 兼容的程序来完成我的任务。下面编写的程序将列出所有与 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;
        }
    }
}
于 2012-05-21T03:36:49.623 回答