简体   繁体   中英

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

I have a batch file in a remote machine. This batch file is run by right clicking on the file and selecting option "Run as administrator". To run this batch file (which is located in remote machine) programmatically I use C# ManagementScope class. But i am not able to find any option to set 'run as administrator' option using ManagementScope class.

I need code solution (any sample code will be excellent) to execute that batch file.

You should look into using the c# Process Class . It's way quicker than using WMI.

You can pass Username & Password Credentials like this, it's a class I created sometime ago :

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;
    }
}

In your App you just have the following :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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