Powershell Script Task for SSIS

SSIS and Powershell are two of my current loves in technology, so of course, I was excited to see someone has made a Powershell Script Task. I’ve been meaning to try this out for months. Unfortunately it looks like the project isn’t currently active, but I thought I’d still give it a whirl, and post it here hoping to save others a little time on setup.

First you’ll need to download the source code as there’s no installation package. Then you’ll need to compile the project to produce the dll. I used Visual Studio 2008 but I guess Visual C# Express Edition will do it.

Double click the file called Defiant.SqlServer.PowerShellScriptTask.csproj from the source code you downloaded. This will open the project in your IDE. Build the project and a bin\debug  directory containing a dll will be created in the project folder.

bin debug folder

We are only interested in the file called Defiant.SqServer.PowerShellScriptTask.dll. We will need to register this file in the GAC and copy it to a place where SSIS can find it. Save the commands below, with appropriate modifications, to a batch file.

cd\
c:
cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
gacutil /uf "Defiant.SqlServer.PowerShellScriptTask";
gacutil /if "C:\Users\Rhys\Desktop\Source\Defaint.SqlServer.PowerShellScriptTask\bin\Debug\Defiant.SqlServer.PowerShellScriptTask.dll";
copy "C:\Users\Rhys\Desktop\Source\Defaint.SqlServer.PowerShellScriptTask\bin\Debug\Defiant.SqlServer.PowerShellScriptTask.dll"; "C:\Program Files\Microsoft SQL Server\100\DTS\Tasks";

You may need to modify; the path on line 3, this is the location of gacutil. line5, this is the full path of Defiant.SqlServer.PowerShellScriptTask.dll. line 6 the full path of Defiant.SqlServer.PowerShellScriptTask.dll and the path to DTS\Tasks for the instance on SQL Server you want to copy it to. For some reason I couldn’t get this to work on 2005 but it worked fine for 2008. Restart the SSIS service using services.msc then we are ready to fire up BIDS.

When inside BIDS the Powershell Script Task will not appear in the toolbox automatically. Right click on the toolbox and select Choose Items. In the SSIS Control Flow Items tab scroll down to Powershell Script Task and check the box next to it.

choose toolbox items

Now the task will appear in the toolbox. Drop the task onto the designer to begin working with it.

Powershell Script Task

Right click the task and choose edit. Anyone who has worked with the VB.Net \ C# Script Task should work this out straight away.

Powershell Script Task Editor

Let’s try something simple first. Enter the below line of Powershell in the Script window;

?View Code POWERSHELL
Write-Host "This is a test!";

Click OK and then run the package. Below is the output I received.

SSIS package "Package.dtsx" starting.
Information: 0x0 at PowerShell Script Task, PowerShellScriptTask: This is a test!
Information: 0x0 at PowerShell Script Task, PowerShellScriptTask:
SSIS package "Package.dtsx" finished: Success.

Great, it works! Now lets try working with some variables. Add two variables like below (substituting your name for mine);

ssis variable powershell

Then edit the Powershell Script Task and add these variables as below.

ssis powershell script task variables

Add the below Powershell to the Script window;

?View Code POWERSHELL
$ssis.Variables["User::write"] = $ssis.Variables["User::readOnly"];

This code just assigns the value of the readOnly variable to write. Next drop a VB.net Script task onto the designer and connect the Powershell Script Task to it. We’re going to add code here to display a message box with the contents of the write variable. This will show that our Powershell script task has successfully read from, and written to, our SSIS variables.

script task edit

Click edit script and and the below VB.Net code.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

 _
 _
Partial Public Class ScriptMain
	Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

	Enum ScriptResults
		Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
		Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
	End Enum

	Public Sub Main()
        Dts.Variables("write").Value = Dts.Variables("readOnly").Value.ToString
        MsgBox("Hello " & Dts.Variables("write").Value.ToString)
        Dts.TaskResult = ScriptResults.Success
    End Sub

End Class

Your final package should look something like below.

powershell final ssis package thumb Powershell Script Task for SSIS

Execute the package and, if all goes well, you should see the below message box.

MsgBox Powershell Script Task thumb Powershell Script Task for SSIS

So that’s the basics of working with the Powershell Script Task. I’ve got over my deep hatred of VB.net, since working with it in SSIS so much, but Powershell is a welcome addition to my box of tricks! I’m looking forward to deploying this in future projects. It’s a shame the project seems inactive but this is really something I’d like to see Microsoft provide out-of-the-box in SSIS.

5 Comments

  1. Rhys,

    If you’ve compiled it for 2008, you should upload the DLL back to the CodePlex project as an attachment on a Discussion post or something. Though there’s no installer even if you do that, it takes it one step closer to making it easier for someone else to use it.

    (I need to start learning Powershell!)

  2. Rhys says:

    Hi Todd,

    I didn’t have the ability to attach files to a discussion on codeplex. So I’ve uploaded it here…

    http://www.youdidwhatwithtsql.com/wp-content/downloads/ssis_tasks/Defiant.SqlServer.PowerShellScriptTask.dll

    Rhys

  3. [...] Powershell Script Task for SSIS – Good post on a Powershell Script Task. [...]

  4. Robin Tanner says:

    Rhys,

    In the below code, I’m trying to pipe a list of filenames to an object variable.

    cls
    # PowerShell script to list the txt files under the IDX folder
    cd\
    cd \\[FileServer]\[Directory]
    $Dir = get-childitem \\[FileServer]\[Directory] -recurse
    $List = $Dir | where {$_.extension -eq “.txt”}
    $FileNames = $List | ft name
    $ssis.Variables["User::FileNames"] = $FileNames;

    However, when I do this, it tells me the following:

    Exception setting “CursorPosition”: “The method or operation is not implemented.”

    Any ideas..?

  5. Rhys says:

    Hi Robin,

    I’ve not had the chance to run your code but try removing the ‘cls’. I think this may be causing your error.

    I see your using the -recurse option so using the ForEach loop to get filenames isn’t an option for you?

    Rhys

Leave a Reply