在.Net窗体中使用cmd.exe运行多个命令的命令行实用程序

3

我想要运行tabcmd.exe实用程序来在Tableau服务器上发布视图。手动操作步骤如下,日志文件将在以下位置更新每个步骤:

"C:\Users[UserName]\AppData\Roaming\Tableau\tabcmd.log"

在同一会话中,我必须按照一个接一个的顺序手动执行这些步骤:
  1. 运行 cmd.exe
  2. 使用命令 tabcmd login -s "http:/sales-server:8000" -t Sales -u administrator -p p@ssw0rd! 登录
  3. 使用命令 tabcmd createproject -n "Quarterly_Reports" -d "Workbooks showing quarterly sales reports." 创建项目名称
  4. 使用命令 tabcmd publish "analysis.twbx" -n "Sales_Analysis" --db-user "jsmith" --db-password "p@ssw0rd" 发布视图
  5. 使用命令 tabcmd refreshextracts --workbook "My Workbook" 刷新
  6. 使用命令 tabcmd logout 退出登录
现在我正在尝试从我的 .Net 窗体自动化这些步骤,所以我使用了下面的代码作为尝试,但它没有起作用。
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");
            }
        }  

我能成功登录,但无法继续进行后续步骤,我不知道该如何进一步操作,因此希望得到帮助。 提前感谢!


重定向输出但实际上不读取它是一个坏主意。当输出缓冲区已满且由于您没有读取它而无法清空时,程序将会挂起。这通常很快发生,大约在2千字节左右。 - Hans Passant
1个回答

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

我会把输出部分留给你自己处理。你可以尝试这个方法,虽然不是很简洁,但可以自动化主要步骤。要么就为每个命令打开一个进程,直到最后一个进程退出。希望这对你有所帮助。

我不明白你在这里建议什么。 p.StartInfo.Arguments = @"\YourLocation\tabcmd.bat"; - Dev
你需要创建批处理文件并将其保存到指定位置,在启动进程运行批处理文件时,参数指向该文件,这样当 cms.exe 启动时就会运行该批处理文件。 - Derek
如果我手动运行批处理文件,它可以正常工作,但如果通过代码运行它,什么也不会发生。 - Dev
p.StartInfo.Arguments 不是必需的,只需要 p.StartInfo.FileName = @"\YourLocation\tabcmd.bat"; 就足够了。 - Dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接