0

我有以下方法:

 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 服务器上之后,它不再执行,我也没有收到任何错误。

可能是什么问题?

4

1 回答 1

1

很可能是权限问题 - 服务帐户可能无法写入您指定的目录,或者它没有读取脚本文件本身的权限。

但是,您更大的问题是您的错误处理不够稳健。具体来说,如果脚本导致错误,您将永远不会检测到(正如您所发现的那样!!)。

您需要做些什么来解决这个问题取决于您的脚本如何报告它的错误。您通常需要重定向StandardOutputStandardError,并检查退出代码

这是一种可能实现的粗略概述。您将需要根据您的脚本和环境对其进行定制。

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);
}
于 2012-07-09T14:53:32.950 回答