Src/Announcement.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Collections;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

namespace SMLets
{
    [Cmdlet(VerbsCommon.New, "SCSMAnnouncement", SupportsShouldProcess = true)]
    public class AddAnnouncement : SMCmdletBase
    {
       
        #region Parameters
        private String _DisplayName = null;
        private String _Body = null;
        private String _Priority = null;
        private DateTime _ExpirationDate;
        
        [Parameter(Position = 0,
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The title of the annoucncement.")]
        [ValidateNotNullOrEmpty]
        public string DisplayName
        {
            get{return _DisplayName;}
            set{_DisplayName = value;}
        }
        
        [Parameter(Position = 1,
        Mandatory = true,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The body of the announcement")]
        [ValidateNotNullOrEmpty]
        public string Body
        {
            get{return _Body;}
            set{_Body = value;}
        }

        [Parameter(Position = 2,
        Mandatory = false,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The priority of the announcement. Must be exactly 'Low', 'Medium', or 'Critical'.")]
        [ValidateNotNullOrEmpty]
        [ValidateSet("Low","Medium","Critical")]
        public string Priority
        {
            get { return _Priority; }
            set { _Priority = value; }
        }

        [Parameter(Position = 3,
        Mandatory = true,
        ValueFromPipelineByPropertyName = true,
        HelpMessage = "The expiration date of the announcement. Pass a datetime object. Convert to UTC time first. Required.")]
        [ValidateNotNullOrEmpty]
        public DateTime ExpirationDate
        {
            get { return _ExpirationDate; }
            set { _ExpirationDate = value; }
        }

        private SwitchParameter _passThru;
        [Parameter]
        public SwitchParameter PassThru
        {
            get { return _passThru; }
            set { _passThru = value; }
        }


        #endregion

        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)
            {
            }
        }

    }

    [Cmdlet(VerbsCommon.Get, "SCSMAnnouncement", SupportsShouldProcess = true)]
    public class GetAnnouncement : SMCmdletBase
    {

        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)
            {
            }
        }
    }

    [Cmdlet(VerbsCommon.Set, "SCSMAnnouncement", SupportsShouldProcess = true)]
    public class SetAnnouncement : SMCmdletBase
    {

        #region Parameters
        private String _DisplayName = null;
        private String _Body = null;
        private String _Priority = null;
        private DateTime? _ExpirationDate = null;
        private String _InternalID = null;

        [Parameter(Position = 0, HelpMessage = "The title of the announcement.")]
        public string DisplayName
        {
            get { return _DisplayName; }
            set { _DisplayName = value; }
        }

        [Parameter(Position = 1, HelpMessage = "The body of the announcement")]
        public string Body
        {
            get { return _Body; }
            set { _Body = value; }
        }

        [Parameter(Position = 2, HelpMessage = "The priority of the announcement. Must be exactly 'Low', 'Medium', or 'Critical'.")]
        [ValidateSet("Low","Medium","Critical")]
        public string Priority
        {
            get { return _Priority; }
            set { _Priority = value; }
        }

        [Parameter(Position = 3, HelpMessage = "The expiration date of the announcement. Pass a datetime object. Convert to UTC time first.")]
        public DateTime? ExpirationDate
        {
            get { return _ExpirationDate; }
            set { _ExpirationDate = value; }
        }

        [Parameter(Position = 4,
        Mandatory = true,
        ParameterSetName = "ById",
        HelpMessage = "The internal ID (GUID) of the announcement.")]
        public String InternalID
        {
            get { return _InternalID; }
            set { _InternalID = value; }
        }

        private EnterpriseManagementObject _announcement = null;
        [Parameter(ValueFromPipeline=true,
            Mandatory=true,
            ParameterSetName = "ByObject")]
        public EnterpriseManagementObject Announcement
        {
            get { return _announcement; }
            set { _announcement = value; }
        }

        private SwitchParameter _passThru;
        [Parameter]
        public SwitchParameter PassThru
        {
            get { return _passThru; }
            set { _passThru = value; }
        }

        #endregion
    

        private ManagementPackEnumeration enumPriority = null;
        private ManagementPackClass clsAnnouncement = null;
        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));
                }
            }
        }

        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"));
            }
        }

    }
}