【问题标题】:Create Exchange Mailboxes on Remote Exchange 2010 Server in C#使用 C# 在远程 Exchange 2010 服务器上创建 Exchange 邮箱
【发布时间】:2012-02-23 16:59:09
【问题描述】:

标题几乎说明了一切。我有一个未在 Exchange Server 上运行的 C# 应用程序。我需要能够创建邮箱。我尝试使用this 教程,但它需要PowerShell IIS 虚拟目录:

  1. 不需要 SSL
  2. 允许基本身份验证

哪些是我们不能做的。这导致我有两种可能的解决方案。有没有办法将上面列出的教程修改为不需要这两个限制,或者有没有办法完全不使用 power shell?

这里是代码,以防你不想去链接:

using System; using System.Security; using System.Management.Automation; using System.Management.Automation.Runspaces; namespace PowerShellTest { class Program { static void Main(string[] args) { // Prepare the credentials that will be used when connecting // to the server. More info on the user to use on the notes // below this code snippet. string runasUsername = @"username"; string runasPassword = "password"; SecureString ssRunasPassword = new SecureString(); foreach (char x in runasPassword) ssRunasPassword.AppendChar(x); PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword); // Prepare the connection var connInfo = new WSManConnectionInfo( new Uri("http://ServersIpAddress/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials); connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; // Create the runspace where the command will be executed var runspace = RunspaceFactory.CreateRunspace(connInfo); // generate the command parameters var testNumber = 18; var firstName = "Test"; var lastName = "User" + testNumber; var username = "tuser" + testNumber; var domainName = "pedro.test.local"; var password = "ActiveDirectoryPassword1234"; var ssPassword = new SecureString(); foreach (char c in password) ssPassword.AppendChar(c); // create the PowerShell command var command = new Command("New-Mailbox"); command.Parameters.Add("Name", firstName + " " + lastName); command.Parameters.Add("Alias", username); command.Parameters.Add( "UserPrincipalName", username + "@" + domainName); command.Parameters.Add("SamAccountName", username); command.Parameters.Add("FirstName", firstName); command.Parameters.Add("LastName", lastName); command.Parameters.Add("Password", ssPassword); command.Parameters.Add("ResetPasswordOnNextLogon", false); command.Parameters.Add( "OrganizationalUnit", "NeumontStudents"); // Add the command to the runspace's pipeline runspace.Open(); var pipeline = runspace.CreatePipeline(); pipeline.Commands.Add(command); // Execute the command var results = pipeline.Invoke(); runspace.Dispose(); if (results.Count > 0) Console.WriteLine("SUCCESS"); else Console.WriteLine("FAIL"); } } }

【问题讨论】:

    标签: c# powershell exchange-server exchange-server-2010


    【解决方案1】:

    您可以使用许多不同的AuthenticationMechanism 设置所谓的运行空间

    访问 MSDN 站点以获取代码示例:

    • 基本身份验证(在您的示例中使用)
    • 证书认证
    • Kerberos 身份验证
    • 协商的身份验证

    http://msdn.microsoft.com/en-us/library/ff326159(v=exchg.140).aspx

    无论如何,现在还没有必要放弃 PowerShell。

    【讨论】:

      【解决方案2】:

      该博客文章中的代码有点损坏。 Pipeline 类是创建命令的一种过于复杂的方式,其编写方式涉及创建一对运行空间(一个本地,一个远程),而不仅仅是远程的。

      另外,IIS 中的“Basic”和“http”并不意味着“PowerShell 中没有安全性和没有加密”。默认情况下,通过 WinRM 层发送的所有内容都是加密的。

      来自 Exchange 团队的This Link 很好地介绍了在 C# 中执行此操作的正确方法。

      所以:

      • 您不必担心 IIS“基本”,因为还有另一层安全性。
      • 如果您使用 Exchange 团队的 C#,您可以将代码减半并加快速度

      还要 100% 透明:

      您无法远程管理 Exchange 2010,除非通过 PowerShell。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2012-03-29
        • 2013-11-09
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多