Ошибка повышения разрешения при запуске кода промпакета - PullRequest
0 голосов
/ 28 февраля 2019

Я пытаюсь удалить старые сертификаты из ОС, поэтому я написал метод для этого:

    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, а затем запускаюсь от имени администратора, это работает, если я просто дважды щелкаю, а не

пользователь, которого я запускаю, находится в группе локальных администраторов

...