C#-通过Powershell运行空间删除Exchange电子邮件地址



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

Set-Mailbox "GenMgr" -EmailAddresses @{remove="GenMgr@domain.com"}

但是,通过远程运行空间调用下面的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();

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删除电子邮件别名?

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

有关更多信息,请参阅相关问题将哈希表从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();

最新更新