0

How could I convert the follow powershell command to C# code, especially parameters for -index.

Get-Mailbox | select-object -index 0, 1, 2, 3, 4, 5

I just want to retrieve the mail box many times to avoid extremely big memory usage.

How to set 0, 1, 2, 3, 4, 5 to CommandParameters?

ryo
  • 59
  • 7

1 Answers1

0

I'm not a programmer but this is should get you closer:

Command cmdMailbox = new Command("Get-Mailbox");
cmdMailbox.Parameters.Add("Identity", 'someone');

Command cmdSelect = new Command("Select-Object");
int[] indexes = new int[] {0,1,2,3,4,5}; 
cmdSelect.Parameters.Add("Index",indexes ); 
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Hi, Shay, thanks for your reply, i tried this one, but still error. Parameter set cannot be resolved using the specified named parameters. at System.Management.Automation.CmdletParameterBinderController.ThrowAmbiguousParameterSetException(UInt32 parameterSetFlags, MergedCommandParameterMetadata bindableParameters) – ryo Aug 12 '13 at 00:52
  • What command have you tried? Maybe you can use the AddScript method and execute PowerShell commands, $ps.AddScript('Get-Mailbox -ResultSize 10 | Select-Object -Index (0..4)') – Shay Levy Aug 12 '13 at 08:34