0

I have the following method:

 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());
        }
    }

The problem I'm having is that the script executes correctly when I run that method when debugging locally but after I have placed the site on the iis server it no longer executes and I am not getting any errors either.

What can be the issue?

1 Answer 1

1

Most likely it is a permissions problem - the service account probably cannot write to the directory you specified, or it does not have permissions to read the script file itself.

However, your bigger issue is that your error handling is not sufficiently robust. Specifically, if the script causes an error, you will never detect that (as you have found out!!).

What you need to do to fix this depends slightly on how your script will report it's errors. You will generally want to redirect StandardOutput and StandardError, and check the exit code.

Here is an rough outline of one possible implementation. You will need to tailor it to your script and environment.

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Yes I know the error handling is far from complete, I'm just trying to get the script to execute first then move onto that. Basically everyone logs into the site via NTLM and everyone does have full control over the folder/files so what else should I be checking for?
Have tried the above and it keeps crashing when it gets to this line string output = scriptProc.StandardOutput.ReadToEnd(); or on string error = scriptProc.StandardError.ReadToEnd();
Without knowing what the error you are facing is you are shooting in the dark. That's why you need to get your error handling fixed before attempting to fix your bug. What errors do the ReadToEnd() calls throw?
If I step through it and just hover over the different Process methods, I can see System.InvalidOperationException on most of them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.