【问题标题】:Enable exchange 2010 mailbox启用 Exchange 2010 邮箱
【发布时间】:2012-01-11 18:56:00
【问题描述】:

我正在尝试使用 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;
        }

【问题讨论】:

  • How do I programatically create an exchange 2010 mailbox using C# 的可能副本。请阅读已接受答案的第一段 - 这有帮助吗?
  • 当我尝试接受答案的代码时,我收到以下错误:没有为 Windows PowerShell 版本 2 注册管理单元。
  • 这就是你的答案。 :) Exchange 管理单元尚未注册 PowerShell,因此它无法识别 Enable-Mailbox。所以你的新问题(serverfault 更好的是“如何为 PowerShell 2 注册 Exchange 2010 管理单元?”。
  • 但它只是说当我从 C# 调用该函数时它尚未注册,当我尝试从 PS 启用邮箱时,我可以打开与 exch 的会话。服务器,然后成功调用 enable-mailbox 命令。当我尝试这个时:pedroliska.wordpress.com/2011/07/22/… 它显示所有命令都没有错误,但是当我将 'get-command' 替换为 'Enable-Mailbox' 和附加参数时,它没有给出错误,但它也不起作用。我认为这是因为链接中的代码我没有连接到交换服务器(?)

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


【解决方案1】:

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

Set-User –identity username –RemotePowershellEnabled $True

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

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

}

【讨论】:

  • 我已经用普通的 PowerShell 代码对其进行了测试,然后一切正常。我跑了 Set-User –identity username –RemotePowershellEnabled $True 你上面提到的只是为了确定,但是我在我的问题中显示的代码除了显示一个错误之外什么也没做: The term 'Enable-Mailbox' is not Recognized as the name一个cmdlet,functi....这是因为我猜没有加载管理单元??
  • 尝试使用 connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;而不是默认值。
  • 我进行了更改并遇到了一些新错误,我修复了大部分错误,但目前陷入以下错误。下一条评论中的下一部分。连接到远程服务器失败并显示以下错误消息:WinRM 客户端无法处理请求。服务器不支持客户端请求的身份验证机制,或者在服务配置中禁用了未加密的流量。验证服务配置中的未加密流量设置或指定服务器支持的身份验证机制之一。
  • 要使用 Kerberos,请将计算机名称指定为远程目标。还要验证客户端计算机和目标计算机是否已加入域。要使用 Basic,请将计算机名称指定为远程目标,指定 Basic 身份验证并提供用户名和密码。服务器报告的可能的身份验证机制:有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。
  • 我已经修改了上面的答案。根据您上面的 cmets,我认为您可能有 kerberos 身份验证问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 2013-11-09
  • 2015-01-23
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多