0
指定当我运行在Visual Studio 2015年以下,而调试的代码,它工作正常命令ID存在。当它被部署到IIS时,在第二个ps.Invoke()
行处出现以下错误:WinRM服务无法处理请求。一个命令已经与客户端
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();
}
}
}
我连接到Exchange(API文档:https://technet.microsoft.com/en-us/library/dn641234(v=exchg.160).aspx)使用C#和PowerShell 3.0试图成员在Exchange添加到通讯组。
PowerShellResponse
是我们自定义的类,而ps.GetResponse()
是创建PowerShellResponse
的自定义函数。