c# - 启用 exchange 2010 邮箱

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

最佳答案

这可能是与 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;
    } 

}

关于c# - 启用 exchange 2010 邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361520/

相关文章:

c# - 如何使用 EntityFramework 5 在导航属性上只深入一层?

c# - 从 .Net Framework 4.7 升级到 4.8 后使用 MSBuild 发布项目时出错

c# imap/pop3 邮件服务器

javascript - 呈现后的母版页控件 Id

javascript - CC 收件人的 Mandrill 合并变量不起作用

python - SMTPAuthenticationError : (535, b'5.7.8 Django 生产中不接受用户名和密码?

php - 无法使用 codeigniter 验证密码

C# 按对象搜索组合框的索引

c# - 为什么我们的项目需要接口(interface)层/抽象类?

c# - 图像未显示在 ASP.NET 上