C#-通过Powershell运行空间删除Exchange电子邮件地址 [英] C# - Remove Exchange Email Address via Powershell Runspace
问题描述
每个 Technet-添加或删除电子邮件地址对于邮箱,使用PowerShell控制台,以下操作成功从邮箱中删除了电子邮件别名:
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"}
但是,通过远程运行空间调用下面的PSCommand对象会引发System.Management.Automation.RemoteException错误.
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:无法处理参数'EmailAddresses'上的参数转换.无法将值"@ {remove='GenMgr@domain.com'}"转换为"Microsoft.Exchange.Data.ProxyAddressCollection".错误:地址'@ {remove='GenMgr@domain.com'}'无效:"@ {remove='GenMgr@domain.com'}"不是有效的SMTP地址.域名不能包含空格,并且必须具有前缀和后缀,例如example.com."
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."
在我看来,问题出在EmailAddresses参数中的'remove'指令上.
It seems to me the problem is with the 'remove' instruction in the EmailAddresses parameter.
如何在Windows 8上使用C#和PowerShell远程运行空间来使Exchange 2010删除电子邮件别名?
How do I use C# and the PowerShell remote runspace on Windows 8 to get Exchange 2010 to remove an email alias?
推荐答案
Powershell使用哈希表.而不是传递字符串,而是使用具有email@address.com
值的单个remove
元素传递哈希表.
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
.
请参阅相关问题将哈希表从C#传递到powershell ,以了解更多信息.
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();
这篇关于C#-通过Powershell运行空间删除Exchange电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!