【问题标题】:How do I programatically create an exchange 2010 mailbox using C#如何使用 C# 以编程方式创建 Exchange 2010 邮箱
【发布时间】:2011-12-28 13:20:56
【问题描述】:

我的任务是编写一个程序来自动创建一个 2010 Exchange 邮箱。我的研究告诉我使用 powershell,但我似乎找不到要引用的命名空间,并且想要一些示例代码。我在网上找到了一些代码,但我不知道 PowerShell 的命名空间是什么。我认为它可能是 System.Management.Automation 但是当我尝试引用命名空间时,它在 dotnet 列表中不存在。我只有 System.Management 和 System.Management.Instrumentation。

任何帮助将不胜感激?

【问题讨论】:

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


    【解决方案1】:

    当我这样做时,我不得不单独下载 Powershell,但不确定是否仍然如此。你可以从here获得它。

    下面是创建邮箱的示例代码:

    SecureString password = new SecureString();
    string str_password = "pass";
    string username = "userr";
    
    string liveIdconnectionUri = "http://exchange.wenatex.com/Powershell?serializationLevel=Full";
    
    foreach (char x in str_password)
    {
        password.AppendChar(x);
    }
    
    PSCredential credential = new PSCredential(username, password);
    
    // Set the connection Info
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
    credential);
    
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
    
    // create a runspace on a remote path
    // the returned instance must be of type RemoteRunspace
    
    Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
    
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    
    command.AddCommand("Enable-Mailbox");
    command.AddParameter("Identity", usercommonname);
    command.AddParameter("Alias", userlogonname);
    command.AddParameter("Database", "MBX_SBG_01");
    
    powershell.Commands = command;
    try
    {
        // open the remote runspace
        runspace.Open();
        // associate the runspace with powershell
        powershell.Runspace = runspace;
        // invoke the powershell to obtain the results
        return = powershell.Invoke();
    }
    catch (Exception ex)
    {
    
        Console.WriteLine(ex.Message);
    }
    finally
    {
        // dispose the runspace and enable garbage collection
        runspace.Dispose();
        runspace = null;
        // Finally dispose the powershell and set all variables to null to free
        // up any resources.
        powershell.Dispose();
        powershell = null;
    }
    

    【讨论】:

    • PowerShell 的命名空间是:Microsoft.PowerShell
    • return = powershell.Invoke(); 无法编译,您是否遗漏了什么?
    【解决方案2】:

    这是一个老问题,但它可能对未来的访问者有所帮助......

    w69rdy 的回答对我不起作用。但我得到了它的工作并在这里写了博客http://pedroliska.wordpress.com/2011/07/22/running-exchange-management-shell-commands-powershell-with-c/

    【讨论】:

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