繁体   English   中英

从.Net Win Form使用cmd.exe在命令行实用工具中运行多个命令

[英]Running multiple commands in a command line utility using cmd.exe from .Net win form

我想运行tabcmd.exe实用程序以在Tableau Server中发布视图。 手动步骤如下,日志文件将针对该位置的每个步骤进行更新

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

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

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

现在,我正在尝试从.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");
            }
        }  

我能够成功登录,并且无法进一步执行后续步骤,我不知道如何进一步进行此操作,因此我希望对此有所帮助。 提前致谢!

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

  //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")

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

希望这会有所帮助。

暂无
暂无

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

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