ssis 执行任务的日志

介绍 (Introduction)

SQL Server Integration Services (SSIS) and PowerShell (PS) together offer a plethora of opportunities, and some shortcuts when having to import, export, or at times moving data. I have come across packages that contain a Script Task with lines and lines of C# code that, done with PowerShell, could make maintaining that package much easier. Overall, the most common thing I see Script Task doing is accessing the file system or doing some manipulation on a file. One thing I hope that picks up speed in the BI world of SSIS is utilizing PowerShell for these type of actions. This is not to say one is better than the other as you should pick what is best in your eyes, but when I can do operations against the file system with a one-liner in PowerShell it is just easier to maintain that in the package. In this article I will go over how you can use the most common task utilized for executing PowerShell code in an SSIS package: Execute Process Task.

SQL Server集成服务(SSIS)和PowerShell(PS)一起提供了很多机会,并且在必须导入,导出或有时移动数据时提供了一些快捷方式。 我遇到了一些包含脚本任务的程序包,这些脚本任务包含几行C#代码,使用PowerShell可以使维护该程序包更加容易。 总的来说,我看到脚本任务最常做的事情是访问文件系统或对文件进行一些操作。 我希望能够在SSIS的BI世界中加快速度的一件事是利用PowerShell执行此类操作。 这并不是说一个要比另一个好,因为您应该选择最适合自己的东西,但是当我可以在PowerShell中使用单行代码对文件系统进行操作时,将其维护在包中就更容易了。 在本文中,我将介绍如何使用最常见的任务来执行SSIS包中的PowerShell代码: Execute Process Task

设置 (The Setup)

Executing PowerShell with the Execute Process Task means you are going to call “powershell.exe” and then pass it some parameters. Now you can just go to a command prompt and type in “powershell /?” to see your options, but at a minimum you will generally use the following:

通过执行过程任务执行PowerShell意味着您将调用“ powershell.exe”,然后将一些参数传递给它。 现在,您可以转到命令提示符并键入“ powershell /?” 来查看您的选择,但是至少您通常会使用以下内容:

  • NoProfile – this ensures it does not try to load any profile on the machine and I always use it as a precautionary measure. NoProfile –确保它不会尝试在计算机上加载任何配置文件,并且我始终将其用作预防措施。
  • ExecutionPolicy – While PowerShell can be locked down when it comes to the execution policy for calling scripts and commands, it can also be “Bypass”-ed. When you are testing, your policy may allow you to execute PowerShell code. If 6 months from now some domain administrator wants to change that policy for your server, this will ensure they do not break all the packages where you use PowerShell. ExecutionPolicy –尽管在调用脚本和命令的执行策略上可以锁定PowerShell,但也可以对其进行“绕过”编辑。 在测试时,您的策略可能允许您执行PowerShell代码。 如果从现在开始的6个月内,某个域管理员想要更改服务器的策略,这将确保他们不会破坏使用PowerShell的所有软件包。
  • Command – This is either a script block (wrap the code in curly braces) or in double-quotes. 命令 –这可以是一个脚本块(用大括号括起来的代码)或双引号。
  • File – in place of the command parameter, if you want to call a file. 文件 –如果要调用文件,请代替命令参数。

The version of SSIS tools I will be using: SQL Server Data Tools Business Intelligence for Visual Studio 2013.

我将使用的SSIS工具的版本: 用于Visual Studio 2013SQL Server数据工具商业智能 。

执行流程任务 (Execute Process Task)

The execute process task is fairly simple to work with, and only a few fields need to be completed. You can get full documentation on this task with SSIS by going to the link provided in the reference section at the end of this article. You can see the main window of the task properties is the “Process” pane, which I have circled below the main fields we will work with:

执行流程任务非常简单,只需填写几个字段即可。 您可以通过转至本文末尾参考部分中提供的链接来获得有关SSIS任务的完整文档。 您可以看到任务属性的主窗口是“ Process”窗格,我在我们将要使用的主要字段下面圈了圈:

