Src/subscription.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
using System;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Subscriptions;
using Microsoft.EnterpriseManagement.Configuration.IO;
using System.Text.RegularExpressions;

namespace SMLets
{
    [Cmdlet("Get","SCSMSubscription")]
    public class GetSMSubscriptionCommand : SMCmdletBase
    {
        private string _filter = "Name LIKE '%'";
        [Parameter(ValueFromPipeline=true)]
        public string Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }
        protected override void ProcessRecord()
        {
            ManagementPackRuleCriteria criteria = new ManagementPackRuleCriteria(Filter);
            foreach (WorkflowSubscriptionBase ws in _mg.Subscription.GetSubscriptionsByCriteria(criteria))
            {
                WriteObject(ws);
            }

        }
    }
    
    [Cmdlet("Remove","SCSMSubscription", SupportsShouldProcess=true)]
    public class RemoveSMSubscriptionCommand : SMCmdletBase
    {
        private WorkflowSubscription _subscription;
        [Parameter(Position=0,Mandatory=true,ValueFromPipeline=true)]
        public WorkflowSubscription Subscription
        {
            get { return _subscription; }
            set { _subscription = value; }
        }
        protected override void ProcessRecord()
        {
            try
            {
                if ( ShouldProcess(_subscription.Name))
                {
                    _subscription.ManagementGroup.Subscription.DeleteSubscription(_subscription);
                }
            }
            catch ( Exception e )
            {
                WriteError(new ErrorRecord(e, "Unknown error", ErrorCategory.NotSpecified, _subscription));
            }

        }
    }

    [Cmdlet("New", "SCSMNotificationSubscription")]
    public class NewSMNotificationSubscriptionCommand : SMCmdletBase
    {
        private string _criteria = String.Empty;
        private string _displayname = String.Empty;
        private string _description = String.Empty;
        private OperationTypeEnum _operationtypeenum;
        private OperationType _operationtype;
        private ManagementPackClass _class;
        private EnterpriseManagementObject[] _recipients;
        private ManagementPack _managementpack;
        private ManagementPackObjectTemplate _template;
        private bool _enabled = true; 
        
        
        public enum OperationTypeEnum
        {
            Add,
            Update
            //Remove - note supported for now
        }

        [Parameter(ValueFromPipeline = false, Mandatory=false)]
        public string Criteria
        {
            get { return _criteria; }
            set { _criteria = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public OperationTypeEnum OperationType
        {
            get { return _operationtypeenum; }
            set { _operationtypeenum = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public ManagementPackClass Class
        {
            get { return _class; }
            set { _class = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public string DisplayName
        {
            get { return _displayname; }
            set { _displayname = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = false)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = false)]
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = false)]
        public EnterpriseManagementObject[] Recipients
        {
            get { return _recipients; }
            set { _recipients = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public ManagementPack ManagementPack
        {
            get { return _managementpack; }
            set { _managementpack = value; }
        }

        [Parameter(ValueFromPipeline = false, Mandatory = true)]
        public ManagementPackObjectTemplate NotificationTemplate
        {
            get { return _template; }
            set { _template = value; }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            switch(_operationtypeenum)
            {
                case OperationTypeEnum.Add:
                    _operationtype = Microsoft.EnterpriseManagement.Subscriptions.OperationType.Add;
                    break;
                case OperationTypeEnum.Update:
                    _operationtype = Microsoft.EnterpriseManagement.Subscriptions.OperationType.Update;
                    break;
            }
        }

        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            //Create subscription and set configurable properties
            InstanceTypeSubscription instancetypeSubscription = new InstanceTypeSubscription(_operationtype,_class.Id, _criteria );   
            NotificationSubscription subscription = new NotificationSubscription(_displayname, _description, instancetypeSubscription);
            subscription.Enabled = _enabled;
            subscription.TemplateIds.Add(_template.Id);
            subscription.Name = SMHelpers.MakeMPElementSafeUniqueIdentifier("NotificationSubscription");
            
            //TODO: Do we need these or do they have defaults set?
            //subscription.MaximumRunningTimeSeconds = 7200;
            //subscription.RetryDelaySeconds = 60;
            //subscription.EnableBatchProcessing = true;
            
            //Add recipient users
            if(_recipients != null)
            {
                foreach(EnterpriseManagementObject recipient in _recipients)
                {
                    subscription.Recipients.Add(new NotificationSubscriptionRecipient(recipient.Id.ToString(), NotificationSubscriptionRecipientType.ToRecipient));
                }
            }

            _mg.Subscription.InsertSubscription(_managementpack.Id, subscription);
        }
    }
}