2017-09-27 8 views
0

Je suis d'essayer de créer des groupes de distribution pour l'échange avec Powershell de C# avec ce code:WinRM refusé pour PowerShell à distance en C#, cryto nécessaire?

private void createDistributionGroup(string groupName, string DNOU) 
    { 
     System.Security.SecureString pass = new System.Security.SecureString("57R0NG_P455W0RD"); 
     PSCredential cred = new PSCredential(SA_username, pass); 
     WSManConnectionInfo connection = new WSManConnectionInfo(new Uri("http://myEXserver.mydomain.com/PowerShell/"), "https://schemas.microsoft.com/powershell/Microsoft.Exchange", cred); 
     connection.AuthenticationMechanism = AuthenticationMechanism.Basic; 
     Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connection); 
     PowerShell ps = PowerShell.Create(); 
     PSCommand command = new PSCommand(); 
     command.AddCommand("New-DistributionGroup"); 
     command.AddParameter("Name", groupName); 
     ps.Commands = command; 
     try 
     { 
      runspace.Open(); 
      ps.Runspace = runspace; 
      ps.Invoke(); 
     } 
     finally 
     { 
      runspace.Dispose(); 
      runspace = null; 
      ps.Dispose(); 
      ps = null; 
     } 
    } 

Mais quand j'exécute ce code, une erreur winrm ajouter que: connexion forbiden, trafic non crypté refusé par le serveur ...

Comment puis-je résoudre ce problème?

Répondre

0

Avez-vous essayé d'utiliser l'authentification Kerberos? Si vous devez utiliser l'authentification de base, vous devez utiliser SSL. Ceci n'est parfois pas configuré correctement, vous devez donc faire quelques tests.

+0

Je l'ai testé avec l'authentification kerberos mais il annexera une ARROR à –

0

Avec l'authentification de base, vous (comme TomG) devez utiliser SSL/HTTPS. Vous avez probablement besoin aussi définir l'authentification de proxy de base:

WSManConnectionInfo connection = new WSManConnectionInfo(new Uri("**https**://myEXserver.mydomain.com/PowerShell/"), "https://schemas.microsoft.com/powershell/Microsoft.Exchange", cred); 
connection.AuthenticationMechanism = AuthenticationMechanism.Basic; 
connection.ProxyAuthentication = AuthenticationMechanism.Basic; 

Lorsque vous utilisez Kerberos et HTTP, l'accès proxy serait mis à aucun:

WSManConnectionInfo connection = new WSManConnectionInfo(new Uri("**http**://myEXserver.mydomain.com/PowerShell/"), "https://schemas.microsoft.com/powershell/Microsoft.Exchange", cred); 
connection.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
connection.ProxyAccessType = ProxyAccessType.None;