2012-07-09 164 views
0

我有以下方法:asp.net運行腳本

protected void RunWSFScript() 
    { 
     try 
     { 
      System.Diagnostics.Process scriptProc = new System.Diagnostics.Process(); 
      scriptProc.StartInfo.FileName = @"cscript"; 
      scriptProc.StartInfo.Arguments = "\\\\server\\folder\\script.wsf \\\\server\\folder\\" 
       + file + ".xml"; 
      scriptProc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //prevent console window from popping up 
      scriptProc.Start(); 
      scriptProc.WaitForExit(); 
      scriptProc.Close(); 

      string message = @"Please verify output."; 
      System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
      sb.Append("<script type = 'text/javascript'>"); 
      sb.Append("window.onload=function(){"); 
      sb.Append("alert('"); 
      sb.Append(message); 
      sb.Append("')};"); 
      sb.Append("</script>"); 
      ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString()); 
     } 
     catch 
     { 
      string message = "An error has occured. Please try again."; 
      System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
      sb.Append("<script type = 'text/javascript'>"); 
      sb.Append("window.onload=function(){"); 
      sb.Append("alert('"); 
      sb.Append(message); 
      sb.Append("')};"); 
      sb.Append("</script>"); 
      ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString()); 
     } 
    } 

我遇到的問題是,該腳本正確執行,當我在本地運行調試時,但之後,我已經把網站上的那個方法iis服務器不再執行,我也沒有收到任何錯誤。

可能是什麼問題?

回答

1

這很可能是一個權限問題 - 服務帳戶可能無法寫入您指定的目錄,或者它沒有權限讀取腳本文件本身。

但是,你更大的問題是你的錯誤處理不夠健壯。特別是,如果腳本導致錯誤,您將永遠無法檢測到(如您已經發現的那樣!!)。

你需要做些什麼來解決這個問題,取決於腳本如何報告錯誤。您通常需要重定向StandardOutputStandardError,並檢查exit code

以下是的一個可能的執行的粗略輪廓。您需要根據您的腳本和環境進行調整。

System.Diagnostics.Process scriptProc = new System.Diagnostics.Process(); 
scriptProc.StartInfo.FileName = @"cscript"; 
scriptProc.StartInfo.RedirectStandardOutput = true; 
scriptProc.StartInfo.RedirectStandardError = true; 
scriptProc.StartInfo.Arguments = @"\\server\some\folder\script.wsf"; 
scriptProc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 

scriptProc.Start(); 

// Read out the output and error streams *before* waiting for exit. 
string output = p.StandardOutput.ReadToEnd(); 
string error = p.StandardError.ReadToEnd(); 

// Specify a timeout to ensure it eventually completes - this is 
// unattended remember!! 
scriptProc.WaitForExit(10000); 
scriptProc.Close(); 

// Check the exit code. The actual value to check here will depend on 
// what your script might return. 
if (script.ExitCode != 0) 
{ 
    throw new Exception(
     "Oh noes! Exit code was " + script.ExitCode + ". " + 
     "Error is " + error + ". " + 
     "Output is " + output); 
} 
+0

謝謝。是的,我知道錯誤處理還遠遠沒有完成,我只是想讓腳本先執行然後移到那個腳本上。基本上每個人都通過NTLM登錄到網站,並且每個人都可以完全控制文件夾/文件,所以我還應該檢查什麼? – user1468537 2012-07-10 06:46:54

+0

已經嘗試了上面的內容,並且當它到達這一行''''''''''''''''''''''''或'string error = scriptProc.StandardError.ReadToEnd();'' – user1468537 2012-07-10 07:28:07

+0

不知道錯誤是什麼正面臨的是你在黑暗中拍攝。這就是爲什麼你需要在嘗試修復你的bug之前讓你的錯誤處理得到修復。 ReadToEnd()調用拋出什麼錯誤? – 2012-07-10 07:43:27