Running Active Directory Shell Commands Using C #

I am writing a module that will execute any shell commands related to Active Directory and other shell commands on a specific domain controller.

Some of the teams work, but some of the teams do not work correctly.

Here is the code

public static void ExecuteShellCommand(string _FileToExecute, string _CommandLine, ref string _outputMessage, ref string _errorMessage)
{
    System.Diagnostics.Process _Process = null;

    try
    {
       _Process = new System.Diagnostics.Process();

       string _CMDProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe", new object[] { Environment.SystemDirectory });

       string _Arguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", new object[] { _FileToExecute });

       _Arguments = string.Format(" /C \"{0}\"", _Arguments);
       Console.WriteLine("---aruguments quering : cmd.exe" + _Arguments);                
       System.Diagnostics.ProcessStartInfo _ProcessStartInfo = new System.Diagnostics.ProcessStartInfo(_CMDProcess, _Arguments);               
       _ProcessStartInfo.CreateNoWindow = true;                
       _ProcessStartInfo.UseShellExecute = false;                
       _ProcessStartInfo.RedirectStandardOutput = true;
       _ProcessStartInfo.RedirectStandardInput = true;
       _ProcessStartInfo.RedirectStandardError = true;
       _Process.StartInfo = _ProcessStartInfo;
       //_ProcessStartInfo.Domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain().Name;

       _Process.Start();

       _errorMessage = _Process.StandardError.ReadToEnd();
       _Process.WaitForExit();

       _outputMessage = _Process.StandardOutput.ReadToEnd();
       _Process.WaitForExit();
    }
    catch (Exception _Exception)
    {                
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }
    finally
    {
       _Process.Close();
       _Process.Dispose();
       _Process = null;
    }
}

CommandExecutionEngine.ExecuteShellCommand("nltest", "/logon_query /server:india.cobra.net", ref output, ref error);
Console.WriteLine("output for dir : " + output + " error : " + error);

Teams

repadmin /showrepl

dcdiag

dcdiag /s:<dcname

the nltest command executes, but returns no result. If the other mentioned commands giving an error are not recognized as an internal or external command. Where, if I execute the command directly from the console, it works fine.

Here I call the process in the context of the domain administrator account so that I do not have any permissions.

Please offer.

0
source share
1 answer

, UseShellExecute = false, . .

0

All Articles