【问题标题】:The WinRM service cannot process the request. A command already exists with the command ID specified by the clientWinRM 服务无法处理该请求。具有客户端指定的命令 ID 的命令已存在
【发布时间】:2016-12-03 08:01:02
【问题描述】:

当我在 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();
        }
    }
}

我正在使用 C# 和 PowerShell 3.0 连接到 Exchange(API 文档:https://technet.microsoft.com/en-us/library/dn641234(v=exchg.160).aspx),试图将成员添加到 Exchange 中的通讯组。

PowerShellResponse 是我们的自定义类,ps.GetResponse() 是自定义函数来创建说PowerShellResponse

【问题讨论】:

    标签: c# powershell exchange-server powershell-3.0


    【解决方案1】:

    由于无法在网上任何地方找到此错误,我想出的解决方案不是在一个 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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多