C# - 通过Powershell Runspace删除Exchange电子邮件地址

Mat*_*att 0 c# powershell exchangewebservices exchange-server-2010

Per Technet -使用PowerShell控制台添加或删除邮箱的电子邮件地址,以下成功删除邮箱中的电子邮件别名:

Set-Mailbox "GenMgr" -EmailAddresses @{remove="GenMgr@domain.com"}
Run Code Online (Sandbox Code Playgroud)

但是,通过远程运行空间调用下面的PSCommand对象会引发System.Management.Automation.RemoteException错误.

command.AddCommand("Set-Mailbox");
command.AddParameter("Identity", "GenMgr");
command.AddParameter("EmailAddresses", "@{remove='GenMgr@domain.com'}");
powershell.Commands = command;
powershell.Invoke();
Run Code Online (Sandbox Code Playgroud)

System.Management.Automation.RemoteException:无法处理参数'EmailAddresses'上的参数转换.无法将值"@ {remove='GenMgr@domain.com'}"转换为"Microsoft.Exchange.Data.ProxyAddressCollection".错误:"地址'@ {remove='GenMgr@domain.com'}'无效:"@ {remove='GenMgr@domain.com'}"不是有效的SMTP地址.域名不能包含空格,它必须有前缀和后缀,例如example.com."

在我看来,问题在于EmailAddresses参数中的'remove'指令.

如何在Windows 8上使用C#和PowerShell远程运行空间来使Exchange 2010删除电子邮件别名?

Mit*_*tch 7

Powershell使用@{ key = value }语法创建哈希表.不是传递字符串,而是remove使用值为的单个元素传递哈希表email@address.com.

请参阅相关问题将散列表从C#传递到powershell以获取更多信息.

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();
Run Code Online (Sandbox Code Playgroud)