0

I have a C# script to mail-enable a distribution group in Exchange. I have tried the following code, which works currently:

Command command = new Command("Enable-DistributionGroup -Identity '#Test DL'");
psInstance.Commands.AddCommand(command);

The above code works, and will return a string with the name of the distribution group. Upon checking the distribution group, it has become mail-enabled.

The below code does not work, and returns an empty string:

Command command = new Command("Enable-DistributionGroup").AddParameter("Identity", "#Test DL");
psInstance.Commands.AddCommand(command);

Am I using the AddParameter function incorrectly?

Saintwolf
  • 699
  • 1
  • 6
  • 19

1 Answers1

2

You need to create the command object, and then do a command.AddParameter separately. See the C# example at PowerShell.AddParameter Method

You can also reference Usage of powerShell.AddCommand for an example from another SO poster.

Community
  • 1
  • 1
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
  • I have also tried the following but to no avail: `Command command = new Command("Enable-DistributionGroup");` `command.addParameter("Identity", "#Test DL");` `psInstance.Commands.AddCommand(command);` – Saintwolf May 27 '15 at 02:00