I have developed a mvc4 application that executes a PowerShell command from WebAPI and gets specific service information from number of remote computers.
Everything works fine on my dev machine but now i deployed it to IIS and it gives me weird results.
If i pass array of computers to the CmdLet, it gives back result for only one computer but if i execute the same CmdLet individually for each computer, it works fine.
Am i missing something here?
This is how my code looks like:
string[] serverNames = new string[] {"server1","server2","server3","server4","server5" };
CommandParameter paramServiceName = new CommandParameter("Name", "SomeService");
CommandParameter paramComputerName = new CommandParameter("ComputerName", computerNames);
Collection<PSObject> psObjects = ExecutePowerShellCommand("Get-Service", new CommandParameter[] { paramServiceName, paramComputerName });
foreach (PSObject psObject in psObjects)
{
// Do Something
}
private Collection<PSObject> ExecutePowerShellCommand(string commandName, CommandParameter[] parameters)
{
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();
Command psCommand = new Command(commandName);
foreach (var cmdParameter in parameters)
{
psCommand.Parameters.Add(cmdParameter);
}
pipeline.Commands.Add(psCommand);
Collection<PSObject> output = pipeline.Invoke();
return output;
}