执行内联代码 (Executing Inline Code)

When I am dealing with a case where I just need to execute a built-in cmdlet, or the one-liner I need to use is fairly short, I will opt for the “-Command” parameter. Now it is possible to put 100 words into a one-liner, but there is a limit for me as to whether how easily it is to maintain that and making sure it is readable. If we work on the example of testing a file path, the cmdlet I would use is, Test-Path and this is within my limit of just using the command parameter. If there was more business logic to include I would opt for the file instead.

当我需要执行一个内置cmdlet或我需要使用的单一代码行很短时,我将选择“ -Command”参数。 现在可以将100个单词放在一个直线上,但是对我来说,保持它并确保可读性是否容易有一个限制。 如果我们以测试文件路径为例,那么我将使用的cmdlet是Test-Path ,这在我仅使用command参数的范围内。 如果要包含更多的业务逻辑,我将选择该文件。

Utilizing variables can vary between individuals and I try not to go to crazy in their use. My thinking in this situation though, I would have a variable to hold my root path, and then a variable that holds my file name that I am working on. I will then have another variable that joins them together via an expression in order to work with the full path to the file, this will make my PowerShell argument easier to read. The final variable I will need is for the arguments I pass to the “powershell.exe” process. So the list of variables and their initial value will look like this, and note when I reference directories or folders I will always include that ending back slash:

每个人的使用变量可能会有所不同,我尽量不要疯狂使用它们。 在这种情况下,我的想法是,我将拥有一个变量来保存我的根路径,然后有一个变量来保存我正在处理的文件名。 然后,我将拥有另一个变量,该变量通过表达式将它们连接在一起,以便使用文件的完整路径,这将使我的PowerShell参数更易于阅读。 我需要的最后一个变量是传递给“ powershell.exe”进程的参数。 因此,变量列表及其初始值将如下所示,并请注意,当我引用目录或文件夹时,我将始终包括以反斜杠结尾的内容:

  • FilePath (String) = C:\TEMP\CollegeScorecardRaw_Data\ FilePath(String)= C:\ TEMP \ CollegeScorecardRaw_Data \
  • FileName (String) = MERGED1996_PP.csv FileName(String)= MERGED1996_PP.csv
  • TestFilePath (String) = @[User::FilePath] + @[User::FileName] TestFilePath(字符串)= @ [User :: FilePath] + @ [User :: FileName]
  • Cmd (String) = “-NoProfile -ExecutionPolicy ByPass -Command \”if (Test-Path ‘” + @[User::TestFilePath] + “‘) {exit 0} else {exit 999}\”” Cmd(String)=“ -NoProfile -ExecutionPolicy ByPass -Command \”如果(Test-Path'“ + @ [User :: TestFilePath] +”'){退出0}否则{退出999} \””

I will explain a bit more about the cmd string later.

稍后,我将解释有关cmd字符串的更多信息。

To pass the variables into the PowerShell command we use the “Cmd” variable, and that is simply configured as an expression for the Arguments property. I prefer doing this, because if I need to update that command I simply update the variable, compared to having to open up the properties window and making the change. You could also configure this variable into a configuration file or project parameter for easier management. I configure the execute process task as shown below in the screenshot:

要将变量传递到PowerShell命令中,我们使用“ Cmd”变量,该变量被简单配置为Arguments属性的表达式。 我更喜欢这样做,因为与需要打开属性窗口并进行更改相比,如果我需要更新该命令,则只需更新变量即可。 您也可以将此变量配置到配置文件或项目参数中,以便于管理。 我配置了执行过程任务,如下面的屏幕快照所示:

You will see that the “Arguments” property looks like I manually typed it in, but going to the Expressions pane you can see that I have mapped the property to the expression:

您将看到“ Arguments”属性看起来像我手动键入的一样,但是转到“表达式”窗格,您可以看到我已将该属性映射到表达式:

One final thing I will do is simply add two script task that will have a message box defined via C# to visually display whether the test was successful or not. A successful execution where the file does exist, will provide this result:

