2013-02-15 31 views
2

我想運行tabcmd.exe utility以在Tableau Server中發佈視圖。手動工序要做到這一點如下,日誌文件中的位置在命令行實用程序中使用.dll中的cmd.exe運行多個命令

每一步都將被更新 「C:\用戶[名] \ AppData \漫遊\的Tableau \ tabcmd.log」

在我執行此相同的會話由一個訂單

  1. 運行cmd.exe的
  2. 日誌手動步驟一個通過使用命令tabcmd登錄-s「HTTP:/銷售服務器:8000」 -t銷售-u管理員-pp @ ssw0rd!
  3. 使用命令創建項目名稱tabcmd createproject -n「Quarterly_Reports」-d「顯示季度銷售報告的工作簿」。
  4. 通過使用命令使用命令tabcmd發佈 「analysis.twbx」 發表意見-n 「Sales_Analysis」 --db用戶 「JSMITH」 --db密碼 「P @ ssw0rd」
  5. 刷新tabcmd refreshextracts --workbook「我的工作簿」
  6. 註銷通過使用命令tabcmd註銷

現在我想從自動我的。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 "[email protected]"); 
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"); 
      } 
     } 

我能夠成功登錄了,我不能夠繼續進行後續步驟,我不知道如何在此進行進一步的,所以我期待着一些這方面的幫助。 在此先感謝!

+0

重定向輸出,但實際上沒有讀它是一個壞主意。當輸出緩衝區已滿並且因爲沒有讀取而不能清空時,程序將會掛起。快速發生,通常大約2千字節。 – 2013-02-15 15:28:18

回答

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 [email protected]!", 
     @"tabcmd createproject -n 'Quarterly_Reports' -d 'Workbooks showing quarterly sales reports.'", 
     @"abcmd publish 'analysis.twbx' -n 'Sales_Analysis' --db-user 'jsmith' --db-password '[email protected]'", 
     @"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") 

我將把輸出部分留給你自己。你可以試試這個,它不完全乾淨,但會自動化主要步驟。這是要麼,或者爲最後一個進程退出時爲每個命令打開一個進程?

希望這有幫助。

+0

我不明白你在這裏建議什麼。 p.StartInfo.Arguments = @「\ YourLocation \ tabcmd.bat」; – Dev 2013-02-15 16:43:18

+0

您生成批處理文件,然後將其保存到某個位置,當您啓動進程以運行批處理文件時,參數指向該文件,以便cms.exe啓動它時運行批處理文件。 – Derek 2013-02-16 08:00:05

+0

如果我手動運行批處理文件,它可以正常工作,但是如果通過代碼運行它,則不會發生任何事情。 – Dev 2013-02-18 10:36:33