c# - Exchange 和 PowerShell

标签 c# powershell exchange-server-2010

我目前正在开发 Exchange 连接器并使用 C# 中的 PowerShell 脚本,如下所示:

public void Connect(string exchangeFqdn_, PSCredential credential_)
{
    var wsConnectionInfo = new WSManConnectionInfo(new Uri("http://" + exchangeFqdn_ + "/powershell"), 
                                            "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential_);

    wsConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

    Runspace = RunspaceFactory.CreateRunspace(wsConnectionInfo);
    Runspace.Open();
}

然后,我使用 Powershell 对象执行我的脚本:

public override List<PSObject> ExecuteCommand(Command command_)
{
    List<PSObject> toreturn;
    PowerShell powershell = null;
    try
    {
        powershell = PowerShell.Create();
        powershell.Commands.AddCommand(command_);
        powershell.Runspace = Runspace;
        toreturn = new List<PSObject>(powershell.Invoke());
    }
    finally
    {
        if (powershell != null) 
            powershell.Dispose();
    }
    return toreturn;
}

我可以用这个命令添加一个邮箱:

Command command = new Command("New-Mailbox");
command.Parameters.Add("Name", name_);
command.Parameters.Add("OrganizationalUnit", ou_);
command.Parameters.Add("UserPrincipalName", upn_);
command.Parameters.Add("FirstName", firstname_);
command.Parameters.Add("Initials", initials_);
command.Parameters.Add("LastName", lastname_);
command.Parameters.Add("ResetPasswordOnNextLogon", false);
command.Parameters.Add("Password", secureString_);

但是当我尝试删除这个邮箱(或另一个邮箱)时我遇到了一个问题:

Command command = new Command("Remove-Mailbox");
command.Parameters.Add("Identity", identity_);
command.Parameters.Add("Permanent", true);

System.Management.Automation.RemoteException: Cannot invoke this function because the current host does not implement it.

我不明白。为什么我可以添加用户,但不能删除它? 我错过了什么吗?

最佳答案

我找到了答案,多亏了这个 topic .

我这样更改了 Command 对象:

        Command command = new Command("Remove-Mailbox");
        command.Parameters.Add("Identity", identity_);
        command.Parameters.Add("Permanent", true);
        command.Parameters.Add("Confirm", false);

它就像一个魅力。 谢谢 !

希望对大家有帮助!

关于c# - Exchange 和 PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099853/

相关文章:

c# - 将应用构建为 Release 时更改 Log4Net 根级别

javascript - 在后面的代码中获取 Facebook 登录详细信息

PowerShell 输出不是预期的

regex - 使用正则表达式提取(重复)包含括号的组

c# - 远程检查交换凭据并检查用户登录

c# - 从 Exchange 读取联系人备注字段

c# - 如果我将数组的大小初始化为 0 会发生什么?

c# - FromBluetoothAddressAsync 永远不会在 WPF 应用程序中的 Windows 10 Creators Update 上返回

python - 执行绝对路径包含空格的powershell文件

powershell - 如何将复杂的 Powershell 数组序列化到磁盘以供第二个脚本处理?