简体   繁体   English

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

[英]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 : 通过Technet-使用PowerShell控制台添加或删除邮箱的电子邮件地址 ,以下操作成功从邮箱中删除了电子邮件别名:

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

However, invoking the PSCommand object below via remote runspace throws a System.Management.Automation.RemoteException error. 但是,通过远程运行空间调用下面的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: Cannot process argument transformation on parameter 'EmailAddresses'. System.Management.Automation.RemoteException:无法处理参数“ EmailAddresses”上的参数转换。 Cannot convert value "@{remove='GenMgr@domain.com'}" to type "Microsoft.Exchange.Data.ProxyAddressCollection". 无法将值“ @ {remove='GenMgr@domain.com'}”转换为“ 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." 错误:“地址'@ {remove='GenMgr@domain.com'}'无效:“ @ {remove='GenMgr@domain.com'}”不是有效的SMTP地址。域名不能包含空格,并且必须具有前缀和后缀,例如example.com。”

It seems to me the problem is with the 'remove' instruction in the EmailAddresses parameter. 在我看来,问题出在EmailAddresses参数中的“ remove”指令上。

How do I use C# and the PowerShell remote runspace on Windows 8 to get Exchange 2010 to remove an email alias? 如何在Windows 8上使用C#和PowerShell远程运行空间来使Exchange 2010删除电子邮件别名?

Powershell uses the @{ key = value } syntax to create a hashtable . Powershell使用@{ key = value }语法创建哈希表 Instead of passing a string, pass the hashtable with a single remove element with the value of email@address.com . 而不是传递字符串,而是将哈希表传递给单个remove元素,其值为email@address.com

See the related question Passing a hashtable from C# to powershell for more. 有关更多信息,请参见相关问题: 将哈希表从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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM