【问题标题】:C# - Remove Exchange Email Address via Powershell RunspaceC# - 通过 Powershell Runspace 删除 Exchange 电子邮件地址
【发布时间】:2014-06-01 01:33:09
【问题描述】:

根据Technet - Add or Remove Email Addresses for a Mailbox,使用 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 参数中的“删除”指令。

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

【问题讨论】:

    标签: c# powershell exchangewebservices exchange-server-2010


    【解决方案1】:

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

    查看相关问题Passing a hashtable from C# to 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();
    

    【讨论】:

    • 要删除多个别名,请使用字符串数组作为哈希表“删除”条目的值。你的例子很到位。
    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多