asp.net运行脚本

时间:2012-07-09 14:29:49

标签: c# asp.net wsh

我有以下方法:

 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 个答案:

答案 0 :(得分: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);
}
相关问题