【问题标题】:Exchange and PowerShellExchange 和 PowerShell
【发布时间】:2013-04-19 07:51:19
【问题描述】:

我目前正在开发一个 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:无法调用此函数,因为当前主机没有实现它。

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

【问题讨论】:

    标签: c# powershell exchange-server-2010


    【解决方案1】:

    感谢topic,我找到了答案。

    我像这样更改了Command 对象:

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

    它就像一个魅力。 谢谢 !

    我希望对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2011-06-10
      • 2015-07-04
      • 2015-06-11
      相关资源
      最近更新 更多