0

I need to run this as an administrator

string cname = Environment.UserDomainName + "\\" + Environment.UserName;
string resetTrust = "netdom /resetpwd /s:server01 /ud:" + cname + "/pd:" + textBoxPassword.Text.Trim();
String output = ExecuteCommandAsAdmin(resetTrust);
MessageBox.Show(output);

Here is the method ExecuteCommandAsAdmin

public static string ExecuteCommandAsAdmin(string command)
{
    string cname = Environment.UserDomainName + "\\" + Environment.UserName;

    ProcessStartInfo procStartInfo = new ProcessStartInfo()
    {
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true,
        FileName = "runas.exe",
        Arguments = "/user:" + cname + "\"cmd /K " + command + "\""
    };

    using (Process proc = new Process())
    {
        proc.StartInfo = procStartInfo;
        proc.Start();

        string output = proc.StandardOutput.ReadToEnd();

        if (string.IsNullOrEmpty(output))
            output = proc.StandardError.ReadToEnd();

        return output;
    }
}

If you know a better way to do this, please let me know. I am not storing the password. Just passing it so it can be executed. If the wrong person enters the wrong password, nothing happens since they aren't an Admin.

Thank You

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
software is fun
  • 7,286
  • 18
  • 71
  • 129
  • i don't see a problem... you aren't hard coding anything. this probably isn't a `legit` SO question as you don't have a coding question. You are asking for more opinion. – T McKeown Apr 17 '14 at 15:37
  • 2
    possible duplicate of [How to start a Process as administrator mode in C#](http://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp) – Jon B Apr 17 '14 at 15:38
  • Define what you mean by running as administrator. – David Heffernan Apr 17 '14 at 15:42
  • Administrator is a user or a group that contains users in it. Not all commands execute with a "Standard" account. – software is fun Apr 17 '14 at 15:59
  • So which is it to be? You have to pick a user. The built in `Administrator`? Anyway, I think you are doing it wrong. I think you need to use either the `runas` verb to elevate, or use a manifest to force elevation at startup, or shell out to an executable that has that manifest. Why did you choose to try to shell out to `runas.exe`? – David Heffernan Apr 17 '14 at 16:00
  • did you see my code above? it's not working as expected. The output should be "This command as completed successfully" but I get a "" – software is fun Apr 17 '14 at 16:05
  • Yes I did see the code above. But you did not explain what you were trying to do, nor did you explain how the code failed to meet your expectations. Do you want to let me help you solve the problem, or would you rather I lent my help to somebody else? – David Heffernan Apr 17 '14 at 16:08
  • I am trying to reset the trust relationship between a workstation and the server. – software is fun Apr 17 '14 at 16:17
  • Are you trying to elevate, or run the process as a specific user? – David Heffernan Apr 17 '14 at 16:39
  • The user is the person that is currently logged in. I am not checking if the user is an admin. It won't work if they aren't an admin since group policies prevent runas for stanard users. – software is fun Apr 17 '14 at 19:20
  • I think you aren't understanding. I am a sysadmin. I am also the developer. I am trying to run 1 command with elevated privs. I can accomplish the same task using Command Prompt (Run As Administrator) – software is fun Apr 21 '14 at 12:17

1 Answers1

0

Use runas /user:name@domain cmd

Matthew R.
  • 4,332
  • 1
  • 24
  • 39
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177