1

我正在使用 Service Manager/System Center 2012,Service Pack 1。(版本 7.5.2905.0

我正在尝试做一些应该很简单的事情,但似乎有关于如何实际进行查询的零文档。因此,我有一个用户对象(该用户的 EnterpriseManagementObject),并且我想查询服务管理器以了解该用户是受影响用户的所有事件。

代码是:(从我找到的一个例子)

            strIncidentSearchCriteria =
            String.Format(@"<Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">
                               <Expression>                                            
                                  <SimpleExpression>
                                      <ValueExpressionLeft>
                                       <Property>$Context/Path[Relationship='WorkItem!System.WorkItemAffectedUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/UserName$</Property>
                                       </ValueExpressionLeft>
                                       <Operator>Equal</Operator>
                                      <ValueExpressionRight>
                                        <Value>" + user.FullName + @"</Value>
                                      </ValueExpressionRight>
                                  </SimpleExpression>
                               </Expression>                             
                            </Criteria>");

            EnterpriseManagementObjectCriteria emocWorkitem = new EnterpriseManagementObjectCriteria(incidentSearchCriteria, mpc, mp, emg);

这会引发异常:

The provided path ($Context/Path[Relationship='WorkItem!System.WorkItemAffectedUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/UserName$) was not valid.

参数名称:路径参考

我很确定 Property 字段是错误的,但似乎没有关于如何创建 XML 或如何指定 Property 或任何内容的文档。

如果我将搜索更改为:

    //strIncidentSearchCriteria = String.Format(@"<Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">" +
    //                "<Expression>" +
    //                "<SimpleExpression>" +
    //                    "<ValueExpressionLeft>" +
    //                    "<Property>$Context/Property[Type='System.WorkItem.Incident']/Status$</Property>" +
    //                    "</ValueExpressionLeft>" +
    //                    "<Operator>NotEqual</Operator>" +
    //                    "<ValueExpressionRight>" +
    //                    "<Value>" + new Guid("2b8830b6-59f0-f574-9c2a-f4b4682f1681") + "</Value>" +  // Resolved
    //                    //Closed:"<Value>" + new Guid("bd0ae7c4-3315-2eb3-7933-82dfc482dbaf") + "</Value>" +
    //                    "</ValueExpressionRight>" +
    //                "</SimpleExpression>" +
    //                "</Expression>" +
    //            "</Criteria>");

(当然,没有注释掉)

另外,另一个问题:如果我执行上面的查询并返回所有已解决的匹配事件 - 它在能够枚举之前内存不足 - 显然,它是这样编写的,因此它将所有匹配的内容拉入列表或其他内容中然后你枚举 - 而不是根据需要提取更多数据。有什么办法可以拉动一切吗?(我最初的方法是提取所有内容,然后自己过滤 - 这是一次性的,如果需要,我可以忍受一个小时。(但是,我也需要这个查询用于长期项目,所以我'我真的需要知道如何查询影响给定用户的所有事件,以及所有分配的用户是 X 等。你从哪里学到这些东西??)

谢谢

更新:

这个查询失败了,我该如何让它工作。(这是我最终想要开始工作的事情)

<Criteria xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/"> 
   <Expression>
      <And>
         <Expression>
            <SimpleExpression>
               <ValueExpressionLeft>
                  <Property>$Context/Property[Type='System.WorkItem.Incident']/Status$</Property>
               </ValueExpressionLeft>
               <Operator>NotEqual</Operator>
               <ValueExpressionRight>
                  <Value>2b8830b6-59f0-f574-9c2a-f4b4682f1681</Value>
               </ValueExpressionRight>
            </SimpleExpression>
         </Expression>

         <Expression>
            <And>
               <Expression>
                  <SimpleExpression>
                     <ValueExpressionLeft>
                        <Property>$Context/Property[Type='System.WorkItem.Incident']/Status$</Property>
                     </ValueExpressionLeft>
                     <Operator>NotEqual</Operator>
                     <ValueExpressionRight>
                        <Value>bd0ae7c4-3315-2eb3-7933-82dfc482dbaf</Value>
                     </ValueExpressionRight>
                  </SimpleExpression>
               </Expression>


          <Expression>
            <SimpleExpression>
              <ValueExpressionLeft>
                                <Property>$Context/Path[Relationship='WorkItem!System.WorkItemAssignedToUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/DisplayName$</Property>
              </ValueExpressionLeft>
                 <Operator>Like</Operator>
              <ValueExpressionRight>
                <Value>%Bill%</Value>
              </ValueExpressionRight>
            </SimpleExpression>
          </Expression>

            </And>       
      </And>
   </Expression>
</Criteria>

=======更新和解决方案========

想通了,你必须告诉它事件类型的位置,所以这个查询有效:

<Criteria xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/"> 
   <Expression>
      <And>
         <Expression>
            <SimpleExpression>
               <ValueExpressionLeft>
                  <Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/Status$</Property>
               </ValueExpressionLeft>
               <Operator>NotEqual</Operator>
               <ValueExpressionRight>
                  <Value>2b8830b6-59f0-f574-9c2a-f4b4682f1681</Value>
               </ValueExpressionRight>
            </SimpleExpression>
         </Expression>

         <Expression>
            <And>
               <Expression>
                  <SimpleExpression>
                     <ValueExpressionLeft>
                        <Property>$Context/Property[Type='CoreIncident!System.WorkItem.Incident']/Status$</Property>
                     </ValueExpressionLeft>
                     <Operator>NotEqual</Operator>
                     <ValueExpressionRight>
                        <Value>bd0ae7c4-3315-2eb3-7933-82dfc482dbaf</Value>
                     </ValueExpressionRight>
                  </SimpleExpression>
               </Expression>


          <Expression>
            <SimpleExpression>
              <ValueExpressionLeft>
                                <Property>$Context/Path[Relationship='WorkItem!System.WorkItemAssignedToUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/DisplayName$</Property>
              </ValueExpressionLeft>
                 <Operator>Like</Operator>
              <ValueExpressionRight>
                <Value>%Bill%</Value>
              </ValueExpressionRight>
            </SimpleExpression>
          </Expression>

            </And>       
         </Expression>
      </And>
   </Expression>
</Criteria>
4

1 回答 1

1

我建议您学习类型投影... ¿ 为什么?因为类型投影就像 Service Manager 中的视图。您评论的标准非常有效,因为您只使用了一个简单的属性,并且他的属性在类EnterpriseManagementObject中可用,而这些属性又存在于管理包中。您的代码抛出的异常是正确的,因为他在事件类的基本定义中找不到该路径。System.WorkItem.IncidentSystem.WorkItem.Incident.Library

解决方案?键入投影。

此路径:$Context/Path[Relationship='WorkItem!System.WorkItemAffectedUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/UserName$属于事件与受影响用户之间的关系。

因此,如果您运行此查询,您可以找到此类型投影的名称:

SELECT
LT.ElementName,
LT.LTValue,
LT.MPElementId,
MP.MPName,
MP.MPFriendlyName,
MP.MPKeyToken,
MP.MPVersion
FROM 
LocalizedText LT 
INNER JOIN 
ManagementPack MP ON LT.ManagementPackId = MP.ManagementPackId
WHERE 
LT.ElementName like '%Incident.Projection%'
AND
LT.LanguageCode like '%ENU%'

您会发现您正在搜索的类型投影的名称是System.WorkItem.Incident.ProjectionType并且他存在于ServiceManager.IncidentManagement.Library管理包中。

现在是时候去VS了。现在我们必须更改我们正在使用 ( EnterpriseManagementObject)的对象EnterpriseManagementObjectProjection

     string strCriteria =
     String.Format(@"<Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">
          <Expression>
            <SimpleExpression>
              <ValueExpressionLeft>
                <Property>$Context/Path[Relationship='WorkItem!System.WorkItemAffectedUser' TypeConstraint='System!System.Domain.User']/Property[Type='System!System.Domain.User']/UserName$</Property>
              </ValueExpressionLeft>
                 <Operator>Like</Operator>
              <ValueExpressionRight>
                <Value>%" + userName + @"%</Value>
              </ValueExpressionRight>
            </SimpleExpression>
          </Expression>
        </Criteria>");


        ManagementPack workItemMp = emg.GetManagementPack("System.WorkItem.Incident.Library", "31bf3856ad364e35", new Version("7.5.3079.0"));  // Incident MP
        ManagementPack projMp = emg.GetManagementPack("ServiceManager.IncidentManagement.Library", "31bf3856ad364e35", new Version("7.5.3079.0")); // <-- MP where Type Projection lives
        ManagementPackTypeProjection workItemRelConfigProj = emg.EntityTypes.GetTypeProjection("System.WorkItem.Incident.ProjectionType", projMp); // <-- Type Projection Name
        ObjectProjectionCriteria projCriteria = new ObjectProjectionCriteria(strCriteria, workItemRelConfigProj, projMp, emg);
        IObjectProjectionReader<EnterpriseManagementObject> reader = emg.EntityObjects.GetObjectProjectionReader<EnterpriseManagementObject>(projCriteria, ObjectQueryOptions.Default);

        if (reader != null && reader.Count > 0)
        {
            foreach (EnterpriseManagementObjectProjection obj in reader)
            {
                //TO DO
            }
        }

所以现在你的路径将完美运行,这将返回你想要的对象:-)。我希望这会有所帮助。

来自智利的欢呼!

于 2015-08-14T16:20:06.993 回答