Пример #1
0
        protected override void BeginProcessing()
        {
            // the base.BeginProcesing must come first as it sets up the enterprisemanagementgroup!
            base.BeginProcessing();
            ManagementPackClass clsAnnouncement = SMHelpers.GetManagementPackClass(ClassTypes.System_Announcement_Item, SMHelpers.GetManagementPack(ManagementPacks.System_AdminItem_Library, _mg), _mg);

            // Compare the string case insenstively
            if (_Priority != null)
            {
                if (String.Compare(_Priority, "low", true) == 0)
                {
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Low, _mg);
                }
                else if (String.Compare(_Priority, "critical", true) == 0)
                {
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Critical, _mg);
                }
                else if (String.Compare(_Priority, "medium", true) == 0)
                {
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Medium, _mg);
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(new ArgumentException("Priority"), "Priority must be low/medium/critical", ErrorCategory.InvalidOperation, _Priority));
                }
            }
        }
Пример #2
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));
            }
        }
Пример #3
0
        protected override void ProcessRecord()
        {
            try
            {
                ManagementPackClass       clsAnnouncement = SMHelpers.GetManagementPackClass(ClassTypes.System_Announcement_Item, SMHelpers.GetManagementPack(ManagementPacks.System_AdminItem_Library, _mg), _mg);
                ManagementPackEnumeration enumPriority    = null;
                switch (_Priority)
                {
                case "Low":
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Low, _mg);
                    break;

                case "Critical":
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Critical, _mg);
                    break;

                case "Medium":
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Medium, _mg);
                    break;

                default:
                    enumPriority = SMHelpers.GetEnum(Enumerations.System_Announcement_PriorityEnum_Medium, _mg);
                    break;
                }

                CreatableEnterpriseManagementObject emo = new CreatableEnterpriseManagementObject(_mg, clsAnnouncement);

                emo[clsAnnouncement, "Id"].Value = System.Guid.NewGuid().ToString();
                if (_DisplayName != null)
                {
                    emo[clsAnnouncement, "DisplayName"].Value = _DisplayName;
                }
                emo[clsAnnouncement, "Title"].Value = _DisplayName;

                if (_Body != null)
                {
                    emo[clsAnnouncement, "Body"].Value = _Body;
                }
                emo[clsAnnouncement, "Priority"].Value       = enumPriority.Id;
                emo[clsAnnouncement, "ExpirationDate"].Value = _ExpirationDate;

                emo.Commit();
                if (_passThru)
                {
                    WriteObject(ServiceManagerObjectHelper.AdaptManagementObject(this, _mg.EntityObjects.GetObject <EnterpriseManagementObject>(emo.Id, ObjectQueryOptions.Default)));
                }
            }
            catch (Exception)
            {
            }
        }
Пример #4
0
 protected override void ProcessRecord()
 {
     try
     {
         ManagementPackClass clsAnnouncement = SMHelpers.GetManagementPackClass(ClassTypes.System_Announcement_Item, SMHelpers.GetManagementPack(ManagementPacks.System_AdminItem_Library, _mg), _mg);
         foreach (EnterpriseManagementObject emo in _mg.EntityObjects.GetObjectReader <EnterpriseManagementObject>(clsAnnouncement, ObjectQueryOptions.Default))
         {
             WriteObject(ServiceManagerObjectHelper.AdaptManagementObject(this, emo));
         }
     }
     catch (Exception)
     {
     }
 }
Пример #5
0
 protected override void BeginProcessing()
 {
     base.BeginProcessing();
     if (Bulk)
     {
         idd = new IncrementalDiscoveryData();
     }
     // If you can't get the prefix, just prepend 'IR'
     try
     {
         ManagementPackClass        incidentSettings         = SMHelpers.GetManagementPackClass("System.WorkItem.Incident.GeneralSetting", SMHelpers.GetManagementPack("ServiceManager.IncidentManagement.Library", _mg), _mg);
         EnterpriseManagementObject incidentSettingsInstance = _mg.EntityObjects.GetObject <EnterpriseManagementObject>(incidentSettings.Id, ObjectQueryOptions.Default);
         incidentPrefix = incidentSettingsInstance[null, "PrefixForId"].Value.ToString() + "{0}";
     }
     catch
     {
         // do nothing - incidentPrefix was already set
         ;
     }
 }
Пример #6
0
        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);
                    //}
                }
            }
        }
Пример #7
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));
            }
        }