12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using Microsoft . Azure . Commands . Automation . Common ;
16
- using Microsoft . Azure . Commands . Automation . Model ;
17
15
using System ;
16
+ using System . Collections . Generic ;
17
+ using System . Linq ;
18
18
using System . Management . Automation ;
19
19
using System . Security . Permissions ;
20
+ using Microsoft . Azure . Commands . Automation . Common ;
21
+ using Microsoft . Azure . Commands . Automation . Model ;
22
+ using Microsoft . Azure . Commands . Automation . Properties ;
23
+ using DayOfWeek = Microsoft . Azure . Commands . Automation . Model . DayOfWeek ;
20
24
21
25
namespace Microsoft . Azure . Commands . Automation . Cmdlet
22
26
{
@@ -57,6 +61,30 @@ public NewAzureAutomationSchedule()
57
61
[ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , HelpMessage = "The schedule description." ) ]
58
62
public string Description { get ; set ; }
59
63
64
+ /// <summary>
65
+ /// Gets or sets the schedule days of the week.
66
+ /// </summary>
67
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByWeekly , Mandatory = false , HelpMessage = "The list of days of week for the weekly schedule." ) ]
68
+ public System . DayOfWeek [ ] DaysOfWeek { get ; set ; }
69
+
70
+ /// <summary>
71
+ /// Gets or sets the schedule days of the month.
72
+ /// </summary>
73
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDaysOfMonth , Mandatory = false , HelpMessage = "The list of days of month for the monthly schedule." ) ]
74
+ public DaysOfMonth [ ] DaysOfMonth { get ; set ; }
75
+
76
+ /// <summary>
77
+ /// Gets or sets the schedule day of the week.
78
+ /// </summary>
79
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = false , HelpMessage = "The day of week for the monthly occurrence." ) ]
80
+ public System . DayOfWeek ? DayOfWeek { get ; set ; }
81
+
82
+ /// <summary>
83
+ /// Gets or sets the schedule day of the week.
84
+ /// </summary>
85
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = false , HelpMessage = "The Occurrence of the week within the month." ) ]
86
+ public DayOfWeekOccurrence DayOfWeekOccurrence { get ; set ; }
87
+
60
88
/// <summary>
61
89
/// Gets or sets the switch parameter to create a one time schedule.
62
90
/// </summary>
@@ -68,6 +96,9 @@ public NewAzureAutomationSchedule()
68
96
/// </summary>
69
97
[ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByDaily , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
70
98
[ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByHourly , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
99
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByWeekly , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
100
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDaysOfMonth , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
101
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
71
102
public DateTimeOffset ExpiryTime { get ; set ; }
72
103
73
104
/// <summary>
@@ -84,6 +115,27 @@ public NewAzureAutomationSchedule()
84
115
[ ValidateRange ( 1 , byte . MaxValue ) ]
85
116
public byte HourInterval { get ; set ; }
86
117
118
+ /// <summary>
119
+ /// Gets or sets the weekly schedule week interval.
120
+ /// </summary>
121
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByWeekly , Mandatory = true , HelpMessage = "The weekly schedule week interval." ) ]
122
+ [ ValidateRange ( 1 , byte . MaxValue ) ]
123
+ public byte WeekInterval { get ; set ; }
124
+
125
+ /// <summary>
126
+ /// Gets or sets the weekly schedule week interval.
127
+ /// </summary>
128
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDaysOfMonth , Mandatory = true , HelpMessage = "The monthly schedule month interval." ) ]
129
+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = true , HelpMessage = "The monthly schedule month interval." ) ]
130
+ [ ValidateRange ( 1 , byte . MaxValue ) ]
131
+ public byte MonthInterval { get ; set ; }
132
+
133
+ /// <summary>
134
+ /// Gets or sets the schedule time zone.
135
+ /// </summary>
136
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , HelpMessage = "The schedule time zone." ) ]
137
+ public string TimeZone { get ; set ; }
138
+
87
139
/// <summary>
88
140
/// Execute this cmdlet.
89
141
/// </summary>
@@ -95,26 +147,168 @@ protected override void AutomationProcessRecord()
95
147
Name = this . Name ,
96
148
StartTime = this . StartTime ,
97
149
Description = this . Description ,
98
- ExpiryTime = this . ExpiryTime
150
+ ExpiryTime = this . ExpiryTime ,
151
+ TimeZone = this . TimeZone ,
99
152
} ;
100
153
101
- if ( this . ParameterSetName == AutomationCmdletParameterSets . ByOneTime )
154
+ switch ( this . ParameterSetName )
102
155
{
103
- schedule . Frequency = ScheduleFrequency . Onetime ;
156
+ case AutomationCmdletParameterSets . ByOneTime :
157
+ schedule . Frequency = ScheduleFrequency . Onetime ;
158
+ break ;
159
+ case AutomationCmdletParameterSets . ByDaily :
160
+ schedule . Frequency = ScheduleFrequency . Day ;
161
+ schedule . Interval = this . DayInterval ;
162
+ break ;
163
+ case AutomationCmdletParameterSets . ByHourly :
164
+ schedule . Frequency = ScheduleFrequency . Hour ;
165
+ schedule . Interval = this . HourInterval ;
166
+ break ;
167
+ case AutomationCmdletParameterSets . ByWeekly :
168
+ schedule = this . CreateWeeklyScheduleModel ( ) ;
169
+ break ;
170
+ case AutomationCmdletParameterSets . ByMonthlyDayOfWeek :
171
+ schedule = this . CreateMonthlyScheduleModel ( ) ;
172
+ break ;
173
+ case AutomationCmdletParameterSets . ByMonthlyDaysOfMonth :
174
+ schedule = this . CreateMonthlyScheduleModel ( ) ;
175
+ break ;
104
176
}
105
- else if ( this . ParameterSetName == AutomationCmdletParameterSets . ByDaily )
177
+
178
+ Schedule createdSchedule = this . AutomationClient . CreateSchedule ( this . ResourceGroupName , this . AutomationAccountName , schedule ) ;
179
+ this . WriteObject ( createdSchedule ) ;
180
+ }
181
+
182
+ /// <summary>
183
+ /// The validate.
184
+ /// </summary>
185
+ /// <returns>
186
+ /// The <see cref="Schedule"/>.
187
+ /// </returns>
188
+ /// <exception cref="Exception">
189
+ /// throws exception
190
+ /// </exception>
191
+ private Schedule CreateMonthlyScheduleModel ( )
192
+ {
193
+ var dayOfWeek = this . DayOfWeek . HasValue ? this . DayOfWeek . ToString ( ) : null ;
194
+ if ( ( ! string . IsNullOrWhiteSpace ( dayOfWeek ) && this . DayOfWeekOccurrence == 0 ) || ( string . IsNullOrWhiteSpace ( dayOfWeek ) && this . DayOfWeekOccurrence != 0 ) )
106
195
{
107
- schedule . Frequency = ScheduleFrequency . Day ;
108
- schedule . Interval = this . DayInterval ;
196
+ throw new ArgumentException ( Resources . MonthlyScheduleNeedsDayOfWeekAndOccurrence ) ;
109
197
}
110
- else if ( this . ParameterSetName == AutomationCmdletParameterSets . ByHourly )
198
+
199
+ var newSchedule = new Schedule
111
200
{
112
- schedule . Frequency = ScheduleFrequency . Hour ;
113
- schedule . Interval = this . HourInterval ;
114
- }
201
+ Name = this . Name ,
202
+ StartTime = this . StartTime ,
203
+ Description = this . Description ,
204
+ ExpiryTime = this . ExpiryTime ,
205
+ Frequency = ScheduleFrequency . Month ,
206
+ Interval = this . MonthInterval ,
207
+ MonthlyScheduleOptions = this . IsMonthlyScheduleNull ( )
208
+ ? null
209
+ : new MonthlyScheduleOptions ( )
210
+ {
211
+ DaysOfMonth = this . DaysOfMonth ,
212
+ DayOfWeek = this . DayOfWeek == null && this . DayOfWeekOccurrence == 0
213
+ ? null
214
+ : new DayOfWeek ( )
215
+ {
216
+ Day = dayOfWeek ,
217
+ Occurrence = this . DayOfWeekOccurrence == 0 ? null : this . DayOfWeekOccurrence . ToString ( )
218
+ }
219
+ }
220
+ } ;
115
221
116
- Schedule createdSchedule = this . AutomationClient . CreateSchedule ( this . ResourceGroupName , this . AutomationAccountName , schedule ) ;
117
- this . WriteObject ( createdSchedule ) ;
222
+ return newSchedule ;
223
+ }
224
+
225
+ /// <summary>
226
+ /// The is monthly schedule null.
227
+ /// </summary>
228
+ /// <returns>
229
+ /// The <see cref="bool"/>.
230
+ /// </returns>
231
+ private bool IsMonthlyScheduleNull ( )
232
+ {
233
+ return this . DaysOfMonth == null && this . DayOfWeek == null && this . DayOfWeekOccurrence == 0 ;
234
+ }
235
+
236
+ /// <summary>
237
+ /// The create weekly schedule model.
238
+ /// </summary>
239
+ /// <returns>
240
+ /// The <see cref="Schedule"/>.
241
+ /// </returns>
242
+ private Schedule CreateWeeklyScheduleModel ( )
243
+ {
244
+ var newSchedule = new Schedule
245
+ {
246
+ Name = this . Name ,
247
+ StartTime = this . StartTime ,
248
+ Description = this . Description ,
249
+ ExpiryTime = this . ExpiryTime ,
250
+ Frequency = ScheduleFrequency . Week ,
251
+ Interval = this . WeekInterval ,
252
+ WeeklyScheduleOptions = this . DaysOfWeek == null
253
+ ? null
254
+ : new WeeklyScheduleOptions ( )
255
+ {
256
+ DaysOfWeek = this . DaysOfWeek . Select ( day => day . ToString ( ) ) . ToList ( )
257
+ }
258
+ } ;
259
+
260
+ return newSchedule ;
118
261
}
119
262
}
263
+
264
+ /// <summary>
265
+ /// The day of week occurrence.
266
+ /// </summary>
267
+ public enum DayOfWeekOccurrence
268
+ {
269
+ First = 1 ,
270
+ Second = 2 ,
271
+ Third = 3 ,
272
+ Fourth = 4 ,
273
+ Last = - 1
274
+ }
275
+
276
+ /// <summary>
277
+ /// The day of week occurrence.
278
+ /// </summary>
279
+ public enum DaysOfMonth
280
+ {
281
+ One = 1 ,
282
+ Two = 2 ,
283
+ Three = 3 ,
284
+ Four = 4 ,
285
+ Five = 5 ,
286
+ Six = 6 ,
287
+ Seventh = 7 ,
288
+ Eighth = 8 ,
289
+ Ninth = 9 ,
290
+ Tenth = 10 ,
291
+ Eleventh = 11 ,
292
+ Twelfth = 12 ,
293
+ Thirteenth = 13 ,
294
+ Fourteenth = 14 ,
295
+ Fifteenth = 15 ,
296
+ Sixteenth = 16 ,
297
+ Seventeenth = 17 ,
298
+ Eighteenth = 18 ,
299
+ Nineteenth = 19 ,
300
+ Twentieth = 20 ,
301
+ TwentyFirst = 21 ,
302
+ TwentySecond = 22 ,
303
+ TwentyThird = 23 ,
304
+ TwentyFourth = 24 ,
305
+ TwentyFifth = 25 ,
306
+ TwentySixth = 26 ,
307
+ TwentySeventh = 27 ,
308
+ TwentyEighth = 28 ,
309
+ TwentyNinth = 29 ,
310
+ Thirtieth = 30 ,
311
+ ThirtyFirst = 31 ,
312
+ LastDay = - 1
313
+ }
120
314
}
0 commit comments