简体   繁体   English

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

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

I want to run tabcmd.exe utility to publish views in tableau server. 我想运行tabcmd.exe实用程序以在Tableau Server中发布视图。 Manual step to do this is as follows, log file will be updated for every step in the location 手动步骤如下,日志文件将针对该位置的每个步骤进行更新

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

In the same session I have to perform this steps manually one by one order 在同一会话中,我必须一步一步地手动执行此步骤

  1. Run cmd.exe 运行cmd.exe
  2. log in by using the command tabcmd login -s "http:/sales-server:8000" -t Sales -u administrator -pp@ssw0rd! 使用命令tabcmd登录-s“ http:/ sales-server:8000” -t Sales -u管理员-pp @ ssw0rd!
  3. Create Project Name using the command tabcmd createproject -n "Quarterly_Reports" -d "Workbooks showing quarterly sales reports." 使用命令tabcmd createproject -n“ Quarterly_Reports” -d“显示季度销售报告的工作簿”创建项目名称
  4. Publish views by using the command tabcmd publish "analysis.twbx" -n "Sales_Analysis" --db-user "jsmith" --db-password "p@ssw0rd" 使用命令tabcmd发布视图publish“ analysis.twbx” -n“ Sales_Analysis” --db-user“ jsmith” --db-password“ p @ ssw0rd”
  5. Refresh by using the command tabcmd refreshextracts --workbook "My Workbook" 通过使用命令tabcmd refreshextracts --workbook“ My Workbook”刷新
  6. Log out by using the command tabcmd logout 使用命令tabcmd注销注销

Now I am trying to automate this steps from my .Net win form, so I used below code as a try and It is not working. 现在,我正在尝试从.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");
            }
        }  

I am able to log in successfully and I am not able to proceed consequent steps further, I have no clue how to proceed on this further, so I am looking forward some help on this. 我能够成功登录,并且无法进一步执行后续步骤,我不知道如何进一步进行此操作,因此我希望对此有所帮助。 Thanks in advance! 提前致谢!

I'm unsure if this is of use to you or not but you could try creating a batch file with all of those commands and the execute the batch file from command prompt like this :- 我不确定这是否对您有用,但是您可以尝试使用所有这些命令创建一个批处理文件,然后从命令提示符处执行该批处理文件,如下所示:

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

I'll leave the output section to yourself. 我将输出部分留给自己。 You could try this, it is not exactly clean but will automate the main steps. 您可以尝试一下,它不是很干净,但是可以自动完成主要步骤。 It's either this, or opening a process for each command as the last process exits? 是这样,还是在最后一个进程退出时为每个命令打开一个进程?

Hope this is of help. 希望这会有所帮助。

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

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