public override void ExecuteCmdlet()
{
Schedule inputSchedule = null;
try
{
inputSchedule = DataServiceClient.Schedule.GetResource(
ResourceGroupName,
LabName,
PolicyName);
}
catch (CloudException ex)
{
if (ex.Response.StatusCode != System.Net.HttpStatusCode.NotFound
|| Time == null)
{
throw;
}
}
// Do nothing if user cancelled the operation
var actionDescription = string.Format(CultureInfo.CurrentCulture,
Resources.SavePolicyDescription,
PolicyName, LabName);
string actionWarning;
if (inputSchedule == null)
{
inputSchedule = new Schedule
{
TimeZoneId = TimeZoneInfo.Local.Id,
TaskType = TaskType.LabVmsStartupTask,
WeeklyRecurrence = new WeekDetails
{
Time = Time.Value.ToString("HHmm"),
Weekdays = Days == null ? null : Days.Select(i => i.ToString()).ToList()
},
Status = Disable ? PolicyStatus.Disabled : PolicyStatus.Enabled
};
actionWarning = string.Format(CultureInfo.CurrentCulture,
Resources.CreatePolicyWarning,
PolicyName);
}
else
{
actionWarning = string.Format(CultureInfo.CurrentCulture,
Resources.AlterPolicyWarning,
PolicyName);
if (Time.HasValue)
{
inputSchedule.WeeklyRecurrence.Time = Time.Value.ToString("HHmm");
}
if (Days != null)
{
inputSchedule.WeeklyRecurrence.Weekdays = Days.Select(i => i.ToString()).ToList();
}
if (Disable)
{
inputSchedule.Status = PolicyStatus.Disabled;
}
if (Enable)
{
inputSchedule.Status = PolicyStatus.Enabled;
}
}
if (!ShouldProcess(
actionDescription,
actionWarning,
Resources.ShouldProcessCaption))
{
return;
}
var outputSchedule = DataServiceClient.Schedule.CreateOrUpdateResource(
ResourceGroupName,
LabName,
PolicyName,
inputSchedule
);
WriteObject(outputSchedule.DuckType<PSSchedule>());
}
}