2

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;
}
5
  • If you just want to get service information, what is the benefits of encapsulating PowerShell versus a WMI call or a System.Component.ServiceCOntroler instance? Commented Jun 3, 2013 at 21:51
  • i am doing executing other custom powershell scripts and want to make it consistent across all the calls. Commented Jun 3, 2013 at 21:56
  • what does "weird results" mean? Errors? If so, can you post them? If it's running in the context of IIS, have you confirmed the AppPool account has the correct permissions? Commented Jun 12, 2013 at 18:39
  • @NickNieslanik : If you read the question, you will see that the AppPool is set up correctly because when querying for computer at a time works fine. There are no errors. Again, please read the question. Commented Jun 12, 2013 at 18:55
  • I'm not sure it was necessary to be snarky - I made a mistake, my bad Commented Jun 12, 2013 at 19:04

1 Answer 1

2

This is because of a bug in the Get-Service cmdlt in PowerShell 2.0 where it doesn't accept a string array for the ComputerName parameter (despite what the help file says). This is fixed in PowerShell 3.0.

To demonstrate, run the following in PowerShell 2.0:

$serverName = "Server1", "Server2", "Server3"
Get-Service -ComputerName $serverName -Name "wuauserv"  | format-table -property MachineName, Status, Name, DisplayName -auto

This returns the following:

MachineName    Status Name     DisplayName
-----------    ------ ----     -----------
Server1       Running wuauserv Windows Update

Running the same code in PowerShell 3.0 gives you your expected results:

MachineName    Status Name     DisplayName
-----------    ------ ----     -----------
Server1       Running wuauserv Windows Update
Server2       Running wuauserv Windows Update
Server3       Running wuauserv Windows Update

The workaround to running this in PowerShell 2.0 is to use the pipeline and a ForEach to enumerate your server names properly:

$serverName = "Server1", "Server2", "Server3"
$serverName | ForEach {Get-Service -ComputerName $_ -Name "wuauserv"} | format-table -property MachineName, Status, Name, DisplayName -auto

Which gives you your expected results:

MachineName    Status Name     DisplayName
-----------    ------ ----     -----------
Server1       Running wuauserv Windows Update
Server2       Running wuauserv Windows Update
Server3       Running wuauserv Windows Update
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.