Running PsExec From IIS: PsExecSVC hanging on remote host
I am trying to run a .bat file on a remote host from a web page.
PSEXECSVC (on host) will hang and not close after the execution of the batch causing the server to wait indefinitely.
When running the exact same code (See at bottom) in a Console Application the service closes and everything is fine...The weird part is when the batch file consists of a one liner echo:
@echo off
echo ------ Start -----
exit
PsExecSvc will close and "------ Start -----"
is shown on the page. (This is what I want..)
On the other hand,
@echo off
echo ------ Start -----
echo echo2
exit
PSEXECSVC will hang at the end of execution...
When manually killing PSEXECSVC, only "------ Start -----"
shows and StdErr
prints:
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to novi2...
Starting PsExec service on novi2...
Connecting with PsExec service on novi2...
Starting C:/dbInfo.bat on novi2...
Error communicating with PsExec service on novi2:
No matter what is run in the batch file, only the first line of StdOut is redirected and psexecsvc hangs (but after the whole batch is executed :O ).
If running:
info.bat:
@echo off
cscript /nologo test.vbs
exit
test.vbs:
Set objOut = Wscript.StdOut
objOut.WriteLine "----------------------------------"
objOut.Writeline "Win32_OperatingSystem instance"
objOut.Writeline "----------------------------------"
objOut.close
Wscript.quit 666
A ConsoleApplication will print everything and return 666
IIS will only get "----------------------------------" in StdOut and psexecsvc will hangs...
Here is the code used to run psexec in C#...
Process proc = new Process();
ProcessStartInfo procInfo = new ProcessStartInfo(@"C:/PsExec.exe", @"\\novi2 -u Domain\usernameadmin -p Password C:/info.bat");
procInfo.UseShellExecute = false;
procInfo.RedirectStandardOutput = true;
procInfo.RedirectStandardError = true;
proc.StartInfo = procInfo;
proc.Start();
String output = proc.StandardOutput.ReadToEnd();
String err = proc.StandardError.ReadToEnd();
Trace.WriteLine("Out >> " + output);
Trace.WriteLine("Err >> " + err);
proc.Close();
I am using开发者_StackOverflow社区 IIS 7, ASP MVC3 and .NET4.0 on the Server (Sample projects from VS2010) and WinXP SP3 for the host. To avoid problems the ApplicationPool in IIS is using the same admin account as psexec.
The problem is likely the PSExecSvc on your remote host (novi2) did not clean up after itself and now needs to be deleted before it can run successfully. I now add the following at the end of each script that starts PSExec against a remote host:
sc \\novi2 delete PSExecSvc
Also, if you're instantiating PSExec on, and not against, a remote host, then you'll need to ensure you add the switch for accepting its EULA like so:
psexec \\HOST1 /accepteula -u userid -p password COMMAND
Hope that helps, Lizz
精彩评论