1

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

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

哪些是我们做不到的。这导致我有两种可能的解决方案。有没有办法修改上面列出的教程以不需要这两个限制,或者有没有办法在不使用电源外壳的情况下做到这一点?

这是代码,以防您不想访问链接:


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");

        }
    }
}


4

2 回答 2

1

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

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

Exchange 团队的这个链接很好地涵盖了在 C# 中执行此操作的正确方法。

所以:

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

还要 100% 透明:

您不能通过 PowerShell 远程管理 Exchange 2010。

希望这可以帮助

于 2012-02-23T20:44:18.240 回答
1

您可以设置一个所谓的运行空间,其中包含许多不同AuthenticationMechanism

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

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

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

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

于 2012-02-23T17:33:36.313 回答