使用.Net win表单中的cmd.exe在命令行实用程序中运行多个命令

Dev*_*Dev 2 c# cmd winforms tableau-api

我想运行tabcmd.exe实用程序以在tableau服务器中发布视图.手动执行此操作的步骤如下,将更新该位置中每个步骤的日志文件

"C:\用户[用户名] \应用程序数据\漫游\的Tableau\tabcmd.log"

在同一个会话中,我必须逐个手动执行此步骤

  1. 运行cmd.exe
  2. 使用命令tabcmd登录-s"http:/ sales-server:8000"-t Sales -u administrator -pp @ssw0rd登录!
  3. 使用命令tabcmd createproject -n"Quarterly_Reports"-d"显示季度销售报告的工作簿"创建项目名称.
  4. 使用命令tabcmd发布"analysis.twbx"-n"Sales_Analysis"--db-user"jsmith"--db-password"p @ ssw0rd"发布视图
  5. 使用命令tabcmd refreshextracts -workbook"My Workbook"刷新
  6. 使用命令tabcmd logout注销

现在我试图从我的.Net win表单自动执行这些步骤,所以我使用下面的代码作为尝试,它不起作用.

String path = @"C:\Program Files (x86)\Tableau\Tableau Server\7.0\bin\tabcmd.exe"
ProcessStartInfo startInfo = new ProcessStartInfo ();
startInfo.FileName = "\""+path+ "\"";
startInfo.Arguments = String.Format("login -s http://Server1:8000 --db-user "jsmith" --db-password "p@ssw0rd");
startInfo.UseShellExecute = false ;
startInfo.CreateNoWindow = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();      


using (StreamWriter sw = p.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine("createproject -n \"MyProject\" -d \"MyProjectWorkbook\"");
                //sw.WriteLine("My next Command");
                //sw.WriteLine("My next Command");
            }
        }  
Run Code Online (Sandbox Code Playgroud)

我能够成功登录,我无法继续进行后续步骤,我不知道如何继续这样做,所以我期待着对此有所帮助.提前致谢!

Der*_*rek 5

我不确定这是否对您有用,但您可以尝试使用所有这些命令创建批处理文件,并从命令提示符执行批处理文件,如下所示: -

  //Build Commands - You may have to play with the syntax

    string[] cmdBuilder = new string[5] 
      { 
       @"tabcmd login -s 'http:/sales-server:8000' -t Sales -u administrator -p p@ssw0rd!",
       @"tabcmd createproject -n 'Quarterly_Reports' -d 'Workbooks showing quarterly sales reports.'",
       @"abcmd publish 'analysis.twbx' -n 'Sales_Analysis' --db-user 'jsmith' --db-password 'p@ssw0rd'", 
       @"tabcmd refreshextracts workbook 'My Workbook'",
       @"tabcmd logout"

      };


     //Create a File Path

    string BatFile = @"\YourLocation\tabcmd.bat";

    //Create Stream to write to file.

    StreamWriter sWriter = new StreamWriter(BatFile);

     foreach (string cmd in cmdBuilder) 
        { 
          sWriter.WriteLine(cmd); 
        }

       sWriter.Close();

    //Run your Batch File & Remove it when finished.


   Process p = new Process();
   p.StartInfo.CreateNoWindow = true;
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.FileName = "C:\\cmd.exe";
   p.StartInfo.Arguments = @"\YourLocation\tabcmd.bat";
   p.Start();
   p.WaitForExit();
   File.Delete(@"\YourLocation\tabcmd.bat")
Run Code Online (Sandbox Code Playgroud)

我会把输出部分留给自己.你可以试试这个,它不是很干净但会自动完成主要步骤.它是这个,还是在最后一个进程退出时为每个命令打开一个进程?

希望这有帮助.