I have an issue trying to execute commands in a web service. The command
starts under the specified user. However, it never completes its execution. I
can execute simple commands like "echo HELLO" and cmd.exe starts but never
terminates. I'm relatively new to .NET, so I may be forgetting some thing
obvious. Thanks! Here's the code:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Diagnostics;
using System.Security;
using System.Threading;
[WebService(Namespace = "http://urldotcomgoeshere")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Class1 : System.Web.Services.WebService
{
public Class1()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string generateWebAccount(string account)
{
string command = Environment.ExpandEnvironmentVariables("%comspec%" );
string args = "";
string returnInfo = "";
try
{
SecureString secPass = new SecureString();
foreach (char c in "password")
{
secPass.AppendChar(c);
}
//EXECUTE COMMANDS
//C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\50\bin\owsadm.exe
//NET
//cmd /c cacls
//cmd /c copy
//command = @"g:\webcreation.bat";
args = "/c echo \"hello\" > g:\\webcreation.log";
ProcessStartInfo startInfo = new ProcessStartInfo(command, args);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UserName = "someuser";
startInfo.Password = secPass;
startInfo.Domain = "somedomain";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
Process proc = Process.Start(startInfo);
proc.WaitForExit();
if (proc.ExitCode != 0)
{
returnInfo = returnInfo + "unsuccessful " + command + args +
proc.StandardOutput.ReadToEnd() + "\n" +
proc.StandardError.ReadToEnd() + "\n";
}
else
{
returnInfo = returnInfo + "successful " + command + args +
proc.StandardOutput.ReadToEnd();
}
}
catch (Exception e)
{
return e.Message;
}
return returnInfo;
}
}