2016-07-28 8 views
0

Visual Studio 2015에서 디버깅하는 동안 아래 코드를 실행하면 정상적으로 작동합니다. IIS에 배포되면 두 번째 ps.Invoke() 줄에 다음 오류가 표시됩니다.WinRM 서비스가 요청을 처리 할 수 ​​없습니다. 클라이언트가 지정한 명령 ID를 가진 명령이 이미 있습니다.

WinRM 서비스가 요청을 처리 할 수 ​​없습니다. 클라이언트가 지정한 명령 ID를 가진 명령이 이미 존재합니다 ( ).

public static PowerShellResponse AddToDistributionGroup(Credentials creds, string groupName, string memberEmail) 
{ 
    PSCredential cred = new PSCredential(creds.Username, creds.Password.ToSecureString()); 

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(Settings.ExchangeServerAutomationUrl), Settings.ExchangeAutomationSchemaName, cred); 
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 

    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) 
    { 
     using (PowerShell ps = PowerShell.Create()) 
     { 
      runspace.Open(); 
      ps.Runspace = runspace; 
      //can't pipe OU to Add-DistrubtionGroupMember b/c it blows up w/ "null reference exception" when member already exists 

      var group = 
        ps 
         .AddCommand("Get-DistributionGroup") 
          .AddParameter("Identity", groupName) 
          .AddParameter("OrganizationalUnit", creds.GetUserDN()) 
         .Invoke() 
         .SingleOrDefault(); 

      if (group == null) 
       return new PowerShellResponse() { Errors = new List<string> { "Group not found." } }; 

      ps.AddStatement(); 

      ps.AddCommand("Add-DistributionGroupMember") 
       .AddParameter("Identity", ((dynamic)group).Identity) 
       .AddParameter("Member", memberEmail); 

      ps.Invoke(); //this is where the error shows up 

      return ps.GetResponse(); 
     } 
    } 
} 

내가 거래소에 연결하고있어 (API 문서 도구 : https://technet.microsoft.com/en-us/library/dn641234(v=exchg.160).aspx)은 Exchange 메일 그룹에 구성원을 추가하려고 C# 및 PowerShell을 3.0을 사용.

PowerShellResponse은 사용자 정의 클래스이며, ps.GetResponse()은 상기 PowerShellResponse을 작성하는 사용자 정의 함수입니다.

답변

0

어디서든 온라인으로이 오류를 찾을 수 없었기 때문에 내가 사용한 해결책은 ps.Invoke()을 사용 문에서 두 번 호출하지 않는 것이 었습니다. 다음은 로컬로 배포되고 IIS에 배포 된 후에는 잘 작동합니다.

public static PowerShellResponse AddToDistributionGroup(Credentials creds, string groupName, string memberEmail) 
{ 
    PSCredential cred = new PSCredential(creds.Username, creds.Password.ToSecureString()); 

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(Settings.ExchangeServerAutomationUrl), Settings.ExchangeAutomationSchemaName, cred); 
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 

    PSObject group; 

    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) 
    { 
     using (PowerShell ps = PowerShell.Create()) 
     { 
      runspace.Open(); 
      ps.Runspace = runspace; 

      group = 
        ps 
         .AddCommand("Get-DistributionGroup") 
          .AddParameter("Identity", groupName) 
          .AddParameter("OrganizationalUnit", creds.GetUserDN()) 
         .Invoke() 
         .SingleOrDefault(); 
     } 
    } 

    //can't pipe OU to Add-DistrubtionGroupMember b/c it blows up w/ "null reference exception" when member already exists 
    if (group == null) 
     return new PowerShellResponse() { Errors = new List<string> { "Group not found." } }; 

    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) 
    { 
     using (PowerShell ps = PowerShell.Create()) 
     { 
      runspace.Open(); 
      ps.Runspace = runspace; 

      ps.AddCommand("Add-DistributionGroupMember") 
       .AddParameter("Identity", ((dynamic)group).Identity) 
       .AddParameter("Member", memberEmail); 

      ps.Invoke(); 

      return ps.GetResponse(); 
     } 
    } 
}