Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys,

I am writing a module which will be executing any kind of shell commands related to active directory and other shell commands on a particular domain controller

Some of command are working but some of commands are not working properly. 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);

repadmin /showrepl

dcdiag

dcdiag /s:

command nltest executing but not returning any result. Where the other mentioned commands giving error is not recognized as internal or external command. Where if I execute command directly from console its working fine.

Here I am invoking a process under the context of domain administrator account so that I will not be have any permission issues.

Please suggest.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900