Как программно создать почтовый ящик Exchange 2010 с помощью C # - PullRequest
4 голосов
/ 03 августа 2010

Мне было дано задание написать программу для автоматического создания почтового ящика обмена 2010 года. Мое исследование говорит мне использовать powershell, но я не могу найти пространство имен для ссылки и хотел бы некоторый пример кода. Я нашел некоторый код в Интернете, но я не знаю, что такое пространство имен для PowerShell. Я думаю, что это может быть System.Management.Automation, но когда я пытаюсь сослаться на пространство имен, его нет в списке dotnet. Все, что у меня есть, это System.Management и System.Management.Instrumentation.

Любая помощь будет оценена?

Ответы [ 2 ]

4 голосов
/ 03 августа 2010

Когда я делал это, мне приходилось загружать Powershell отдельно, хотя я не был уверен, что это так. Вы можете получить его от здесь .

Вот пример кода, который создаст почтовый ящик:

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;
}
1 голос
/ 23 июля 2011

Это старый вопрос, но он может помочь будущим посетителям ...

Ответ w69rdy у меня не сработал.Но я получил это работает и написал об этом здесь http://pedroliska.wordpress.com/2011/07/22/running-exchange-management-shell-commands-powershell-with-c/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...