我要做的最后一件事就是简单地添加两个脚本任务,该任务将具有一个通过C#定义的消息框,以直观地显示测试是否成功。 文件确实存在的成功执行将提供以下结果:

Where a failure for a file that does not exist will result in this view:

如果文件不存在失败,将在此视图中显示:

那cmd变量呢? (What about that cmd variable?)

If you did not know already the execute process task can be finicky at times, so always test. In normal PowerShell scripts if I need to return a true or false around testing a file path I would just execute this:

如果您还不知道执行过程任务有时可能很挑剔,那么请务必进行测试。 在正常的PowerShell脚本中,如果我需要在测试文件路径时返回true或false,我将执行以下命令:

Test-Path ‘C:\TEMP\MyFile.csv’

测试路径“ C:\ TEMP \ MyFile.csv”

This command will return a true or false, however in the execute process task it is expecting an integer value of 0 (zero) for true by default. This cmdlet would not return that by itself so you have to force it to return an integer value, so what if I change it to this:

该命令将返回true或false,但是在执行过程任务中,默认情况下期望true的整数值为0(零)。 该cmdlet不会单独返回该cmdlet,因此您必须强制其返回整数值,如果将其更改为该值,该怎么办:

If (Test-Path ‘C:\TEMP\MyFile.csv’) {0} else {999}

如果(测试路径'C:\ TEMP \ MyFile.csv'){0}否则{999}

In a PowerShell prompt you would get a zero if that path is good, or 999 if it is not. However again, it does not return that if you execute it calling PowerShell.exe. Why? The reason is that the process itself (PowerShell.exe) is returning a zero for successfully completing the command. So the potential is there for it to overwrite any failure value I need returned. What you do is simply force the exit value, if you are familiar with the batch file days you probably remember doing this all the time. So the end result of the command to get the expected result in SSIS:

在PowerShell提示中,如果该路径正确,则将得到零;否则,将得到999。 但是,再次执行调用PowerShell.exe时,它不会返回该值。 为什么? 原因是进程本身(PowerShell.exe)返回零以成功完成命令。 因此,它有可能覆盖我需要返回的任何失败值。 您要做的就是简单地强制退出值,如果您熟悉批处理文件的日子,您可能一直记得这样做。 因此,在SSIS中获得预期结果的命令的最终结果:

If (Test-Path ‘C:\TEMP\MyFile.csv’) {exit 0} else {exit 999}

如果(测试路径'C:\ TEMP \ MyFile.csv'){退出0},否则{退出999}

结语 (Wrap Up)

PowerShell has a huge potential to save you having to write any raw C# code for your packages. In the sense that it is built upon .NET framework itself, a lot of that code is accessible in much easier fashion. Just remember to have it in the back of your mind when designing your packages and you find yourself going to C#, “how would I do this in PowerShell?”

PowerShell具有巨大的潜力,可以节省您为程序包编写任何原始C#代码的麻烦。 从某种意义上说,它是建立在.NET框架本身之上的,因此许多代码都可以以更加容易的方式进行访问。 只需记住在设计软件包时就将它放在脑海中,然后您就会发现要使用C#,“我将如何在PowerShell中执行此操作?”

The solution used to write the article is available here

这里提供了用于撰写本文的解决方案

参考资料 (References)

  • Execute Process Task 执行流程任务
  • Use Test-Path to verify existence of an object. 使用测试路径来验证对象的存在。
  • One-Liners to Get You Started 一线入门

翻译自: https://www.sqlshack.com/ssis-and-powershell-execute-process-task/

ssis 执行任务的日志

