Вопрос

I'm currently developing a Exchange connector and using PowerShell scripts in C#, like this:

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();
}

Then, I execute my script using the Powershell object:

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;
}

I can add a mail box like with this command:

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_);

But I'm facing an issue when I try to remove this mailbox (or another one):

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.

I do not understand. Why can I add a user, but not delete it ? Am i missing something ?

Это было полезно?

Решение

I've found the answer, thanks to this topic.

I changed the Command object like that:

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

And it works like a charm. Thanks !

I hope that help someone !

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top