繁体   English   中英

如何使用C#运行远程计算机程序“以管理员身份运行”

[英]how to run a remote computer program 'run as administrator' using C#

我在远程计算机上有一个批处理文件。 通过右键单击该文件并选择选项“以管理员身份运行”,可以运行该批处理文件。 要以编程方式运行此批处理文件(位于远程计算机中),请使用C#ManagementScope类。 但是我找不到使用ManagementScope类设置“以管理员身份运行”选项的任何选项。

我需要代码解决方案(任何示例代码都会很出色)来执行该批处理文件。

您应该研究使用c#Process Class 它比使用WMI更快。

您可以像这样传递用户名和密码凭据,这是我之前创建的一个类:

class Logon : Process
{
    internal Logon(string filename, string username, string passwordtxt, string argument)
    {
        StartInfo.Domain = "Your-Domain";
        StartInfo.FileName = filename;
        StartInfo.UserName = username;
        StartInfo.Password = GetSecurePassword(passwordtxt);
        StartInfo.UseShellExecute = false;
        StartInfo.Arguments = argument;
        StartInfo.LoadUserProfile = true;
    }

    public System.Security.SecureString GetSecurePassword(string passwordtxt)
    {
        SecureString SS = new SecureString();
        foreach (char PSW in passwordtxt)
        {
            SS.AppendChar(PSW);
        }

        return SS;
    }
}

在您的应用中,您只需执行以下操作:

public void verifyuser(string filename, string argument)
{
    try
    {
        var logon = new SecureLogon(
        filename, txtuser.Text, txtpassword.Text, argument);

        logon.Start();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message,"Notification");
    }
}

您可以使用psexec来执行此操作。

Process.Start("psexec", "params");

您是否尝试过将其添加到“批处理”的开头?

runas /user:Administrator Example1Server.exe

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM