i have a small application to configure IIS using Process.StartInfo. The application works fine in Windows 2008 R2 64-bit and all the 32-bit machines. I dont see much differensce even if i use Runas. The application is run as local admin on the box.
the code is below
namespace ConsoleApplication1
{
class Program
{
private const string configIISParameters2012 = "/k START /w C:\\Windows\\SysNative\\PKGMGR.EXE /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;
private const string exeCommand = "CMD.EXE";
static void Main()
{
RunCommand(exeCommand, configIISParameters2012);
}
public static bool RunCommand(String command, String arguments)
{
bool ret = false;
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.Verb = "unas";
startInfo.FileName = command;
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
process.Start();
process.WaitForExit();
process.Close();
ret = true;
}
catch (Exception ex)
{
//Utility.Log("Exception executing command :" + ex.StackTrace);
}
return ret;
}
Related
I am doing an unattended installer, I ran it with cmd as below, and it is working without any problem.
setup.exe -q -J -Djava.security.manager=allow
Now I am trying to use the same arguments in my c# console application code, but it will ignore the arguments. Can you please check the method below and support if there is a way to do it?
static int RunSetup(out string stdout)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = "-q -J -Djava.security.manager=allow";
startInfo.FileName = "setup.exe";
startInfo.WorkingDirectory = Environment.CurrentDirectory;
process.StartInfo = startInfo;
process.Start();
stdout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
int exitCode = process.ExitCode;
process.Close();
return exitCode;
}
catch (Exception exception)
{
throw new Exception("exeption = " + exception.Message);
}
}
I wrote a small .net console application that just prints the arguments that are passed to the application:
Console.WriteLine(string.Join(":", args ));
and named it setup.exe.
When i used your code example to call it it prints out -q:-J:-Djava.security.manager=allow as expected, so your code seem to work just as you expected. This was tested using .NET 7, though.
I am trying to call an executable from a webapi. When I run the code in Visual Studio it works perfect, but when I host it on a test server on IIS it doesn't work. Also I don't get any errors. What am I missing here.? Other functions which doesn't require that executable works fine.
Here is my code.
string output = "";
ProcessStartInfo startinfo = new ProcessStartInfo();
var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ExeDir");
var err = "";
startinfo.FileName = path + #"\Executable.exe";
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
try
{
process.Start();
}
catch(Exception e)
{
err = e.Message;
}
I am trying to remove old certificates from the os so i wrote a method for that:
public ActionResult DeleteOldCertificates(Session session)
{
try
{
return (DeleteAutority(session) == ActionResult.Success
? Delete2018(session) == ActionResult.Success
? ActionResult.Success : ActionResult.Failure
: ActionResult.Failure);
}
catch (Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
public ActionResult Delete2018(Session session)
{
try
{
var constants = new Constants(session);
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C CERTUTIL.exe -delstore MY Loi2018";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
session.Log($"Delete2018 ExitCode: {process.ExitCode}");
session.Log($"Delete2018 Message: {process.StandardOutput.ReadToEnd()}");
return process.ExitCode != 0 ? ActionResult.Failure : ActionResult.Success;
}
catch (Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
public ActionResult DeleteAutority(Session session)
{
try
{
var constants = new Constants(session);
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/C CERTUTIL.exe -delstore -enterprise root Autority";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
session.Log($"DeleteAutority ExitCode: {process.ExitCode}");
session.Log($"DeleteAutority Message: {process.StandardOutput.ReadToEnd()}");
return process.ExitCode != 0 ? ActionResult.Failure : ActionResult.Success;
}
catch (Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
Unfortunatelly during execution I am receiving error:
Message: Administrator permissions are needed to use the selected options. Use an administrator command prompt to complete these tasks.
CertUtil: The requested operation requires elevation.
On the windows 7 this code works. On the Windows server 2012 if i use rmb and then run as admin then it is working if I just double click then not
user I am running this is in local admins group
I dont know where is my missing. Even it dit not write out log. Could you fix my test code:
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.Arguments = #"/C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
//startInfo.UseShellExecute = false;
//startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex) {
Debug.Print("Error: " + ex.Message);
}
}
Finally got it to work, code should look like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"/C powershell.exe /C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
This worked for me! You can use full paths to cmd.exe and powershell.exe if you feel necessary, it's not that important here.
I want to run cmd command with C# for install service in Windows, I'm using of below code:
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments =
"\"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe\" \"D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe\"";
process.StartInfo = startInfo;
process.Start();
}
}
but this program don't work. if I run this command in cmd.exe work properly but when I run this project don't execute command and MyNewService.exe don't install.
where is my problem?
do you help me?
Instead of starting a cmd.exe and passing installutil as an argument (then your service executabel an argument of the argument), try starting the installutil.exe executable directly passing the MyNewService.exe as the argument.
You should always wait for the process to exit, and should always check the exit code of the process as below.
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe";
startInfo.Arguments = "D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe";
process.StartInfo = startInfo;
bool processStarted = process.Start();
process.WaitForExit();
int resultCode = process.ExitCode;
if (resultCode != 0)
{
Console.WriteLine("The process intallutil.exe exited with code {0}", resultCode);
}
}