Elevating permission error while running batch prom code - c#

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

Related

C# Process Argument is not working with startInfo.Arguments

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.

Open hidden internet explorer using c#

I saw that many people asked this question but it just does not work for me!
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\Internet Explorer\iexplore.exe";
startInfo.Arguments = Url.ToString();
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
catch (Win32Exception ex)
{
var err = ex.ToString();
}
It still opens the browser !
anybody can say why?

Create a process that execute powershell in C# but failed outpt

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.

process.startInfo fails on windows 2012 64-bit

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;
}

Command line run error

After looking on forums, I have written this snippet:
public string ExecuteCmd()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = this.m_command;
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
The m_command is member of a class, initialized in the constructor.
For my tests, it is net user. When the compiler arrives at this point, I get the following exception:
StandardOut has not been redirected or the process hasn't started yet.
Where is my mistake?
You need this:
//....
startInfo.Arguments = "/C " + this.m_command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//....

Categories