WinRM服务无法处理该请求.具有客户端指定的命令ID的命令已存在
本文关键字:命令 ID 存在 客户端 服务 处理 请求 WinRM | 更新日期: 2023-09-27 17:59:16
当我在Visual Studio 2015中调试时运行下面的代码时,它运行得很好。当它被部署到IIS时,我在第二行ps.Invoke()
处得到以下错误:
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
的自定义函数。
由于无法在任何地方找到在线讨论的错误,我提出的解决方案不是在一个using语句中调用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();
}
}
}