启用Exchange 2010邮箱

时间:2011-12-02 19:28:10

标签: c# asp.net email powershell exchange-server

我正在尝试使用C#代码在Exchange 2010服务器上创建/启用邮箱。 我看到的每个地方都看到人们使用下面显示的代码。

但是我收到以下错误:

术语“Enable-Mailbox”无法识别为cmdlet,函数,脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

我做错了什么?

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

1 个答案:

答案 0 :(得分:1)

这可能是与PowerShell相关的错误。如果从远程计算机(而不是交换服务器)运行代码,则必须为相关用户启用远程PowerShell访问,并确保防火墙允许在端口80上连接到Exchange服务器。在交换机上服务器:

Set-User –identity username –RemotePowershellEnabled $True

用户还必须是允许邮箱创建的Exchange管理角色的成员。

如果您使用的是负载均衡器和/或有DAG,则可能需要设置备用服务帐户才能启用Kerberos身份验证。有关详细信息,请参阅http://technet.microsoft.com/en-us/library/ff808313.aspx。我必须启用它以使代码在我的环境中运行。我修改了一些代码,只是测试我能否运行exchange powershell命令。如果成功,以下代码将使用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;
    } 

}