Enable RabbitMQ management plugin using C#

530 views Asked by At

I've been trying to enable RabbitMQ management plugin using C# code.

I was successfully able to install RabbitMQ server using c# by using following code.

        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();

        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

        Pipeline pipeline = runspace.CreatePipeline();

        Command myCommand = new Command("Start-Process");


        CommandParameter testParam = new CommandParameter("FilePath", @"C:\Users\saadp\Desktop\Dependencies\rabbitmq-server-3.8.3.exe");
        CommandParameter testParam2 = new CommandParameter("ArgumentList", new string[] { "/S" });
        CommandParameter testParam3 = new CommandParameter("Wait");

        myCommand.Parameters.Add(testParam);
        myCommand.Parameters.Add(testParam2);
        myCommand.Parameters.Add(testParam3);

        pipeline.Commands.Add(myCommand);

        var results = pipeline.Invoke();

But, When I try to enable RabbitMQ management plugin using following CommandParameters, It doesn't affect anything. What actually happens is after executing this code new Command Prompt opens and closes in a matter of fraction.

Here is the code which I've tried.

        CommandParameter testParam = new CommandParameter("FilePath", @"""C:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.3\sbin\rabbitmq-plugins.bat""");
        CommandParameter testParam2 = new CommandParameter("ArgumentList", new string[] { "'enable rabbitmq_management'" });
        CommandParameter testParam3 = new CommandParameter("Wait");
2

There are 2 answers

0
Sa'ad On BEST ANSWER

I got this working by following this code.

private static string RunScript(string scriptText)
    {
        // create Powershell runspace

        Runspace runspace = RunspaceFactory.CreateRunspace();

        // open it

        runspace.Open();

        // create a pipeline and feed it the script text

        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(scriptText);

        // add an extra command to transform the script
        // output objects into nicely formatted strings

        // remove this line to get the actual objects
        // that the script returns. For example, the script

        // "Get-Process" returns a collection
        // of System.Diagnostics.Process instances.

        pipeline.Commands.Add("Out-String");

        // execute the script

        pipeline.Invoke();

        // close the runspace

        runspace.Close();
    }
2
Arsalan Valoojerdi On

Try this:

        var startInfo =
            new ProcessStartInfo
            {
                FileName = @"C:\Windows\System32\cmd.exe",
                Arguments = "/c rabbitmq-plugins enable rabbitmq_management",
                WorkingDirectory = @"C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.11\sbin",
                WindowStyle = ProcessWindowStyle.Maximized
            };

        Process.Start(startInfo)?.WaitForExit();
        Process.Start("net", "stop RabbitMQ")?.WaitForExit();
        Process.Start("net", "start RabbitMQ")?.WaitForExit();