简体   繁体   English

启用Exchange 2010邮箱

[英]Enable exchange 2010 mailbox

I'm trying to create/enable a mailbox on an exchange 2010 server from C# code. 我正在尝试从C#代码在Exchange 2010服务器上创建/启用邮箱。 Everywhere I look I see people using the code shown below. 我到处看到的人都在使用下面显示的代码。

However I get the following error: 但是我收到以下错误:

The term 'Enable-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. 术语“启用邮箱”不被视为cmdlet,函数,脚本文件或可操作程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。

What am I doing wrong? 我究竟做错了什么?

        SecureString password = new SecureString();

        string str_password = "myPassword";
        string username = "myUsername";

        //FQDN is ofcourse the (fully qualified) name of our exchange server..
        string liveIdconnectionUri = "http://FQDN/Powershell?serializationLevel=Full";

        foreach (char x in str_password)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(username, password);

        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "domainname.ltd/OUName/TestAcc Jap");
        command.AddParameter("Alias", "TestAccJap");
        command.AddParameter("Database", "DB-Name");

        powershell.Commands = command;

        try
        {
            runspace.Open();
            powershell.Runspace = runspace;
            powershell.Invoke();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            runspace.Dispose();
            runspace = null;
            powershell.Dispose();
            powershell = null;
        }

This is probably a powershell related error. 这可能是与Powershell相关的错误。 If you are running the code from a remote machine (not the exchange server), you have to enable remote powershell access for the user in question and make sure the firewall(s) allows connections to the exchange server on port 80. On the exchange server: 如果从远程计算机(而不是交换服务器)运行代码,则必须为有问题的用户启用远程Powershell访问,并确保防火墙允许在端口80上连接到交换服务器。服务器:

Set-User –identity username –RemotePowershellEnabled $True

The user also has to be a member of an exchange management role allowing mailbox creation. 用户还必须是允许邮箱创建的交换管理角色的成员。

If you are using a load balanser and/or have a DAG you may have to set up an Alternate Service Account to enable Kerberos authentication. 如果使用负载均衡器和/或具有DAG,则可能必须设置备用服务帐户才能启用Kerberos身份验证。 See http://technet.microsoft.com/en-us/library/ff808313.aspx for details. 有关详细信息,请参见http://technet.microsoft.com/zh-cn/library/ff808313.aspx I had to enable this to make the code run in my environment. 我必须启用它才能使代码在我的环境中运行。 I modified the code a bit to just test if I was able to run exchange powershell commands. 我对代码进行了一些修改,以测试是否能够运行exchange powershell命令。 The following code responds with the full name of the USERIDENT user if successful. 如果成功,以下代码将以USERIDENT用户的全名响应。

static void Main(string[] args)
{
    SecureString password = new SecureString();
    string str_password = "PASS";
    string username = "domain\\user";

    //FQDN is ofcourse the (fully qualified) name of our exchange server.. 
    string liveIdconnectionUri = "http://SERVERFQDN/Powershell?serializationLevel=Full";
    foreach (char x in str_password)
    {
        password.AppendChar(x);
    }
    PSCredential credential = new PSCredential(username, password);
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
    Runspace runspace = null;
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    command.AddCommand("Get-Mailbox");
    command.AddParameter("Identity", "USERIDENT");
    powershell.Commands = command;
    try
    {
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
        runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
        runspace.Open();
        powershell.Runspace = runspace;
        Collection<PSObject> commandResults = powershell.Invoke<PSObject>();
        foreach (PSObject result in commandResults)
        {
            Console.WriteLine(result.ToString());
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        runspace.Dispose();
        runspace = null;
        powershell.Dispose();
        powershell = null;
    } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM