【问题标题】:Run Active Directory shell commands using C#使用 C# 运行 Active Directory shell 命令
【发布时间】:2012-09-17 20:48:47
【问题描述】:

我正在编写一个模块,它将执行与 Active Directory 相关的任何类型的 shell 命令以及特定域控制器上的其他 shell 命令。

某些命令可以正常工作,但某些命令不能正常工作。

这里是代码

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 正在执行但没有返回任何结果。其他提到的给出错误的命令不被识别为内部或外部命令。如果我直接从控制台执行命令,它的工作正常。

这里我在域管理员帐户的上下文中调用一个进程,这样我就不会遇到任何权限问题。

请提出建议。

【问题讨论】:

  • 为什么要执行cmd?不能直接调用你的命令吗?
  • 因为我必须为所有命令集创建通用模块。如何直接调用命令?
  • 我的意思是不要在你的 Process 对象中调用 cmd /c "dcdiag",而是调用 dcdiag
  • 这将给出错误系统找不到文件。我必须在一个特定的域控制器上运行所有这些命令,而不是在本地机器上。所以我认为使用 cmd 执行将是最好的方法。
  • Process 类启动进程就像您使用开始菜单中的“运行”命令启动它们一样。我不确定使用cmd 会有什么帮助。我不认为你会在“运行”窗口中输入“cmd /c whatever”来执行“whatever”。 Process.Start("repadmin", "/showrepl") 之类的东西足以用指定的参数启动这个程序。要在另一台机器上远程启动进程,您不能使用Process 类,但请查看this question

标签: c# shell scripting active-directory cmd


【解决方案1】:

可能由于 UseShellExecute = false,找不到应用程序位置。使用完整路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2013-01-31
    相关资源
    最近更新 更多