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
을 작성하는 사용자 정의 함수입니다.