Matt
Matt

Reputation: 630

C# - Remove Exchange Email Address via Powershell Runspace

Per Technet - Add or Remove Email Addresses for a Mailbox, using the PowerShell console, the following successfully removes an email alias from a mailbox :

Set-Mailbox "GenMgr" -EmailAddresses @{remove="[email protected]"}

However, invoking the PSCommand object below via remote runspace throws a System.Management.Automation.RemoteException error.

command.AddCommand("Set-Mailbox");
command.AddParameter("Identity", "GenMgr");
command.AddParameter("EmailAddresses", "@{remove='[email protected]'}");
powershell.Commands = command;
powershell.Invoke();

System.Management.Automation.RemoteException: Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value "@{remove='[email protected]'}" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error: "The address '@{remove='[email protected]'}' is invalid: "@{remove='[email protected]'}" isn't a valid SMTP address. The domain name can't contain spaces and it has to have a prefix and a suffix, such as example.com."

It seems to me the problem is with the 'remove' instruction in the EmailAddresses parameter.

How do I use C# and the PowerShell remote runspace on Windows 8 to get Exchange 2010 to remove an email alias?

Upvotes: 0

Views: 3295

Answers (1)

Mitch
Mitch

Reputation: 22251

Powershell uses the @{ key = value } syntax to create a hashtable. Instead of passing a string, pass the hashtable with a single remove element with the value of [email protected].

See the related question Passing a hashtable from C# to powershell for more.

command.AddCommand("Set-Mailbox");
command.AddParameter("Identity", "GenMgr");

var addresses = new Hashtable();
addresses.Add("remove", "[email protected]");
command.AddParameter("EmailAddresses", adresses);

powershell.Commands = command;
powershell.Invoke();

Upvotes: 7

Related Questions