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) { } }
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) { } }
protected override void ProcessRecord() { try { if (_announcement != null) { WriteVerbose("Setting by instance"); bool change = false; if (_DisplayName != null) { WriteDebug("Setting instance Name"); _announcement[null, "DisplayName"].Value = _DisplayName; _announcement[null, "Title"].Value = _DisplayName; change = true; } if (_Body != null) { WriteDebug("Setting instance Body"); _announcement[null, "Body"].Value = _Body; change = true; } if (_Priority != null) { WriteDebug("Setting instance Priority"); _announcement[null, "Priority"].Value = enumPriority.Id; change = true; } if (_ExpirationDate != null) { WriteDebug("Setting instance ExpirationDate"); _announcement[null, "ExpirationDate"].Value = _ExpirationDate; change = true; } if (change) { WriteDebug("Overwriting instance"); _announcement.Overwrite(); if (_passThru) { WriteObject(ServiceManagerObjectHelper.AdaptManagementObject(this, _announcement)); } } } else if (_InternalID != null) { bool change = false; WriteVerbose("Setting by ID"); EnterpriseManagementObject emo = _mg.EntityObjects.GetObject <EnterpriseManagementObject>(new Guid(_InternalID), ObjectQueryOptions.Default); if (_DisplayName != null) { emo[clsAnnouncement, "DisplayName"].Value = _DisplayName; emo[clsAnnouncement, "Title"].Value = _DisplayName; change = true; } if (_Body != null) { emo[clsAnnouncement, "Body"].Value = _Body; change = true; } if (_Priority != null) { emo[clsAnnouncement, "Priority"].Value = enumPriority.Id; change = true; } if (_ExpirationDate != null) { emo[clsAnnouncement, "ExpirationDate"].Value = _ExpirationDate; change = true; } if (change) { emo.Overwrite(); if (_passThru) { WriteObject(ServiceManagerObjectHelper.AdaptManagementObject(this, emo)); } } } } catch (Exception exception) { WriteError(new ErrorRecord(exception, "Object", ErrorCategory.NotSpecified, "Announcement")); } }
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); //} } } }