最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows - How to install CA certificate into LocaMachine Root via powershell under standard user (Administrators group) - Stack

programmeradmin0浏览0评论

I need to install CA certificate into LocaMachine Root via powershell. I'm trying to run this script with elevated privileges to Standard user from Administrators group.

Import-PfxCertificate -Password (ConvertTo-SecureString -String 'certificatePassword' -AsPlainText -Force) -CertStoreLocation Cert:\LocalMachine\Root -FilePath 'filePath'

But, it fails on Acces Denied error.

If I execute the same script with elevated privileges to predefined windows Administrator user, then it works without error.

How to configure Standard user permissions to allow access?

I need to install it remotely via API and I don't want to require the predefined windows Administrator credentials from app using this API.

Thx in advance.

UPDATE (here is the screenshot of Standard user from Administrators group and the C# code where the process is started with credentials of this user):

command = the powershell script above

adminUsername = 'Admin'

adminPasswprd = '*****'

domain = 'computername'

public static (bool, string) ExecuteCommand(string command, string adminUsername, string adminPassword, string domain = "")
    {
        try
        {
            SecureString securePassword = new SecureString();
            foreach (char c in adminPassword)
            {
                securePassword.AppendChar(c);
            }

            // Create PowerShell process information
            ProcessStartInfo processInfo = new ProcessStartInfo()
            {
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{command}\"",
                UseShellExecute = false, 
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = false,
                CreateNoWindow = true,
                UserName = adminUsername,
                Password = securePassword,
                Domain = domain,
                Verb = "runas" 
            };

            using (Process process = new Process())
            {
                process.StartInfo = processInfo;
                process.Start();
                string standardOutput = process.StandardOutput.ReadToEnd();
                string errorOutput = process.StandardError.ReadToEnd();
                process.WaitForExit();

                if (!string.IsNullOrEmpty(errorOutput))
                {
                    standardOutput += Environment.NewLine + "Error: " + errorOutput;
                }

                return (string.IsNullOrEmpty(errorOutput), standardOutput);
            }
        }
        catch (Exception ex)
        {
            return (false, $"Error during PowerShell script execution: {ex.Message}");
        }
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论