2

I am using a Script Task in SQL Server Integration Services (SSIS 2008) to run a command on the Windows command line. The command simply decrypts a file with a known filename.

When I execute the task, the Script task box turns green and reports that it ran successfully, but when checking on the file, I see that nothing at all has happened. Below is my code. Any idea what I'm doing wrong here? Thank you in advance.

Update: My goal is to decrypt a file from a C# SSIS script, and to learn why the code below doesn't do that. I'm not as concerned with whether or not the task box turns green or reports a success.

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Diagnostics;


    public void Main()
    {
        string DecryptCommand;

        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        DecryptCommand = "echo my_passphrase|gpg --batch --passphrase-fd 0 --decrypt-files      my_file_" + DateTime.Now.ToString("MMddyyyy") + ".pgp"; 
        var process = new Process { StartInfo = startInfo };

        process.Start();
        process.StandardInput.WriteLine("cd D:\\Foo\\Bar\\EncryptedFiles");
        process.StandardInput.WriteLine(DecryptCommand);
        process.StandardInput.WriteLine("exit");
        process.WaitForExit();

    }
}

}

5
  • Building on what @SomeGuy has indicated, you never check the return code from process or look at the StandardOuput and StandardError streams. Pop the former into an OnInformation and the latter to OnError events and then things will start happening.
    – billinkc
    Commented Feb 4, 2014 at 3:14
  • Any reason you aren't just using an execute process task for this?
    – Nick.Mc
    Commented Feb 4, 2014 at 4:36
  • ElectricLlama - I couldn't get the execute process task to work. I called cmd.exe and input the arguments, but all that happens is a command window opens and the arguments do not run. I suppose I could try scripting the creation of a batch file (because I need a dynamic date) then try running the batch as a task. Commented Feb 4, 2014 at 16:16
  • This problem for me has been solved, but it would be great if this question about the code were answered. My problem was solved by writing and executing a script which creates a batch file. Then an Execute Process Task runs the batch file. HOWEVER - The main crux of this page is, how to you call CMD.EXE from C# code and run a command? I would still love to know exactly what's missing from my code above which causes the command and its arguments/switches to actually run, and actually manipulate the intended file. Commented Feb 4, 2014 at 17:30
  • You need to use /C as the first argument to your "Arguments" entry and specify cmd.exe as the executable.
    – Thronk
    Commented Apr 22, 2015 at 17:12

1 Answer 1

1
Dts.TaskResult = (int)ScriptResults.Success;

This line means you will always return true when the process exits, regardless of what the process returned. Instead you likely want to capture the process' exit code (even better, log its general output while debugging) and use that to evaluate whether or not your script succeeded.

Going a step further, is there a reason you are using command.exe rather than executing a batch or vb script? That would give you more control over your output.

Edit: As for why your program isn't executing as intended, have you tried manually flushing your streams after inputs?

5
  • I have removed that line of code from my example as it is irrelevant to my actual problem. I realize that's a standard piece of code in every SSIS script task. I'm more concerned with, why is my code not executing the decrypt commands in the Windows command line. Commented Feb 4, 2014 at 15:53
  • Before we start digging into why it's not working, why take this approach? It seems easier to simply call the batch file rather than introduce the added layer of complexity (and overhead!) created by first calling CMD, then using it to call the batch file!
    – SomeGuy
    Commented Feb 4, 2014 at 18:22
  • I don't actually see it as complex, or too much overhead. In order to decrypt my file, I have to use a script task which, via the code, creates the batch file. Then I have to use an execute process task to run that batch file. Sure, its easy, but its 2 steps. I don't see whats wrong with instead using only one script task step, especially if its not a ton of code? Since there isn't a built-in decrypt task in SSIS 2008, I think using a small bit of code to accomplish the step is totally reasonable. Lastly, I simply want to learn how to accomplish cli taks from code. To be a better programmer! Commented Feb 5, 2014 at 17:49
  • At any rate, looking through your code, things appear to work if you remember to flush your streams.
    – SomeGuy
    Commented Feb 5, 2014 at 18:04
  • Thanks. I tried this after every input but the file wasn't decrypted and no errors thrown::::: process.StandardInput.Flush(); Commented Feb 5, 2014 at 21:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.