locked
Execute Batch file or windows commands from BizTalk application RRS feed

  • Question

  • Hello,

    One of the client senario, There is one application, in which they are having various cmd and bat files with different arguments. As of now they are calling them using windows task scheduler. Now they want the whole process to be done by BizTalk server.

    What are the possible options for this type of senario? Like

    1. From expression shape

    2. Custom adaptor(Send port)

    or any other.

    Also, if any command execution fails, how can we identify that?

    Thanks for your support.


    Thanks and Regards, Sagar.


    • Edited by Sagar Sharma Thursday, September 6, 2012 10:00 AM
    Thursday, September 6, 2012 8:47 AM

Answers

  • You could use the Schedule Task Adapter to kick off the orchestration. To call the batch files, I would make a C# class to do that for you. Then you can call this class in an expression shape.

    public static class CallBatchFile
        {
            public static void Call(string batchFile)
            {
                
                Process p;
                try
                {
                    p = new Process(); 
                    p.StartInfo = new ProcessStartInfo(batchFile);
                    
                    //Provide arguments                
    //To do: use p.StartInfo.Arguments to provide arguments for the batch file
                    //To read output of process (before error)
                    p.StartInfo.RedirectStandardOutput = true;
                    //To read output of process(after error)
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    p.WaitForExit();               
                    p.Close();
                }
                catch (System.Exception ex)
                {
                    throw new Exception(String.Format("Call() failed with exception {0}.", ex.ToString()));
                }
                
            }

    Thursday, September 6, 2012 10:05 AM

All replies

  • Have a look at Schedule Task adapter from CodePlex. It could help you.

    Christophe BRANDO...If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.

    Thursday, September 6, 2012 9:20 AM
  • Have a look at Schedule Task adapter from CodePlex. It could help you.

    Christophe BRANDO...If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.


    Scheduled Task Adapter for run a .bat OR .cmd file? I don't know how to do that. And on the top of that, I was looking for like sendport type of thing so that I can call it as per the requirements.

    Thanks and Regards, Sagar.

    Thursday, September 6, 2012 9:54 AM
  • You could use the Schedule Task Adapter to kick off the orchestration. To call the batch files, I would make a C# class to do that for you. Then you can call this class in an expression shape.

    public static class CallBatchFile
        {
            public static void Call(string batchFile)
            {
                
                Process p;
                try
                {
                    p = new Process(); 
                    p.StartInfo = new ProcessStartInfo(batchFile);
                    
                    //Provide arguments                
    //To do: use p.StartInfo.Arguments to provide arguments for the batch file
                    //To read output of process (before error)
                    p.StartInfo.RedirectStandardOutput = true;
                    //To read output of process(after error)
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    p.WaitForExit();               
                    p.Close();
                }
                catch (System.Exception ex)
                {
                    throw new Exception(String.Format("Call() failed with exception {0}.", ex.ToString()));
                }
                
            }

    Thursday, September 6, 2012 10:05 AM
  • Thanks Nils for your rply. This is one thing that we can call it from orchestration using an expression shape. Is there any other way, so that I can avoid calling an orchestration, maybe a send port??

    Thanks and Regards, Sagar.

    Thursday, September 6, 2012 10:14 AM
  • If you can write a stored procedure to execute the batch files, you can call the stored procedure through a WCF-SQL send port, but I don't think that's what you're aiming for.

    Another option to is to write a webservice that calls the CallBatchfile class and tie the webservice to a send port. Sounds like a bit of overkill just to call a batch file.

    My solution would be to use the expression shape in an orchestration.

    • Marked as answer by Sagar Sharma Sunday, September 9, 2012 5:50 AM
    • Unmarked as answer by Sagar Sharma Tuesday, September 11, 2012 6:26 AM
    Thursday, September 6, 2012 10:23 AM
  • I would try to avoid using BizTalk for this kind of requirement. I would try to explore SQL Server Jobs to execute those batch files and schedule the jobs. For reporting purpose I would use a table to store the outcomes of the batch files and create reports on them.

    You could use BizTalk also.


    HTH,
    Naushad Alam

    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
    alamnaushad.wordpress.com
    My TechNet Wiki "BizTalk Server: Performance Tuning & Optimization"

    Thursday, September 6, 2012 1:52 PM
    Moderator
  • Should I create a custom adaptor for this?

    Or webservice would be better?


    Thanks and Regards, Sagar.

    Monday, September 17, 2012 11:05 AM
  • So in fact you want to start a process from BizTalk and the reason you cannot use scheduled tasks is because it should be event driven and not time driven?

    In that case you can write a small webservice to take care of this and call it in a BizTalk messaging only scenario. Then you don't need an orchestration.

    You need to be aware though that the webservice is going to wait for the process to finish. If the result needs to be reported back then you might have a challenge if the process is taking too long.

    Otherwise you can look at one-way WCF if you only want to kick-off the process.


    Jean-Paul Smit | Didago IT Consultancy
    Blog | Twitter | LinkedIn
    MCTS BizTalk 2006/2010 + Certified SOA Architect

    Please indicate "Mark as Answer" if this post has answered the question.

    Monday, September 17, 2012 11:26 AM
  • You right, you can call this C# code from the custom pipeline. It is much easier than create a custom adapter. But for such work using an orchestration is a simpler way.

    As a funny workaround you could call this code from a Script functoid in a map and add this map to a port :)


    Leonid Ganeline [BizTalk MVP] BizTalkien: Advanced Questions

    Wednesday, November 28, 2012 4:22 AM
    Moderator