ssis 执行任务的日志_SSIS和PowerShell –执行流程任务相关推荐

  1. 查看ssis执行日志_SSIS包日志记录概述

    查看ssis执行日志 This article gives an overview of the different methods of SQL Server SSIS Package Loggin ...

  2. Java-Mybatis(二): Mybatis配置解析、resultMap结果集映射、日志、分页、注解开发、Mybatis执行流程分析

    Java-Mybatis-02 学习视频:B站 狂神说Java – https://www.bilibili.com/video/BV1NE411Q7Nx 学习资料:mybatis 参考文档 – ht ...

  3. ssis配置文件优先级_SSIS优先约束概述

    ssis配置文件优先级 This article explores the SSIS Precedence Constraints, along with examples and scenarios ...

  4. pip包管理工具-install执行流程简单查看

    pip概述 pip是python提供的包管理工具,该工具提供了对python包的查找.下载.安装与卸载等功能的工具,当前是python中比较主流的管理工具. pip下载安装包的概述 pip工具的本质通 ...

  5. 一文搞懂select语句在MySQL中的执行流程!

    MySQL作为互联网行业使用最多的关系型数据库之一,与其免费.开源的特性是密不可分的.然而,很多小伙伴工作了很多年,只知道使用MySQL进行CRUD操作,这也导致很多小伙伴工作多年后,想跳槽进入大厂, ...

  6. php面试框架的执行流程图,ThinkPHP2.2框架执行流程图,ThinkPHP控制器的执行流程

    ThinkPHP2.2框架执行原理.流程图在线手册 ThinkPHP控制器的执行流程 对用户的第一次URL访问http:///My/index.php/Index/show/所执行的流程进行详细的分析 ...

  7. thinkphp执行流程

    2019独角兽企业重金招聘Python工程师标准>>> thinkphp执行流程 1. 入口文件index.php 用户对url的访问首先被定位到http:index.php, 这里 ...

  8. bs架构与cs架构的区别_Oracle vs Mysql--架构、sql查询执行流程及SQL解析顺序区别说明...

    概述 之前分享的主要是Oracle上的一些内容,那么mysql又有哪些地方不一样呢?下面从MySQL总体架构.sql查询执行流程和语句执行顺序来看一下.. 01 架构总览 下面看一下mysql的架构图 ...

  9. oracle和mysql文件怎么打开_Oracle vs Mysql--架构、sql查询执行流程及SQL解析顺序区别说明-sql文件怎么打开...

    概述 之前分享的主要是Oracle上的一些内容,那么mysql又有哪些地方不一样呢?下面从MySQL总体架构.sql查询执行流程和语句执行顺序来看一下.. 01 架构总览 下面看一下mysql的架构图 ...

最新文章

  1. iOS10 UI教程视图和子视图的可见性
  2. php鼠标经过显示文本,jQuery实现鼠标单击网页文字后在文本框显示的方法
  3. SAP Spartacus的版本机制
  4. 平台式可复用的应用集成能力,助您敏捷、高效的完成企业数字化转型
  5. 面向对象的相关面试题
  6. qmenu qt 关闭,Qt实现点击菜单项后QMenu不关闭功能
  7. c# Excel的操作
  8. Hive窗口函数进阶指南
  9. 易封装app网站打包工具软件v2.0版本
  10. token什么意思中文在C语言中,token是什么意思?
  11. vue使用prevent修饰符阻止标签的默认行为
  12. 软件保护技术 - 基础
  13. IT男潘加宇:老婆在孩子班级群里怒怼数学老师
  14. 组卷与考试系统_题库添加选择题模块
  15. 商务协同办公市场有「后浪」
  16. Java job interview:WinForm界面特效设计案例导航图分享
  17. 黑盒测试简介和常用方法
  18. STEP标准执行方法-ISO-10303-21
  19. stallman 征婚
  20. 用java写篮球弹跳_篮球怎么在家练弹跳?

热门文章

  1. js页面自适应屏幕大小_Web页面适配移动端方案研究
  2. java web典型模块大全_python+selenium基于po模式的web自动化测试框架
  3. GO实例3 Slice append打印
  4. Python基础学习笔记(十三)异常
  5. 《数据结构》C++代码 堆(优先队列)
  6. 建立索引为什么能加快查询速度 【转】
  7. 设计模式之“单例模式”
  8. 设计模式 经典书籍必备推荐
  9. windows 2008 r2 AD密码策略
  10. kubernetes之四:存储