繁体   English   中英

如何从SSIS调用cmd.exe并在命令行上执行命令

[英]How to call cmd.exe from SSIS and execute commands on the command line

我在SQL Server集成服务(SSIS 2008)中使用脚本任务在Windows命令行上运行命令。 该命令仅解密具有已知文件名的文件。

当我执行任务时,“脚本”任务框会变成绿色,并报告它已成功运行,但是在检查文件时,我发现什么都没有发生。 下面是我的代码。 知道我在做什么错吗? 先感谢您。

更新:我的目标是从C#SSIS脚本解密文件,并了解为什么下面的代码不这样做。 我不关心任务栏是否变为绿色或报告成功。

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();

    }
}

}

Dts.TaskResult = (int)ScriptResults.Success;

这行意味着无论进程返回什么,您都将在进程退出时始终返回true。 取而代之的是,您可能想捕获进程的退出代码(甚至更好,在调试时记录其一般输出),然后使用该代码来评估脚本是否成功。

再进一步,是否有理由使用command.exe而不是执行批处理或vb脚本? 这样可以使您更好地控制输出。

编辑:至于为什么您的程序未按预期执行,您是否尝试过在输入后手动刷新流?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM