コード例 #1
0
ファイル: Incident.cs プロジェクト: microsoft/smlets
        private void UpdateNamedIncident(EnterpriseManagementGroup emg, string id)
        {
            // Define the type projection that you want to query for.
            // This example queries for type projections defined by the System.WorkItem.Incident.ProjectionType
            // type in the ServiceManager.IncidentManagement.Library management pack.
            ManagementPackTypeProjection incidentTypeProjection = SMHelpers.GetManagementPackTypeProjection(TypeProjections.System_WorkItem_Incident_ProjectionType, SMHelpers.GetManagementPack(ManagementPacks.ServiceManager_IncidentManagement_Library, emg), emg);

            // Define the query criteria string.
            // This is XML that validates against the Microsoft.EnterpriseManagement.Core.Criteria schema.
            string incidentCriteria = String.Format(@"
                <Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">
                  <Reference Id=""System.WorkItem.Library"" PublicKeyToken=""{0}"" Version=""{1}"" Alias=""WorkItem"" />
                      <Expression>
                            <SimpleExpression>
                              <ValueExpressionLeft>
                                <Property>$Context/Property[Type='WorkItem!System.WorkItem']/Id$</Property>
                              </ValueExpressionLeft>
                              <Operator>Equal</Operator>
                              <ValueExpressionRight>
                                <Value>" + _ID + @"</Value>
                              </ValueExpressionRight>
                            </SimpleExpression>
                      </Expression>
                </Criteria>
                ", workitemMp.KeyToken, workitemMp.Version.ToString());


            WriteDebug(incidentCriteria);
            // Define the criteria object by using one of the criteria strings.
            ObjectProjectionCriteria criteria = new ObjectProjectionCriteria(incidentCriteria, incidentTypeProjection, emg);

            EnterpriseManagementObjectProjection emop = null;

            // For each retrieved type projection, display the properties.
            foreach (EnterpriseManagementObjectProjection projection in
                     emg.EntityObjects.GetObjectProjectionReader <EnterpriseManagementObject>(criteria, ObjectQueryOptions.Default))
            {
                emop = projection;
            }

            if (emop != null)
            {
                SMHelpers.UpdateIncident(emg, clsIncident, emop, this.Impact, this.Urgency, this.Status, this.Classification, this.Source, this.SupportGroup, this.Comment, this.UserComment, this.Description, this.AttachmentPath);
            }
            else
            {
                WriteError(new ErrorRecord(new ObjectNotFoundException(_ID), "Incident not found", ErrorCategory.ObjectNotFound, criteria));
                return;
            }
        }
コード例 #2
0
ファイル: Incident.cs プロジェクト: microsoft/smlets
        protected override void ProcessRecord()
        {
            ManagementPackClass clsIncident = SMHelpers.GetManagementPackClass(ClassTypes.System_WorkItem_Incident, SMHelpers.GetManagementPack(ManagementPacks.System_WorkItem_Incident_Library, _mg), _mg);

            ManagementPack incidentProjectionMp = SMHelpers.GetManagementPack(ManagementPacks.ServiceManager_IncidentManagement_Library, _mg);
            ManagementPackTypeProjection incidentTypeProjection = SMHelpers.GetManagementPackTypeProjection(TypeProjections.System_WorkItem_Incident_ProjectionType, incidentProjectionMp, _mg);

            WriteVerbose("Starting to build search criteria...");
            List <string> criterias    = new List <string>();
            bool          haveCriteria = false;


            // Define the query criteria string.
            // This is XML that validates against the Microsoft.EnterpriseManagement.Core.Criteria schema.
            StringBuilder incidentCriteria = new StringBuilder(String.Format(@"
                <Criteria xmlns=""http://Microsoft.EnterpriseManagement.Core.Criteria/"">
                  <Reference Id=""System.WorkItem.Incident.Library"" PublicKeyToken=""{0}"" Version=""{1}"" Alias=""WorkItem"" />
                      <Expression>", incidentMp.KeyToken, incidentMp.Version.ToString()));

            if (this._ID != null)
            {
                haveCriteria = true;
                WriteVerbose(string.Format("Adding \"ID equal {0}\" to search criteria", this.ID));
                criterias.Add(String.Format(criteriaString, "Id", "Equal", _ID));
            }

            if (this._Title != null)
            {
                haveCriteria = true;
                WriteVerbose(string.Format("Adding \"Title like {0}\" to search criteria", this.Title));
                criterias.Add(String.Format(criteriaString, "Title", "Like", _Title));
            }

            if (this.Impact != null)
            {
                haveCriteria = true;
                ManagementPackEnumeration impEnum = SMHelpers.GetEnum(this.Impact, impactBase);
                WriteVerbose(string.Format("Adding \"Impact equal {0}({1})\" to search criteria", impEnum.DisplayName, impEnum.Id));
                criterias.Add(String.Format(criteriaString, "Impact", "Equal", impEnum.Id));
            }

            if (this.Urgency != null)
            {
                haveCriteria = true;
                ManagementPackEnumeration urgEnum = SMHelpers.GetEnum(this.Urgency, urgencyBase);
                WriteVerbose(string.Format("Adding \"Urgency equal {0}({1})\" to search criteria", urgEnum.DisplayName, urgEnum.Id));
                criterias.Add(String.Format(criteriaString, "Urgency", "Equal", urgEnum.Id));
            }

            if (this.Status != null)
            {
                haveCriteria = true;
                ManagementPackEnumeration statEnum = SMHelpers.GetEnum(this.Status, statusBase);
                WriteVerbose(string.Format("Adding \"Status equal {0}({1})\" to search criteria", statEnum.DisplayName, statEnum.Id));
                criterias.Add(String.Format(criteriaString, "Status", "Equal", statEnum.Id));
            }

            if (this.Classification != null)
            {
                haveCriteria = true;
                ManagementPackEnumeration classificationEnum = SMHelpers.GetEnum(this.Classification, classificationBase);
                WriteVerbose(string.Format("Adding \"Classification equal {0}({1})\" to search criteria", classificationEnum.DisplayName, classificationEnum.Id));
                criterias.Add(String.Format(criteriaString, "Classification", "Equal", classificationEnum.Id));
            }

            if (this.CreatedBefore != DateTime.MinValue)
            {
                haveCriteria = true;
                CultureInfo enUsInfo = new CultureInfo("en-US");
                WriteVerbose(string.Format("Adding \"CreatedDate less than {0}\" to search criteria", this.CreatedBefore.ToString()));
                criterias.Add(String.Format(criteriaString, "CreatedDate", "Less", CreatedBefore.ToUniversalTime()));
            }

            if (this.CreatedAfter != DateTime.MinValue)
            {
                haveCriteria = true;
                CultureInfo enUsInfo = new CultureInfo("en-US");
                WriteVerbose(string.Format("Adding \"CreatedDate greater than {0}\" to search criteria", this.CreatedAfter.ToString()));
                criterias.Add(String.Format(criteriaString, "CreatedDate", "Greater", this.CreatedAfter.ToUniversalTime()));
            }

            if (criterias.Count > 1)
            {
                haveCriteria = true;
                for (int i = 0; i < criterias.Count; i++)
                {
                    criterias[i] = "<Expression>" + criterias[i] + "</Expression>";
                }
            }

            if (criterias.Count > 1)
            {
                incidentCriteria.AppendLine("<And>");
            }

            foreach (var item in criterias)
            {
                incidentCriteria.AppendLine(item);
            }

            if (criterias.Count > 1)
            {
                incidentCriteria.AppendLine("</And>");
            }

            incidentCriteria.AppendLine(@"</Expression>
                </Criteria>");

            WriteDebug("Search criteria: " + incidentCriteria.ToString());

            ObjectProjectionCriteria criteria = null;

            if (haveCriteria)
            {
                // Define the criteria object by using one of the criteria strings.
                criteria = new ObjectProjectionCriteria(incidentCriteria.ToString(), incidentTypeProjection, _mg);
                WriteVerbose("Criteria is: " + incidentCriteria.ToString());
            }
            else
            {
                criteria = new ObjectProjectionCriteria(incidentTypeProjection);
                WriteVerbose("Criteria is Projection");
            }
            string[] EnumPropertiesToAdapt = { "Urgency", "Impact", "TierQueue", "Classification", "Status" };
            // For each retrieved type projection, display the properties.
            List <EnterpriseManagementObjectProjection> result = new List <EnterpriseManagementObjectProjection>();

            foreach (EnterpriseManagementObjectProjection projection in
                     _mg.EntityObjects.GetObjectProjectionReader <EnterpriseManagementObject>(criteria, ObjectQueryOptions.Default))
            {
                if (this.InactiveFor == TimeSpan.Zero || projection.Object.LastModified.Add(this.InactiveFor) < DateTime.UtcNow)
                {
                    //if (this.OlderThan == TimeSpan.Zero || projection.Object.TimeAdded.Add(this.OlderThan) < DateTime.UtcNow)
                    //{
                    //result.Add(projection);
                    WriteVerbose(String.Format("Adding incident \"{0}\" to the pipeline", projection.Object.Name));
                    // WriteObject(projection, false);
                    PSObject o = new PSObject();
                    o.Members.Add(new PSNoteProperty("__base", projection));
                    o.Members.Add(new PSScriptMethod("GetAsXml", ScriptBlock.Create("[xml]($this.__base.CreateNavigator().OuterXml)")));
                    o.Members.Add(new PSNoteProperty("Object", ServiceManagerObjectHelper.AdaptManagementObject(this, projection.Object)));
                    o.Members.Add(new PSNoteProperty("ID", projection.Object[null, "Id"]));
                    o.Members.Add(new PSNoteProperty("Title", projection.Object[null, "Title"]));
                    o.Members.Add(new PSNoteProperty("Description", projection.Object[null, "Description"]));
                    o.Members.Add(new PSNoteProperty("DisplayName", projection.Object[null, "DisplayName"]));
                    o.Members.Add(new PSNoteProperty("Priority", projection.Object[null, "Priority"]));
                    foreach (string s in EnumPropertiesToAdapt)
                    {
                        o.Members.Add(new PSNoteProperty(s, GetEnumerationDisplayString(projection.Object[null, s])));
                    }

                    o.Members.Add(new PSNoteProperty("CreatedDate", projection.Object[null, "CreatedDate"]));
                    o.Members.Add(new PSNoteProperty("LastModified", projection.Object.LastModified.ToLocalTime()));
                    // add the relationship values
                    foreach (KeyValuePair <ManagementPackRelationshipEndpoint, IComposableProjection> helper in projection)
                    {
                        // EnterpriseManagementObject myEMO = (EnterpriseManagementObject)helper.Value.Object;
                        WriteVerbose("Adapting relationship objects: " + helper.Key.Name);
                        String   myName     = helper.Key.Name;
                        PSObject adaptedEMO = ServiceManagerObjectHelper.AdaptManagementObject(this, helper.Value.Object);
                        if (helper.Key.MaxCardinality > 1)
                        {
                            // OK, this is a collection, so add the critter
                            // This is so much easier in PowerShell
                            if (o.Properties[myName] == null)
                            {
                                o.Members.Add(new PSNoteProperty(myName, new ArrayList()));
                            }
                            ((ArrayList)o.Properties[myName].Value).Add(adaptedEMO);
                        }
                        else
                        {
                            try
                            {
                                o.Members.Add(new PSNoteProperty(helper.Key.Name, adaptedEMO));
                            }
                            catch (ExtendedTypeSystemException e)
                            {
                                WriteVerbose("Readapting relationship object -> collection :" + e.Message);
                                // We should really only get this exception if we
                                // try to add a create a new property which already exists
                                Object    currentPropertyValue = o.Properties[myName].Value;
                                ArrayList newValue             = new ArrayList();
                                newValue.Add(currentPropertyValue);
                                newValue.Add(adaptedEMO);
                                o.Properties[myName].Value = newValue;

                                // WriteError(new ErrorRecord(e, "AddAssociatedObject", ErrorCategory.InvalidOperation, p));
                            }
                        }
                    }


                    o.TypeNames[0] = String.Format(CultureInfo.CurrentCulture, "EnterpriseManagementObjectProjection#{0}", incidentTypeProjection.Name);
                    WriteObject(o);
                    //}
                }
            }
        }