首页 > 解决方案 > 使用管理员凭据启动进程并使用 RunAs 执行程序文件 C 中的特定 exe

问题描述

我有这个代码完美运行并打开 CMD,但它不能运行特定的 exe,如 chrome。

$username = "username" 
$password = "password"

$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @("soft\abbas_104",(ConvertTo-SecureString -String "Abbas1122" -AsPlainText -Force))
Start-Process -PassThru -FilePath powershell -Credential $credentials -ArgumentList '-noprofile -command &{Start-Process ',  cmd, ' -Wait -verb runas}'

标签: powershell

解决方案


   Testabc user have the administrator right.

        //Run EXTERNAL APP AS AN ADMIN

        var pass = new SecureString();
         pass.AppendChar('t');
         pass.AppendChar('e');
         pass.AppendChar('s');
         pass.AppendChar('t');
         

        var ps1File = @"C:\Users\testabc\Desktop\LT_Admin.ps1";
        ProcessStartInfo processAdmin;
        processAdmin = new ProcessStartInfo();
        processAdmin.UseShellExecute = false;
        processAdmin.CreateNoWindow = true;
        processAdmin.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
        processAdmin.Password = pass;
        processAdmin.UserName = "testabc";
        processAdmin.Domain = "soft";
        processAdmin.FileName = @"C:\windows\system32\windowspowershell\v1.0\powershell.exe";
        processAdmin.Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{ps1File}\"";
        processAdmin.RedirectStandardOutput = true;           
        Process.Start(processAdmin);

在 ps1File 我有这个代码

Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -Verb RunAs

完美运行...


推荐阅读