使用C#运行Active Directory的shell命令命令、Active、Directory、shell

2023-09-11 22:28:00 作者:年少轻狂。

我在写这将是在执行任何相关的Active Directory和其他shell命令在一个特定的域控制器shell命令的模块。

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.

下面是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:<dcname

NLTEST命令执行,但没有返回任何结果。其中上述其他命令给错误没有被识别为内部或外部命令。在那里,如果我直接从控制台执行命令,其工作的罚款。

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.

请建议。

推荐答案

可能因为UseShellExecute =假,应用程序的位置没有被发现。使用完整路径。

Possibly since UseShellExecute = false, the application location is not being found. Use the full path.