Run cmd or exe file from asp.net page - c#

I have problem with running cmd or exe file from asp.net page.
I used this code:
protected void btnRun_OnClick(object sender, EventArgs e)
{
var p = new Process();
var info = new ProcessStartInfo
{
FileName = Server.MapPath("~/print.cmd"),
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
};
p.StartInfo = info;
p.Start();
p.WaitForExit();
}
When I run my web application on VS Development Server everything is ok,
but when I deploy this code on IIS (ver 7.5), the code working without errors but
nothing happens.
I used search on google and SO, but I didn't find any solution
I tried http://support.microsoft.com/kb/555134 and http://support.microsoft.com/default.aspx?scid=kb;en-us;317012
I set 'Allow service to interact with desctop' for IIS Admin service.
I played with user rights but this didn't give me any results.
I changed Identity for Application Pools
is it possible to run cmd or exe with IIS?

Try:
var info = new ProcessStartInfo
{
FileName = Server.MapPath("~/print.cmd"),
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Hidden,
};

Related

How to call a console app as a different user, using a Windows service being run under Local System?

I am basically trying to create and run a Windows Service on a server. This service has mainly 2 jobs,
To log a timestamp to a file on a UNC (different Server path) so as to check access
Call a simple console app using a different user
Now, I have created the windows service, but it won't log in the Server path as it is started via Local system. So I tried to deploy it using a user account with access to the Server Path and it logged, but then the whole point is to us the local system.
So now, i changed the requirement a little bit, Now is it possible to deploy the service as Local System only , but instead of directly logging on the server path, i would rather make the console app do the logging. But how should I make the console app run as a different user account.
This is the code I am using for calling the console app via a different user (mind you the service is run under LocalSystem), and it is either giving the error, "Cannot find the file specified" or "Invalid Directory Name" but i assure you both the things exist. I even used the paths fed in the startInfo by debugging to check.
public static void InitiateCommandProcess()
{
Process process = new Process();
SecureString ssPwd = new SecureString();
string fileName = ConfigurationManager.AppSettings["FileName"];
string workingDirectory = ConfigurationManager.AppSettings["ApplicationDirectory"];
string param = ConfigurationManager.AppSettings["PARAM"];
string userName = "myUserName";
string password = "MyPassWord";
for (int x = 0; x < password.Length; x++)
{
ssPwd.AppendChar(password[x]);
}
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
ErrorDialog = false,
FileName = #"" + fileName,
WorkingDirectory = #"" + workingDirectory,
Arguments = #"" + param,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
Domain = "EMEA",
UserName = userName,
Password = ssPwd
};
process.StartInfo = startInfo;
try
{
Logger.Log($"Working in Directory Path : {workingDirectory} );");
Logger.Log($" Username : {userName} and Executing : {fileName}");
process.Start();
Logger.Log($"Process \"{ConfigurationManager.AppSettings["FileName"]}\" Executed at {DateTime.Now}");
}
catch (Exception ex)
{
Logger.Log($"Error occured in command process execution, Message : {ex.Message}", true);
}
}
The server path is like this, "\dfs\folder\folder..."
I am a little new to this so please bear with me.

Run app as other user in C#

I've got this project that needs to get finished in two days and I'm nearly done with it. Except there's a part of it that needs to start another app and run it as a different domain user.
So far I've got this in code:
Process proc = new Process();
System.Security.SecureString ssPwd = new System.Security.SecureString();
string password = "SomePassword";
for (int x = 0; x < password.Length; x++)
{
ssPwd.AppendChar(password[x]);
}
ProcessStartInfo StartInfo = new ProcessStartInfo
{
FileName = "Translink.exe",
WorkingDirectory = #"C:\Program Files\Western Union\Universal-Release",
UserName = "someuser",
Domain = "somedomain",
Password = ssPwd,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
proc = Process.Start(StartInfo);
proc.WaitForExit();
So when I make a call with and without user and password for example:
C:\Windows\System32\notepad.exe
it runs in both cases but everytime I try to run that particular app "Translink.exe" from either Program Files or Program Files (x86) it just won't run at all with or without runas.
And I've ran out of things to try, I think I've tried everything so far in stackoverflow as there were other similar articles but I think I must be going wrong somewhere specifically.

Getting execption whenever it runs from process.start

so im having this weird issue with a win 7 pc. I have this application that runs this powershell script from a .bat file.
This is the code:
public void GetLastestEarthBackground()
{
var directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts");
var vbsFile = Path.Combine(directoryPath, Settings.VBSFileName);
if (File.Exists(vbsFile))
{
var process = new Process
{
StartInfo =
{
WorkingDirectory = directoryPath,
Verb = "runas",
UseShellExecute = true,
FileName = "run.bat", //Settings.VBSFileName,
Arguments = "//B //Nologo"
}
};
process.Start();
process.WaitForExit();
}
}
When i run it from my application i see this in the command line:
But if i double click on the batch file i get this resut:
Any ideas?
This seems like a issue with user rights, the program running the script probably does not have the same rights as your user.

Using Process.Start() with IE and waiting for web response

Is there a way to capture a response from the Process class using it to launch Internet Explorer.
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"C:\Program Files\Internet Explorer\iexplore.exe",
Arguments = "http://www.google.com",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
System.Threading.Thread.Sleep(3000);
proc.Kill();
I tried :
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// do something with line
}
but IE sends back an immediate response of hey, i opened and called the URL. Anyone have any ideas?
There is no way of getting information from IE when it is running in a separate process.
However, you can run it inside of a Web Browser control in your own Form. IE then exposes a number of events that you can listen to.

Ensuring Process.Start() runs under the logged in user

I'm running a batch file from some ASP.NET/C# code on a web server. Basically the batch file performs some test automation tasks on a VM using tools like psloggedon and pexec.
If I run the batch file manually when I'm logged into the server under an administrative account, it works fine.
My problem comes when I run it from my code (below), it seems to run under the 'SYSTEM' account, and psloggedon etc. don't seem to work correctly.
Code
Process p = new Process();
p.StartInfo.FileName = "C:\SetupVM.bat";
p.Start();
p.WaitForExit();
I've got this in my web.config, it doesn't seem to make any differance?
<identity impersonate="true" userName="Administrator" password="myadminpassword"/>
Is there anyway I can ensure the batch file runs under the 'Administrator' account?
UPDATED CODE
Process p = new Process();
p.StartInfo.FileName = "C:\\SetupVM.bat";
p.StartInfo.UserName = "Administrator";
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = "C:\\";
string prePassword = "myadminpassword";
SecureString passwordSecure = new SecureString();
char[] passwordChars = prePassword.ToCharArray();
foreach (char c in passwordChars)
{
passwordSecure.AppendChar(c);
}
p.StartInfo.Password = passwordSecure;
p.Start();
p.WaitForExit();
From MSDN:
When UseShellExecute is false, you can start only executables with the Process component.
Maybe this is the issue as I'm trying to run a .bat file?
Thanks.
You can provide the username and password to the StartInfo:
Process p = new Process();
p.StartInfo.FileName = "C:\SetupVM.bat";
p.StartInfo.UserName = "Administrator";
p.StartInfo.Password = "AdminPassword";
p.Start();
p.WaitForExit();
See the documentation for ProcessStartInfo.

Categories

Resources