Comment créer par programme une boîte aux lettres Exchange 2010 à l’aide de C #

On m’a confié la tâche d’écrire un programme pour créer automatiquement une boîte aux lettres d’échange 2010. Ma recherche me dit d’utiliser PowerShell mais je n’arrive pas à trouver l’espace de nom à référencer et j’aimerais un exemple de code. J’ai trouvé du code sur le Web, mais je ne sais pas quel est l’espace de noms pour PowerShell. Je pense que cela pourrait être System.Management.Automation, mais lorsque j’essaie de référencer l’espace de noms, il n’existe pas dans la liste de dotnet. Tout ce que j’ai est System.Management et System.Management.Instrumentation.

Toute aide serait appréciée?

Lorsque je l’ai fait, j’ai dû télécharger Powershell séparément, mais je ne suis pas sûr que ce soit toujours le cas. Vous pouvez l’obtenir d’ ici .

Voici un exemple de code qui créera une boîte aux lettres:

SecureSsortingng password = new SecureSsortingng(); ssortingng str_password = "pass"; ssortingng username = "userr"; ssortingng 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; } 

C’est une vieille question, mais cela pourrait aider les futurs visiteurs …

La réponse de w69rdy n’a pas fonctionné pour moi. Mais je l’ai fait fonctionner et j’ai blogué ici http://pedroliska.wordpress.com/2011/07/22/running-exchange-management-shell-commands-powershell-with-c/