Enable RabbitMQ management plugin using C# - c#

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");

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();
}

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();

Related

How to call power shell script file by passing attributes in c#

How to call power shell script file by passing attributes in c#.
I'm using below code to call ps1 file by passing inputs but getting error near invoke.
error message:
System.Management.Automation.CommandNotFoundException: 'The term
'Get-Childitem C:\samplemm.ps1' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path
is correct and try again.'
namespace SCOMWebAPI.Services
{
public class MaintennceModeService
{
private static IEnumerable<PSObject> results;
internal static string post(MaintenanceMode value)
{
// create Powershell runspace
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command myCommand = new Command("Get-Childitem C:\\samplemm.ps1");
CommandParameter testParam = new CommandParameter("mgmtserver", "NodeName");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
// Execute PowerShell script
results = pipeline.Invoke();
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
}
}
When you type the command Get-ChildItem C:\\samplemm.ps1 into powershell, you are actually binding the text C:\\samplemm.ps1 to the default parameter Path.
The problem with your code is that you have included the first parameter as part of the command name. Simply separate it out.
Instead of
Command myCommand = new Command("Get-Childitem C:\\samplemm.ps1");
Separate out the parameter:
Command myCommand = new Command("Get-Childitem");
CommandParameter pathParameter = new CommandParameter("Path", "C:\\samplemm.ps1");
myCommand.Parameters.Add(pathParameter);

Why does PowerShell class not load a snapin

I have this code in which I load a snapin (from MS Dynamics NAV in this case):
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
using (var ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddScript("Add-PSSnapin Microsoft.Dynamics.Nav.Management")
.AddScript("Get-NAVServerInstance");
//This does not work. Says unknown cmdlet Get-NAVServerInstance
//ps.AddCommand("Add-PSSnapin").AddArgument("Microsoft.Dynamics.Nav.Management")
// .AddCommand("Get-NAVServerInstance");
var output = ps.Invoke();
}
}
This code works when I use the AddScript method as shown in the code.
But why does AddCommand method not work (see commented code)? Looks like the snapin is not loaded, because the error says that the Get-NAVServerInstance cmdlet is unknown.
How is this supposed to work?
I know I can create a runspace with an InitialSessionState on which I have imported the snapin. Then the ps.AddCommand("Get-NAVServerInstance") is working.
But when I want to create a remote runspace session (using WSManConnectionInfo) I can't find a way to supply an initialSessionState.
UPDATE:
So it seems that AddCommand only can be used for cmdlets available when the runspace is opened (or created?). Using an InitialSessionState or RunspaceConfiguration instance with RunspaceFactory.CreateRunspace(...) will do. So this code works:
var config = RunspaceConfiguration.Create();
PSSnapInException warning;
config.AddPSSnapIn("Microsoft.Dynamics.Nav.Management", out warning);
using (Runspace runspace = RunspaceFactory.CreateRunspace(config))
{
runspace.Open();
using (var ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddCommand("Get-NAVServerInstance");
var output = ps.Invoke();
}
}
But my problem is in that case, that I can't specify a WSManConnectionInfo instance.
So how can I create a runspace with a remote connection with a snapin (installed on the remote machine) loaded? How to supply a configuration for a remote connection?
I finally found a hint how to configure a remote session (see https://superuser.com/a/518567).
You need to register a session configuration on the remote computer with
Register-PSSessionConfiguration -Name MyShell -StartupScript 'MyInitScript.ps1'
Then you can set the shellUri parameter of WSManConnectionInfo to http://schemas.microsoft.com/powershell/MyShell
The runspace you create this way will have the commands available which are imported by the MyInitScript.ps1 startup script.
So now this code will work:
string shell = "http://schemas.microsoft.com/powershell/MyShell";
var target = new Uri("https://myserver:port/wsman");
var secured = new SecureString();
foreach (char letter in "password")
{
secured.AppendChar(letter);
}
secured.MakeReadOnly();
var credential = new PSCredential("username", secured);
var connectionInfo = new WSManConnectionInfo(target, shell, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (var ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddCommand("Get-NAVServerInstance");
var output = ps.Invoke();
}
}
Try invoking AddScript like so:
.AddScript("...", false)
this will execute the command in the global scope instead of a new local scope.
I think the proper way to do this is to use the RunspaceConfiguration class. It has an AddPSSnapin method.

passing parameters with c# to powershell

I am trying to pass a string from my c# app to my powershell script .
I keep geting an error:"A positional parameter cannot be found that accepts argument '$null'
what should I do?
my c# code:
public void PowerShell()
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
String scriptfile = #"c:\test.ps1";
Command myCommand = new Command(scriptfile, false);
CommandParameter testParam = new CommandParameter("username", "serverName");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke(); <---error- "A positional parameter cannot be found that accepts argument '$null'"
runspace.Close();
}
my powershell code:
Out-Host $username
Your PS script have no parameters, it simply uses variable. So, try this instead:
Param($username)
Write-Output $username
Please, notice that Out-Host is not acceptable in you case, because it tries to output param to calling scope, not simply write something to output stream.
Also, you can set variable in runspace, so it will be available in script without params:
runspace.SessionStateProxy.SetVariable("username", "SomeUser");
(it must be done after runspace.Open() and before pipeline.Invoke())

add-windowsfeature is not recognized

I'm trying to run powershell commands using C# but I keep getting errors when I invoke the pipeline. I was wondering if anyone know why I keep getting add-windowsfeature is not recognized. Thanks in advance.
private static void RunScript(string name)
{
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new[] { "ServerManager"});
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
// create Powershell runspace
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command cm = new Command("Import-module");
cm.Parameters.Add("name","ServerManager");
pipeline.Commands.Add(cm);
Command command = new Command("add-windowsfeature");
command.Parameters.Add(null, name);
pipeline.Commands.Add(command);
var a = pipeline.Invoke();
foreach (var psObject in a)
{
Console.WriteLine(psObject);
}
runspace.Close();
}
ServerManager is a 64-bit only module (it doesn't exist under C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules but will exist under C:\Windows\System32\WindowsPowerShell\v1.0\Modules). Compile as x64 and your code should work.

Assign variables to powershell using C#

I'am tring to call a powershell script and pass through a parameter. I would all so like to pass through a more than one parameter eventually
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
String scriptfile = "..\\..\\Resources\\new group.ps1";
Command myCommand = new Command(scriptfile, false);
CommandParameter testParam = new CommandParameter("test3");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
runspace.Close();
The problem seems to be ... well nothing happens. Is this how to correctly assign the varibles? Giving test powershell script
# creates group
net localgroup $username /Add
# makes folder
#mkdir $path
This line of code:
CommandParameter testParam = new CommandParameter("test3");
Creates a parameter named test3 that has a value of null. I suspect you want to create a named parameter e.g.:
CommandParameter testParam = new CommandParameter("username", "test3");
And you're script needs to be configured to accept parameters e.g.:
--- Contents of 'new group.ps1 ---
param([string]$Username)
...
The PowerShell script needs to be setup to accept parameters:
Try adding the following to the top of that script and re-testing:
param([string]$username)
Alternatively, you could add the folowing line to the top of your script:
$username = $args[0]
Good luck.

Categories

Resources