-2

Sorry for not being so clear. Here is the explanation.

There is a command "admin setserver systempw " which is used to set the password. On clicking 'Enter' after typing that command in cmd, It will prompt for user input. We have to enter a string and hit 'Enter', which will set that string as password for the server mentioned in the command. Now I have to automate that execution with c# code. The screen should have 2 input text boxes and and a Button. The inputs are the server name and password. On clicking that button, It should execute the command mentioned on the top associating the server name and password entered as inputs to the command. Using the tutorials I could create a Process which will run the first command. But, I am unable to associate the password. How can I associate that password to the Prompt the command I have mentioned does.


C:/> admin setserver systempw 'clicking Enter'
Please enter password: Sai@45678 'Clicking Enter'

Password have been set successfully.

This is the piece of code I am trying to write.

            string servername = TextBox1.Text;

            ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c admin setserver systempw  " + servername );
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;

            procStartInfo.CreateNoWindow = true;

            procStartInfo.WorkingDirectory = @"C:/";

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

            string result = proc.StandardOutput.ReadToEnd();

            MessageBox.Show("Done! " + result);

How to associate the second text box value (password) to the process as a argument. How it is possible to link the password to the prompt it makes "Please enter password: ".

Please explain.

Te Jas
  • 45
  • 9
  • 4
    It would be awesome if you could provide a [mcve] of your attempt so far. – mjwills Jun 29 '18 at 12:51
  • 2
    1) It's not too clear what you're asking... 2) show us what you've done and why it's not working – GPW Jun 29 '18 at 12:52
  • As your question is not clear, are you looking for command line arguments? Command line arguments allow flexibility to take in whatever parameters when you run the app and then manage the data within your code. You can try the following as a solution if that is the case you are looking for. You can extend it quite far depending on what you need. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments – ian_stagib Jun 29 '18 at 13:36

2 Answers2

0

After thoroughly updating your post, I believe you could be after one of two things here:

  • How to execute different commands based on user input?
  • How to change a user's password on a server.

I will give a basic overview for each of these in hopes that you may become better educated, and hopefully will update your post to reflect your truly desired result.

The Amazing Switch

To execute multiple commands within your console application, look into the switch structure available in C#. This will allow you to execute different bits of code based on certain criteria such as user input. For example:

using System;
using static System.Console;

private static bool exit = false;
private static string serverName = string.Empty;
static void Main(string[] args) {
        WriteLine("Please enter a command.");
        string response = ReadLine();

        switch (response) {
            case "setserver": SetServer(); break;
            case "changepass": ChangePassword(); break;
        }
    }
    ReadKey();
}
static void SetServer() {
    WriteLine("Please enter a server name.");
    serverName = ReadLine(); // You should probably validate the user input here.
}
static void ChangePassword() {
    // Execute your needed password change code here.
}

This will get you started with executing multiple commands in a console application.

Changing a Password (Active Directory)

For Active Directory changes, you should take a look at this post and this documentation for more information. The code it utilizes (in case the link ever dies for some reason) is:

// Connect to Active Directory and get the DirectoryEntry object.
// Note, ADPath is an Active Directory path pointing to a user. You would have created this
// path by calling a GetUser() function, which searches AD for the specified user
// and returns its DirectoryEntry object or path. See http://www.primaryobjects.com/CMS/Article61.aspx
DirectoryEntry oDE;
oDE = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);

try {
   // Change the password.
   oDE.Invoke("ChangePassword", new object[]{strOldPassword, strNewPassword});
} 
catch (Exception e) {
   Debug.WriteLine($"Error changing password. Reason: {e.Message}");
}

Changing a Password (SQL Server)

For SQL Server changes, I would recommend you look into this post (where there are two decent answers to solving this issue) and this documentation on changing the password at the SqlConnection object level.

SqlConnection.ChangePassword(string, string);

Changes the SQL Server password for the user indicated in the connection string to the supplied new password.

The answers provided to the other post would combine this code:

string sqlquery = "SELECT Password FROM [Member] where Username=@username";
SqlCommand cmd = new SqlCommand(sqlquery, connect);
cmd.Parameters.AddWithValue("@username", label_username.Text);
cmd.Connection = connect; 
string currentPassword = (string)cmd.ExecuteScalar();

if (currentPassword == textBox_Current.Text) {
    // PASSWORD IS CORRECT, CHANGE IT, NOW.
} else {
    // WOW EASY BUDDY, NOT SO FAST
}

With this advice:

  • consider your string username -> Hash it -> write a query to check whether that hashed value and the user's password's hash value stored in the database is the same
  • consider string password and string newPassword -> check whether each is null or the length is 0
  • consider string password and string newPassword in your code -> Hash both -> check whether the hash values are the same

An Important Note

No matter what issue you are trying to solve, when dealing with passwords you should put heavy thought into the hashing and encrypting that comes along with passwords. If you're going to change a password on a server, you should also verify the old password prior to making changes. You should also validate the new password by checking for illegal characters, ensuring the password strength is adequate, verifying the initial entry of the new password matches a confirmation entry, among a few other things such as the hashing and encryption that may be surrounding the password you'll need to verify against.

Thorough Posting

If you need a more thorough answer to a more specific issue such as actually changing the password on a server you have permissions to, then update your question and be more thorough by following mjwills' advice and visiting the Minimal, Complete, and Verifiable example. This will allow other readers not only assist you in a better manner, but will also assist future readers with similar issues. Once you have updated your question to reflect the truly desired result, I will update my answer (if I have the answer) to demonstrate the knowledge you're seeking.

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
0

After much pondering, I think you are asking "How can a DOS style program get user input?"

Here are two methods, one from the prompt, and another from a popup.

Sub Main()
    Dim whut As String

    Console.WriteLine("I demand input!:")
    whut = Console.ReadLine() 'from dos prompt

    whut = InputBox("Say whut?") 'from popup window

    Console.WriteLine(whut)
End Sub

Hopefully from our guesses at what you want, you can piece something together.

Davesoft
  • 724
  • 1
  • 4
  • 10