Running command line from aspx page

J

jetoo

Hi there,

i'm trying to run a powershell script from a aspx page.

Here's the code:


//Powershell arguments
String powershell = "C:\\WINDOWS\\system32\
\windowspowershell\\v1.0\\powershell.exe";
String args = " -PSConsoleFile \"C:\\Program Files\
\Microsoft\\Exchange Server\\bin\\exshell.psc1\" -noexit -command \".
\'C:\\Program Files\\Microsoft\\Exchange Server\\bin\
\Exchange.ps1\'\"";

//Process class instance
System.Diagnostics.Process proc = new
System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

//Process file and arguments
proc.StartInfo.FileName = powershell;
proc.StartInfo.Arguments = args;
// Démarrage du processus.
proc.Start();

//Data
StreamWriter sw = null;

string output = proc.StandardOutput.ReadToEnd();
sw = new StreamWriter("C:\\temp\\log.txt");
sw.WriteLine(output);
Console.WriteLine(output);

//Wait till exit
proc.WaitForExit();
proc.Close();


I can see very well the process running in the task manager, but i
can't have output log, so I don't know what's going on.
I'm running it on a windows server 2003.

Any help will be appreciated. Thanks in advance.
 
C

Chris Diver

Any help will be appreciated. Thanks in advance.

Hi,

Try putting your proc.WaitForExit(); line after proc.Start();

I think your getting the output of the process before the process
has had a chance to execute.

HTH,
Chris.

//Process class instance
System.Diagnostics.Process proc = new
System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

//Process file and arguments
proc.StartInfo.FileName = powershell;
proc.StartInfo.Arguments = args;
// Démarrage du processus.
proc.Start();
//Wait till exit
proc.WaitForExit();

//Data
StreamWriter sw = null;

string output = proc.StandardOutput.ReadToEnd();
sw = new StreamWriter("C:\\temp\\log.txt");
sw.WriteLine(output);
Console.WriteLine(output);


proc.Close();
 
J

jetoo

I tried it didn't work.
I also tried to put "test" just in the string output line, it doesn't
work either.
It didn't pu test in the log.txt file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top