启用exchange 2010邮箱

本文关键字:邮箱 2010 exchange 启用 | 更新日期: 2023-09-27 18:16:43

我正在尝试从c#代码创建/启用exchange 2010服务器上的邮箱。我看到到处都有人在使用下面所示的代码。

但是我得到以下错误:

术语"启用邮箱"未被识别为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;
        }

启用exchange 2010邮箱

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

Set-User –identity username –RemotePowershellEnabled $True

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

如果您正在使用负载均衡器和/或有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;
    } 
}