2

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.

3 Answers 3

1

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");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Derek for your reply. As I need to run the remote batch file, where can I set the remote machine name or IP
You will need to use PSEXEC Service mate, Google it.. It allows you to run dos commands on remote machines.
0

You could use psexec to do this.

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

1 Comment

Thanks Bali for your reply. We are getting 'Access Denied' error by using this code. Any further information on this will be appreciated
0

Have you tried adding this to the beginning of your "batch"?

runas /user:Administrator Example1Server.exe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.