我正在尝试从操作系统中删除旧证书,所以我为此编写了一个方法:
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;
}
}
不幸的是,在执行过程中我收到错误:
Message: Administrator permissions are needed to use the selected options. Use an administrator command prompt to complete these tasks.
CertUtil:请求的操作需要提升。
在 Windows 7 上,此代码有效。在 Windows server 2012 上,如果我使用 rmb 然后以管理员身份运行,那么如果我只是双击它就可以工作,然后不行
我正在运行的用户在本地管理员组中