Bonjour,
Voila une bonne semaine que je galère à essayer de créer des boites mails sur Exchange 2010 via mon appli C#.
J'ai réussi à ne plus avoir de message d'erreur à l'exécution mais les boites mail ne se créent toujours pas... je désespère.
Je vous met ci-dessous mon code :
Cette fonction me génère ma commande PowerShell que je place en argument dans celle-ci dessous :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public static PSCommand commandeMailboxUser(string firstName, string lastName, string login, string OU, string domain) { PSCommand command = new PSCommand(); command.AddCommand("New-Mailbox"); command.AddParameter("Name", lastName + " " + firstName); command.AddParameter("Alias", login); command.AddParameter("OrganizationalUnit", OU); string UPN = login+"@"+domain; command.AddParameter("UserPrincipalName", UPN); command.AddParameter("SamAccountName", login); command.AddParameter("FirstName", firstName); command.AddParameter("Initials", ""); command.AddParameter("LastName", lastName); SecureString password = convertToSecureString("P@ssw0rd"); command.AddParameter("Password", password); command.AddParameter("ResetPasswordOnNextLogon", false); return command; }
La connexion au serveur fonctionne bien puisqu'un test avec un get-mailbox a été concluant mais avec ce code rien y fait...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public static void createMailBoxUsingPS(string runasUser, SecureString runasPass, PSCommand command) { string domain = Commun.getDom(); runasUser = runasUser + "@" +domain; PSCredential credential = new PSCredential(runasUser, runasPass); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(param.Default.httpExch), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); PowerShell powershell = PowerShell.Create(); 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 powershell.Invoke(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } 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; } }
L'application est exécuté depuis un server 2008, j'ai testé sur un server 2003 et sur un Windows Seven mais rien y fait...
Un détail qui est peu être important le compte qui a les droits pour créer les boite mail n'est pas le même que celui avec lequel j'exécute l'application, j'ai donc mis une fonction intermédiaire qui fait office de runas mais je ne pense pas que ça vienne de là car si je tape un autre compte qui n'a pas les droits j'ai bien une erreur comme quoi je n'ai pas les droits.
Là je sèche complètement, je vous remercie d'avance pour votre aide.
Partager