Previously
In our previous post we created a working command-line tool, but it didn't really do anything.
The only thing we proved was that you could use arguments to choose the command you want to execute.
How to logon
Of course we want to make changes to an ADAM installation, why else would we create a command-line tool.
But to make changes, you need to be logged on.
Command-line tools don't have logon forms or controls, so we have to make due with arguments.
Reading values in arguments
In our previous post, we created a method called ExecuteWhoAmI and it had two parameters:
- command: the name of the command that was called
- args: the arguments that were passed to the command-line
The nice thing is that the args parameter is no longer an array of strings, but an ADAM object.
This allows you to ask Arguments the value of some named argument.
And because of that, we only need the code below to create an Application instance and logon.
You can see how easy it is to retrieve the user name, password, ... from arguments.
There is no longer a need to write code to evaluate all argument strings and determine which one you wish to retrieve.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private const string _RegistrationArgumentName = "registration";
private const string _UserNameArgumentName = "username";
private const string _PasswordArgumentName = "password";
private static Application LogonToAdam(Arguments arguments)
{
// Get registration name from the arguments (default is null)
string registration = arguments.GetVariableValue(_RegistrationArgumentName, null);
// Get username and password from the arguments (default is null)
string userName = arguments.GetVariableValue(_UserNameArgumentName, null);
string password = arguments.GetVariableValue(_PasswordArgumentName, null);
IAuthenticationProvider authenticationProvider = null;
if (userName == null)
{
// if no username was provided, we use windows integrated security
authenticationProvider = new ActiveDirectoryAuthenticationProvider();
}
Application application = new Application();
LogOnStatus logOnStatus = application.LogOn(registration, userName, password, false, null, authenticationProvider);
if (logOnStatus != LogOnStatus.LoggedOn)
{
throw ExceptionManager.CreateInvalidOperationException("Unable to logon.", false);
}
return application;
}
|
Now replace your ExecuteWhoAmI method by the one below and build:
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private static void ExecuteWhoAmI(string command, Arguments args)
{
Console.WriteLine("");
LogManager.Write(LogSeverity.Normal, "Installing...");
Application application = LogonToAdam(args);
Console.WriteLine("Your username is: {0}", application.UserName);
Console.WriteLine("Your culture is: {0}", application.Culture);
Console.WriteLine("Your language is: {0}", application.LanguageName);
Console.WriteLine("Your UI language is: {0}", application.LanguageNameForUI);
Thread.Sleep(1000);
LogManager.Write(LogSeverity.Normal, "Finished install");
Console.WriteLine("");
}
|
Go to your command prompt and execute your command-line tool using the following statement:
Adam.Blog.CommandLine.exe WhoAmI -registration=<your registration> -username=<your username> -password=<your password>
-ask
Try the statement below at the command prompt:
Adam.Blog.CommandLine.exe WhoAmI -ask
Isn't the ease of use of such an argument amazing?
I know I have used it more then once...
What's next
We still have some nice features up our sleave.
In our next post about command-line tools, we will show you how to display your help text with indentation and word wrap without having to write all that code yourself.
Sample Code
The article contains sample code project(s).
You must be logged in to view or download sample code.
Sign in now