【问题标题】:how to run a remote computer program 'run as administrator' using C#如何使用 C# 运行远程计算机程序“以管理员身份运行”
【发布时间】:2012-12-11 13:06:07
【问题描述】:

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

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

【问题讨论】:

    标签: c# remote-server


    【解决方案1】:

    您应该考虑使用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");
        }
    }
    

    【讨论】:

    • 感谢德里克的回复。由于需要运行远程批处理文件,在哪里可以设置远程机器名或IP
    • 你需要使用 PSEXEC 服务伙伴,谷歌一下。它允许你在远程机器上运行 dos 命令。
    【解决方案2】:

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

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

    【讨论】:

    • 感谢巴厘岛的回复。通过使用此代码,我们收到“拒绝访问”错误。任何有关此的进一步信息将不胜感激
    【解决方案3】:

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

    runas /user:Administrator Example1Server.exe
    

    【讨论】:

      最近更新 更多