C# - Remove Exchange Email Address via Powershell Runspace

人盡茶涼 提交于 2019-12-25 02:26:19

问题


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="GenMgr@domain.com"}

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='GenMgr@domain.com'}");
powershell.Commands = command;
powershell.Invoke();

System.Management.Automation.RemoteException: Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value "@{remove='GenMgr@domain.com'}" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error: "The address '@{remove='GenMgr@domain.com'}' is invalid: "@{remove='GenMgr@domain.com'}" 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?


回答1:


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@address.com.

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", "GenMgr@domain.com");
command.AddParameter("EmailAddresses", adresses);

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


来源:https://stackoverflow.com/questions/23121957/c-sharp-remove-exchange-email-address-via-powershell-runspace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!