Пример #1
0
        protected override void ProcessRecord()
        {
            // If nothing has changed, stop
            if (!hasChanged)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new ArgumentException("No values have changed, please provide new values for the incident"),
                        "Incident",
                        ErrorCategory.InvalidOperation,
                        "NoNewValues")
                    );
            }
            try
            {
                systemMp = _mg.ManagementPacks.GetManagementPack(SystemManagementPack.System);

                try
                {
                    clsIncident = SMHelpers.GetManagementPackClass(ClassTypes.System_WorkItem_Incident, SMHelpers.GetManagementPack(ManagementPacks.System_WorkItem_Incident_Library, _mg), _mg);
                }
                catch
                {
                    try
                    {
                        // last ditch try to get a class (this happens if a debug build is used
                        clsIncident = _mg.EntityTypes.GetClasses(new ManagementPackClassCriteria("Name = 'System.WorkItem.Incident'")).First();
                    }
                    catch (Exception e)
                    {
                        ThrowTerminatingError(new ErrorRecord(e, "badclass", ErrorCategory.ObjectNotFound, "System.WorkItem.Incident"));
                    }
                }

                int i = 1;
                if (InputObject != null)
                {
                    foreach (var item in this.InputObject)
                    {
                        ProgressRecord prog = new ProgressRecord(1, "Updating " + item.Object.DisplayName, String.Format("{0} of {1}", i, this.InputObject.Length.ToString()));
                        WriteProgress(prog);
                        SMHelpers.UpdateIncident(_mg, clsIncident, item, this.Impact, this.Urgency, this.Status, this.Classification, this.Source, this.SupportGroup, this.Comment, this.UserComment, this.Description, this.AttachmentPath);
                        i++;
                    }
                }
                else
                {
                    WriteDebug("No input object passed");
                    if (this._ID != null)
                    {
                        UpdateNamedIncident(_mg, this._ID);
                    }
                }
            }
            catch (Exception ex)
            {
                ThrowTerminatingError(new ErrorRecord(ex, "BadSet", ErrorCategory.InvalidOperation, InputObject));
            }
        }
Пример #2
0
        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;
            }
        }
Пример #3
0
        protected override void ProcessRecord()
        {
            try
            {
                ManagementPackClass clsIncident = SMHelpers.GetManagementPackClass(ClassTypes.System_WorkItem_Incident, SMHelpers.GetManagementPack(ManagementPacks.System_WorkItem_Incident_Library, _mg), _mg);

                EnterpriseManagementObjectProjection incidentProjection = new EnterpriseManagementObjectProjection(_mg, clsIncident);

                WriteVerbose("Setting basic properties");
                incidentProjection.Object[clsIncident, "Id"].Value = incidentPrefix;

                incidentProjection.Object[clsIncident, "Title"].Value = this.Title;

                if (CreatedDate != null)
                {
                    incidentProjection.Object[clsIncident, "CreatedDate"].Value = this.CreatedDate;
                }

                SMHelpers.UpdateIncident(_mg, clsIncident, incidentProjection,
                                         this.Impact, this.Urgency, this.Status, this.Classification, this.Source, this.SupportGroup, null, null, this.Description, null);


                if (AffectedCIs != null)
                {
                    WriteVerbose("Adding affected CIs");
                    foreach (var item in AffectedCIs)
                    {
                        WriteVerbose(string.Format("Adding {0} as affected configuration item.", item.Object.DisplayName));
                        SMHelpers.AddAffectedCI(incidentProjection, item.Object, _mg);
                    }
                }

                if (AffectedUser != null)
                {
                    WriteVerbose(string.Format("Adding {0} as affected configuration item.", AffectedUser));
                    SMHelpers.AddAffectedUser(incidentProjection, this.AffectedUser, _mg);
                }

                // a bulk operation
                // do in batches of toCommit (set above)
                if (Bulk)
                {
                    toCommit++;
                    idd.Add(incidentProjection);
                    if (toCommit >= batchSize)
                    {
                        idd.Commit(_mg);
                        idd      = new IncrementalDiscoveryData();
                        toCommit = 0;
                    }
                }
                else
                {
                    incidentProjection.Commit();
                }

                if (PassThru)
                {
                    //Pass the new object to the pipeline
                    WriteObject(incidentProjection);
                }
            }
            catch (Exception ex)
            {
                WriteError(new ErrorRecord(ex, "NewIncident", ErrorCategory.InvalidOperation, Title));
            }
        }