Saintwolf
Saintwolf

Reputation: 719

Powershell Invoke() returns empty if I use AddParameter

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?

Upvotes: 0

Views: 1043

Answers (1)

Tony Hinkle
Tony Hinkle

Reputation: 4742

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.

Upvotes: 2

Related Questions