1313// ----------------------------------------------------------------------------------
1414
1515using System ;
16+ using System . Collections . Generic ;
17+ using System . Linq ;
1618using System . Management . Automation ;
1719using System . Security . Permissions ;
1820using Microsoft . Azure . Commands . Automation . Common ;
@@ -57,6 +59,30 @@ public NewAzureAutomationSchedule()
5759 [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , HelpMessage = "The schedule description." ) ]
5860 public string Description { get ; set ; }
5961
62+ /// <summary>
63+ /// Gets or sets the schedule days of the week.
64+ /// </summary>
65+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByWeekly , Mandatory = false , HelpMessage = "The list of days of week for the weekly schedule." ) ]
66+ public DayOfWeek [ ] DaysOfWeek { get ; set ; }
67+
68+ /// <summary>
69+ /// Gets or sets the schedule days of the month.
70+ /// </summary>
71+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDaysOfMonth , Mandatory = false , HelpMessage = "The list of days of month for the monthly schedule." ) ]
72+ public DaysOfMonth [ ] DaysOfMonth { get ; set ; }
73+
74+ /// <summary>
75+ /// Gets or sets the schedule day of the week.
76+ /// </summary>
77+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = false , HelpMessage = "The day of week for the monthly occurrence." ) ]
78+ public DayOfWeek ? DayOfWeek { get ; set ; }
79+
80+ /// <summary>
81+ /// Gets or sets the schedule day of the week.
82+ /// </summary>
83+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = false , HelpMessage = "The Occurrence of the week within the month." ) ]
84+ public DayOfWeekOccurrence DayOfWeekOccurrence { get ; set ; }
85+
6086 /// <summary>
6187 /// Gets or sets the switch parameter to create a one time schedule.
6288 /// </summary>
@@ -68,6 +94,9 @@ public NewAzureAutomationSchedule()
6894 /// </summary>
6995 [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByDaily , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
7096 [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByHourly , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
97+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByWeekly , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
98+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDaysOfMonth , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
99+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = false , HelpMessage = "The schedule expiry time." ) ]
71100 public DateTimeOffset ExpiryTime { get ; set ; }
72101
73102 /// <summary>
@@ -84,6 +113,21 @@ public NewAzureAutomationSchedule()
84113 [ ValidateRange ( 1 , byte . MaxValue ) ]
85114 public byte HourInterval { get ; set ; }
86115
116+ /// <summary>
117+ /// Gets or sets the weekly schedule week interval.
118+ /// </summary>
119+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByWeekly , Mandatory = true , HelpMessage = "The weekly schedule week interval." ) ]
120+ [ ValidateRange ( 1 , byte . MaxValue ) ]
121+ public byte WeekInterval { get ; set ; }
122+
123+ /// <summary>
124+ /// Gets or sets the weekly schedule week interval.
125+ /// </summary>
126+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDaysOfMonth , Mandatory = true , HelpMessage = "The monthly schedule month interval." ) ]
127+ [ Parameter ( ParameterSetName = AutomationCmdletParameterSets . ByMonthlyDayOfWeek , Mandatory = true , HelpMessage = "The monthly schedule month interval." ) ]
128+ [ ValidateRange ( 1 , byte . MaxValue ) ]
129+ public byte MonthInterval { get ; set ; }
130+
87131 /// <summary>
88132 /// Gets or sets the schedule time zone.
89133 /// </summary>
@@ -118,10 +162,127 @@ protected override void AutomationProcessRecord()
118162 schedule . Frequency = ScheduleFrequency . Hour ;
119163 schedule . Interval = this . HourInterval ;
120164 break ;
165+ case AutomationCmdletParameterSets . ByWeekly :
166+ schedule = this . CreateWeeklyScheduleModel ( ) ;
167+ break ;
168+ case AutomationCmdletParameterSets . ByMonthlyDayOfWeek :
169+ schedule = this . CreateMonthlyScheduleModel ( ) ;
170+ break ;
171+ case AutomationCmdletParameterSets . ByMonthlyDaysOfMonth :
172+ schedule = this . CreateMonthlyScheduleModel ( ) ;
173+ break ;
121174 }
122175
123- var createdSchedule = this . AutomationClient . CreateSchedule ( this . ResourceGroupName , this . AutomationAccountName , schedule ) ;
176+ Schedule createdSchedule = this . AutomationClient . CreateSchedule ( this . ResourceGroupName , this . AutomationAccountName , schedule ) ;
124177 this . WriteObject ( createdSchedule ) ;
125178 }
179+
180+ /// <summary>
181+ /// The validate.
182+ /// </summary>
183+ /// <returns>
184+ /// The <see cref="Schedule"/>.
185+ /// </returns>
186+ /// <exception cref="Exception">
187+ /// throws exception
188+ /// </exception>
189+ private Schedule CreateMonthlyScheduleModel ( )
190+ {
191+ var dayOfWeek = this . DayOfWeek . HasValue ? this . DayOfWeek . ToString ( ) : null ;
192+ if ( ( ! string . IsNullOrWhiteSpace ( dayOfWeek ) && this . DayOfWeekOccurrence == 0 ) || ( string . IsNullOrWhiteSpace ( dayOfWeek ) && this . DayOfWeekOccurrence != 0 ) )
193+ {
194+ throw new Exception ( "for monthly occurrence, both day of week and occurrence need to be specified" ) ;
195+ }
196+
197+ var newSchedule = new Schedule
198+ {
199+ Name = this . Name ,
200+ StartTime = this . StartTime ,
201+ Description = this . Description ,
202+ ExpiryTime = this . ExpiryTime ,
203+ Frequency = ScheduleFrequency . Month ,
204+ Interval = this . MonthInterval ,
205+ DaysOfMonth = this . DaysOfMonth ,
206+ DayOfWeekMonthlySchedule = dayOfWeek ,
207+ DayOfWeekOccurrence = this . DayOfWeekOccurrence == 0 ? null : this . DayOfWeekOccurrence . ToString ( )
208+ } ;
209+
210+ return newSchedule ;
211+ }
212+
213+ /// <summary>
214+ /// The create weekly schedule model.
215+ /// </summary>
216+ /// <returns>
217+ /// The <see cref="Schedule"/>.
218+ /// </returns>
219+ private Schedule CreateWeeklyScheduleModel ( )
220+ {
221+ var newSchedule = new Schedule
222+ {
223+ Name = this . Name ,
224+ StartTime = this . StartTime ,
225+ Description = this . Description ,
226+ ExpiryTime = this . ExpiryTime ,
227+ Frequency = ScheduleFrequency . Week ,
228+ Interval = this . WeekInterval ,
229+ DaysOfWeekWeeklySchedule = this . DaysOfWeek == null
230+ ? null
231+ : this . DaysOfWeek . Select ( day => day . ToString ( ) ) . ToList ( )
232+ } ;
233+
234+ return newSchedule ;
235+ }
236+ }
237+
238+ /// <summary>
239+ /// The day of week occurrence.
240+ /// </summary>
241+ public enum DayOfWeekOccurrence
242+ {
243+ First = 1 ,
244+ Second = 2 ,
245+ Third = 3 ,
246+ Fourth = 4 ,
247+ Last = - 1
248+ }
249+
250+ /// <summary>
251+ /// The day of week occurrence.
252+ /// </summary>
253+ public enum DaysOfMonth
254+ {
255+ One = 1 ,
256+ Two = 2 ,
257+ Three = 3 ,
258+ Four = 4 ,
259+ Five = 5 ,
260+ Six = 6 ,
261+ Seventh = 7 ,
262+ Eighth = 8 ,
263+ Ninth = 9 ,
264+ Tenth = 10 ,
265+ Eleventh = 11 ,
266+ Twelfth = 12 ,
267+ Thirteenth = 13 ,
268+ Fourteenth = 14 ,
269+ Fifteenth = 15 ,
270+ Sixteenth = 16 ,
271+ Seventeenth = 17 ,
272+ Eighteenth = 18 ,
273+ Nineteenth = 19 ,
274+ Twentieth = 20 ,
275+ TwentyFirst = 21 ,
276+ TwentySecond = 22 ,
277+ TwentyThird = 23 ,
278+ TwentyFourth = 24 ,
279+ TwentyFifth = 25 ,
280+ TwentySixth = 26 ,
281+ TwentySeventh = 27 ,
282+ TwentyEighth = 28 ,
283+ TwentyNinth = 29 ,
284+ Thirtieth = 30 ,
285+ ThirtyFirst = 31 ,
286+ LastDay = - 1
126287 }
127288}
0 commit comments