From afeca41a8bb5dc4bc886c43e0830193b64a7cf50 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 1 Oct 2018 14:10:07 +0500 Subject: [PATCH 1/3] Step 1 --- .../commands/SetJobTrigger.cs | 156 ++++++++---------- 1 file changed, 68 insertions(+), 88 deletions(-) diff --git a/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobTrigger.cs b/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobTrigger.cs index 7a365ea12d2..09c20e2ec4a 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobTrigger.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobTrigger.cs @@ -272,23 +272,23 @@ private bool ValidateParameterSet(ref TriggerFrequency newTriggerFrequency) { // First see if a switch parameter was set. List switchParamList = new List(); - if (MyInvocation.BoundParameters.ContainsKey(_paramAtStartup)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(AtStartup))) { switchParamList.Add(TriggerFrequency.AtStartup); } - if (MyInvocation.BoundParameters.ContainsKey(_paramAtLogon)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(AtLogon))) { switchParamList.Add(TriggerFrequency.AtLogon); } - if (MyInvocation.BoundParameters.ContainsKey(_paramOnce)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(Once))) { switchParamList.Add(TriggerFrequency.Once); } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaily)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(Daily))) { switchParamList.Add(TriggerFrequency.Daily); } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeekly)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(Weekly))) { switchParamList.Add(TriggerFrequency.Weekly); } @@ -338,38 +338,38 @@ private bool ValidateParameterSet(ref TriggerFrequency newTriggerFrequency) private bool ValidateStartupParams() { - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysInterval, ScheduledJobErrorStrings.TriggerStartUpType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidWeeksInterval, ScheduledJobErrorStrings.TriggerStartUpType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(At))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidAtTime, ScheduledJobErrorStrings.TriggerStartUpType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramUser)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(User))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerStartUpType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysOfWeek, ScheduledJobErrorStrings.TriggerStartUpType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) || MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) || - MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) || + MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidSetTriggerRepetition, ScheduledJobErrorStrings.TriggerStartUpType); WriteValidationError(msg); @@ -381,32 +381,32 @@ private bool ValidateStartupParams() private bool ValidateLogonParams() { - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysInterval, ScheduledJobErrorStrings.TriggerLogonType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidWeeksInterval, ScheduledJobErrorStrings.TriggerLogonType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(At))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidAtTime, ScheduledJobErrorStrings.TriggerLogonType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysOfWeek, ScheduledJobErrorStrings.TriggerLogonType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) || MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) || - MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) || + MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidSetTriggerRepetition, ScheduledJobErrorStrings.TriggerLogonType); WriteValidationError(msg); @@ -418,38 +418,38 @@ private bool ValidateLogonParams() private bool ValidateOnceParams(ScheduledJobTrigger trigger = null) { - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysInterval, ScheduledJobErrorStrings.TriggerOnceType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidWeeksInterval, ScheduledJobErrorStrings.TriggerOnceType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramUser)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(User))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerOnceType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysOfWeek, ScheduledJobErrorStrings.TriggerOnceType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration))) { _repDuration = TimeSpan.MaxValue; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) || MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) || - MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) || + MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration))) { // Validate Once trigger repetition parameters. try @@ -465,7 +465,7 @@ private bool ValidateOnceParams(ScheduledJobTrigger trigger = null) if (trigger != null) { - if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(nameof(At))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingAtTime, ScheduledJobErrorStrings.TriggerOnceType); WriteValidationError(msg); @@ -478,32 +478,32 @@ private bool ValidateOnceParams(ScheduledJobTrigger trigger = null) private bool ValidateDailyParams(ScheduledJobTrigger trigger = null) { - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval) && + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval)) && _daysInterval < 1) { WriteValidationError(ScheduledJobErrorStrings.InvalidDaysIntervalParam); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidWeeksInterval, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramUser)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(User))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysOfWeek, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) || MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) || - MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) || + MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidSetTriggerRepetition, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); @@ -512,7 +512,7 @@ private bool ValidateDailyParams(ScheduledJobTrigger trigger = null) if (trigger != null) { - if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(nameof(At))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingAtTime, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); @@ -525,26 +525,26 @@ private bool ValidateDailyParams(ScheduledJobTrigger trigger = null) private bool ValidateWeeklyParams(ScheduledJobTrigger trigger = null) { - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysInterval, ScheduledJobErrorStrings.TriggerWeeklyType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval) && + if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval)) && _weeksInterval < 1) { WriteValidationError(ScheduledJobErrorStrings.InvalidWeeksIntervalParam); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramUser)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(User))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerWeeklyType); WriteValidationError(msg); return false; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) || MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) || - MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) || + MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidSetTriggerRepetition, ScheduledJobErrorStrings.TriggerWeeklyType); WriteValidationError(msg); @@ -553,14 +553,14 @@ private bool ValidateWeeklyParams(ScheduledJobTrigger trigger = null) if (trigger != null) { - if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(nameof(At))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingAtTime, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); return false; } if ((trigger.DaysOfWeek == null || trigger.DaysOfWeek.Count == 0) && - !MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek)) + !MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek))) { string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingDaysOfWeek, ScheduledJobErrorStrings.TriggerDailyType); WriteValidationError(msg); @@ -696,7 +696,7 @@ private bool ModifyTrigger(ScheduledJobTrigger trigger, TriggerFrequency trigger private void ModifyStartupTrigger(ScheduledJobTrigger trigger) { - if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay))) { trigger.RandomDelay = _randomDelay; } @@ -704,12 +704,12 @@ private void ModifyStartupTrigger(ScheduledJobTrigger trigger) private void ModifyLogonTrigger(ScheduledJobTrigger trigger) { - if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay))) { trigger.RandomDelay = _randomDelay; } - if (MyInvocation.BoundParameters.ContainsKey(_paramUser)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(User))) { trigger.User = string.IsNullOrEmpty(_user) ? ScheduledJobTrigger.AllUsers : _user; } @@ -717,22 +717,22 @@ private void ModifyLogonTrigger(ScheduledJobTrigger trigger) private void ModifyOnceTrigger(ScheduledJobTrigger trigger) { - if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay))) { trigger.RandomDelay = _randomDelay; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval))) { trigger.RepetitionInterval = _repInterval; } - if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration))) { trigger.RepetitionDuration = _repDuration; } - if (MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(At))) { trigger.At = _atTime; } @@ -740,17 +740,17 @@ private void ModifyOnceTrigger(ScheduledJobTrigger trigger) private void ModifyDailyTrigger(ScheduledJobTrigger trigger) { - if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay))) { trigger.RandomDelay = _randomDelay; } - if (MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(At))) { trigger.At = _atTime; } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval))) { trigger.Interval = _daysInterval; } @@ -758,22 +758,22 @@ private void ModifyDailyTrigger(ScheduledJobTrigger trigger) private void ModifyWeeklyTrigger(ScheduledJobTrigger trigger) { - if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay))) { trigger.RandomDelay = _randomDelay; } - if (MyInvocation.BoundParameters.ContainsKey(_paramAt)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(At))) { trigger.At = _atTime; } - if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval))) { trigger.Interval = _weeksInterval; } - if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek)) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek))) { trigger.DaysOfWeek = new List(_daysOfWeek); } @@ -791,8 +791,8 @@ private void CreateAtLogonTrigger(ScheduledJobTrigger trigger) trigger.Enabled = enabled; trigger.Id = id; - trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay; - trigger.User = MyInvocation.BoundParameters.ContainsKey(_paramUser) ? _user : user; + trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay; + trigger.User = MyInvocation.BoundParameters.ContainsKey(nameof(User)) ? _user : user; } private void CreateAtStartupTrigger(ScheduledJobTrigger trigger) @@ -806,7 +806,7 @@ private void CreateAtStartupTrigger(ScheduledJobTrigger trigger) trigger.Enabled = enabled; trigger.Id = id; - trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay; + trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay; } private void CreateOnceTrigger(ScheduledJobTrigger trigger) @@ -823,10 +823,10 @@ private void CreateOnceTrigger(ScheduledJobTrigger trigger) trigger.Enabled = enabled; trigger.Id = id; - trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay; - trigger.At = MyInvocation.BoundParameters.ContainsKey(_paramAt) ? _atTime : atTime; - trigger.RepetitionInterval = MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) ? _repInterval : repInterval; - trigger.RepetitionDuration = MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) ? _repDuration : repDuration; + trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay; + trigger.At = MyInvocation.BoundParameters.ContainsKey(nameof(At)) ? _atTime : atTime; + trigger.RepetitionInterval = MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) ? _repInterval : repInterval; + trigger.RepetitionDuration = MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) ? _repDuration : repDuration; } private void CreateDailyTrigger(ScheduledJobTrigger trigger) @@ -842,9 +842,9 @@ private void CreateDailyTrigger(ScheduledJobTrigger trigger) trigger.Enabled = enabled; trigger.Id = id; - trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay; - trigger.At = MyInvocation.BoundParameters.ContainsKey(_paramAt) ? _atTime : atTime; - trigger.Interval = MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval) ? _daysInterval : interval; + trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay; + trigger.At = MyInvocation.BoundParameters.ContainsKey(nameof(At)) ? _atTime : atTime; + trigger.Interval = MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval)) ? _daysInterval : interval; } private void CreateWeeklyTrigger(ScheduledJobTrigger trigger) @@ -861,10 +861,10 @@ private void CreateWeeklyTrigger(ScheduledJobTrigger trigger) trigger.Enabled = enabled; trigger.Id = id; - trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay; - trigger.At = MyInvocation.BoundParameters.ContainsKey(_paramAt) ? _atTime : atTime; - trigger.Interval = MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval) ? _weeksInterval : interval; - trigger.DaysOfWeek = MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek) ? new List(_daysOfWeek) : daysOfWeek; + trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay; + trigger.At = MyInvocation.BoundParameters.ContainsKey(nameof(At)) ? _atTime : atTime; + trigger.Interval = MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval)) ? _weeksInterval : interval; + trigger.DaysOfWeek = MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek)) ? new List(_daysOfWeek) : daysOfWeek; } private void WriteValidationError(string msg) @@ -875,25 +875,5 @@ private void WriteValidationError(string msg) } #endregion - - #region Private Members - - private string _paramAtStartup = "AtStartup"; - private string _paramAtLogon = "AtLogon"; - private string _paramOnce = "Once"; - private string _paramDaily = "Daily"; - private string _paramWeekly = "Weekly"; - // - private string _paramDaysInterval = "DaysInterval"; - private string _paramWeeksInterval = "WeeksInterval"; - private string _paramRandomDelay = "RandomDelay"; - private string _paramRepetitionInterval = "RepetitionInterval"; - private string _paramRepetitionDuration = "RepetitionDuration"; - private string _paramRepetitionInfiniteDuration = "RepeatIndefinitely"; - private string _paramAt = "At"; - private string _paramUser = "User"; - private string _paramDaysOfWeek = "DaysOfWeek"; - - #endregion } } From 9911938db30a9d545a164869c24a12830f2d869d Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 1 Oct 2018 14:10:40 +0500 Subject: [PATCH 2/3] Step 2 --- .../commands/utility/CSVCommands.cs | 2 +- .../utility/EnableDisableRunspaceDebugCommand.cs | 2 +- .../commands/utility/ImplicitRemotingCommands.cs | 12 ++++++------ .../commands/NewJobTrigger.cs | 12 ++++++------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs index 6b14a2cf29a..a37c8f9d00b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs @@ -98,7 +98,7 @@ protected override void BeginProcessing() ErrorRecord errorRecord = new ErrorRecord(exception, "CannotSpecifyIncludeTypeInformationAndNoTypeInformation", ErrorCategory.InvalidData, null); this.ThrowTerminatingError(errorRecord); } - if (this.MyInvocation.BoundParameters.ContainsKey("IncludeTypeInformation")) + if (this.MyInvocation.BoundParameters.ContainsKey(nameof(IncludeTypeInformation))) { NoTypeInformation = !IncludeTypeInformation; } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/EnableDisableRunspaceDebugCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/EnableDisableRunspaceDebugCommand.cs index f8981877dff..df47dbdbf13 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/EnableDisableRunspaceDebugCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/EnableDisableRunspaceDebugCommand.cs @@ -389,7 +389,7 @@ protected override void ProcessRecord() // Enable debugging by preserving debug stop events. debugger.UnhandledBreakpointMode = UnhandledBreakpointProcessingMode.Wait; - if (this.MyInvocation.BoundParameters.ContainsKey("BreakAll")) + if (this.MyInvocation.BoundParameters.ContainsKey(nameof(BreakAll))) { if (BreakAll) { diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs index 422063f8d8d..60a0b7f37e6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs @@ -115,7 +115,7 @@ protected override void BeginProcessing() // Throw out terminating error if this is the case. if (IsModuleSpecified && IsFullyQualifiedModuleSpecified) { - string errMsg = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "Module", "FullyQualifiedModule"); + string errMsg = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, nameof(Module), nameof(FullyQualifiedModule)); ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "ModuleAndFullyQualifiedModuleCannotBeSpecifiedTogether", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(error); } @@ -275,7 +275,7 @@ protected override void BeginProcessing() // Throw out terminating error if this is the case. if (IsModuleSpecified && IsFullyQualifiedModuleSpecified) { - string errMsg = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "Module", "FullyQualifiedModule"); + string errMsg = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, nameof(Module), nameof(FullyQualifiedModule)); ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "ModuleAndFullyQualifiedModuleCannotBeSpecifiedTogether", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(error); } @@ -1643,10 +1643,10 @@ private PowerShell BuildPowerShellForGetCommand() { powerShell.AddParameter("Name", this.CommandName); } - powerShell.AddParameter("Module", this.Module); + powerShell.AddParameter(nameof(Module), this.Module); if (IsFullyQualifiedModuleSpecified) { - powerShell.AddParameter("FullyQualifiedModule", this.FullyQualifiedModule); + powerShell.AddParameter(nameof(FullyQualifiedModule), this.FullyQualifiedModule); } powerShell.AddParameter("ArgumentList", this.ArgumentList); @@ -2405,9 +2405,9 @@ private string GenerateReimportingOfModules() { StringBuilder result = new StringBuilder(); - if (_invocationInfo.BoundParameters.ContainsKey("Module")) + if (_invocationInfo.BoundParameters.ContainsKey(nameof(Module))) { - string[] moduleNames = (string[])_invocationInfo.BoundParameters["Module"]; + string[] moduleNames = (string[])_invocationInfo.BoundParameters[nameof(Module)]; foreach (string moduleName in moduleNames) { result.AppendFormat( diff --git a/src/Microsoft.PowerShell.ScheduledJob/commands/NewJobTrigger.cs b/src/Microsoft.PowerShell.ScheduledJob/commands/NewJobTrigger.cs index f7a83d88c92..4f134ef70d5 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/commands/NewJobTrigger.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/commands/NewJobTrigger.cs @@ -272,22 +272,22 @@ private void CreateOnceTrigger() { TimeSpan? repInterval = null; TimeSpan? repDuration = null; - if (MyInvocation.BoundParameters.ContainsKey("RepetitionInterval") || MyInvocation.BoundParameters.ContainsKey("RepetitionDuration") || - MyInvocation.BoundParameters.ContainsKey("RepeatIndefinitely")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) || + MyInvocation.BoundParameters.ContainsKey(nameof(RepeatIndefinitely))) { - if (MyInvocation.BoundParameters.ContainsKey("RepeatIndefinitely")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepeatIndefinitely))) { - if (MyInvocation.BoundParameters.ContainsKey("RepetitionDuration")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration))) { throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepeatIndefinitelyParams); } - if (!MyInvocation.BoundParameters.ContainsKey("RepetitionInterval")) + if (!MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval))) { throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionRepeatParams); } _repDuration = TimeSpan.MaxValue; } - else if (!MyInvocation.BoundParameters.ContainsKey("RepetitionInterval") || !MyInvocation.BoundParameters.ContainsKey("RepetitionDuration")) + else if (!MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || !MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration))) { throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionParams); } From fce2d876abd90017bdc3c1a0149f817b318f6c46 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 1 Oct 2018 14:11:12 +0500 Subject: [PATCH 3/3] [Feature] Step 3 --- .../commands/RegisterJobDefinition.cs | 4 +-- .../commands/ScheduledJobOptionCmdletBase.cs | 4 +-- .../commands/SetJobDefinition.cs | 8 +++--- .../commands/SetScheduledJobOption.cs | 26 +++++++++---------- .../engine/remoting/commands/GetJob.cs | 12 ++++----- .../remoting/commands/InvokeCommandCommand.cs | 2 +- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.PowerShell.ScheduledJob/commands/RegisterJobDefinition.cs b/src/Microsoft.PowerShell.ScheduledJob/commands/RegisterJobDefinition.cs index cb8e47aa228..c724cf77962 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/commands/RegisterJobDefinition.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/commands/RegisterJobDefinition.cs @@ -227,7 +227,7 @@ protected override void ProcessRecord() if (definition != null) { // Set the MaxCount value if available. - if (MyInvocation.BoundParameters.ContainsKey("MaxResultCount")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(MaxResultCount))) { if (MaxResultCount < 1) { @@ -245,7 +245,7 @@ protected override void ProcessRecord() { // If RunEvery parameter is specified then create a job trigger for the definition that // runs the job at the requested interval. - if (MyInvocation.BoundParameters.ContainsKey("RunEvery")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RunEvery))) { AddRepetitionJobTriggerToDefinition( definition, diff --git a/src/Microsoft.PowerShell.ScheduledJob/commands/ScheduledJobOptionCmdletBase.cs b/src/Microsoft.PowerShell.ScheduledJob/commands/ScheduledJobOptionCmdletBase.cs index 89571e90e56..4971658086f 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/commands/ScheduledJobOptionCmdletBase.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/commands/ScheduledJobOptionCmdletBase.cs @@ -175,13 +175,13 @@ public SwitchParameter StartIfIdle protected override void BeginProcessing() { // Validate parameters. - if (MyInvocation.BoundParameters.ContainsKey("IdleTimeout") && + if (MyInvocation.BoundParameters.ContainsKey(nameof(IdleTimeout)) && _idleTimeout < TimeSpan.Zero) { throw new PSArgumentException(ScheduledJobErrorStrings.InvalidIdleTimeout); } - if (MyInvocation.BoundParameters.ContainsKey("IdleDuration") && + if (MyInvocation.BoundParameters.ContainsKey(nameof(IdleDuration)) && _idleDuration < TimeSpan.Zero) { throw new PSArgumentException(ScheduledJobErrorStrings.InvalidIdleDuration); diff --git a/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobDefinition.cs b/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobDefinition.cs index b94dd190d36..f9f9e2e6f62 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobDefinition.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/commands/SetJobDefinition.cs @@ -258,7 +258,7 @@ protected override void ProcessRecord() // If RunEvery parameter is specified then create a job trigger for the definition that // runs the job at the requested interval. bool addedTrigger = false; - if (MyInvocation.BoundParameters.ContainsKey("RunEvery")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RunEvery))) { AddRepetitionJobTriggerToDefinition( _definition, @@ -340,7 +340,7 @@ private void UpdateDefinition() UpdateJobInvocationInfo(); - if (MyInvocation.BoundParameters.ContainsKey("MaxResultCount")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(MaxResultCount))) { _definition.SetExecutionHistoryLength(MaxResultCount, false); } @@ -406,7 +406,7 @@ private Dictionary UpdateParameters() } // RunAs32 - if (MyInvocation.BoundParameters.ContainsKey("RunAs32")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RunAs32))) { if (newParameters.ContainsKey(ScheduledJobInvocationInfo.RunAs32Parameter)) { @@ -419,7 +419,7 @@ private Dictionary UpdateParameters() } // Authentication - if (MyInvocation.BoundParameters.ContainsKey("Authentication")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(Authentication))) { if (newParameters.ContainsKey(ScheduledJobInvocationInfo.AuthenticationParameter)) { diff --git a/src/Microsoft.PowerShell.ScheduledJob/commands/SetScheduledJobOption.cs b/src/Microsoft.PowerShell.ScheduledJob/commands/SetScheduledJobOption.cs index 3ebe841629d..7c0734e4f4a 100644 --- a/src/Microsoft.PowerShell.ScheduledJob/commands/SetScheduledJobOption.cs +++ b/src/Microsoft.PowerShell.ScheduledJob/commands/SetScheduledJobOption.cs @@ -53,67 +53,67 @@ protected override void ProcessRecord() // Update ScheduledJobOptions object with current parameters. // Update switch parameters only if they were selected. // Also update the ScheduledJobDefinition object associated with this options object. - if (MyInvocation.BoundParameters.ContainsKey("StartIfOnBattery")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(StartIfOnBattery))) { _jobOptions.StartIfOnBatteries = StartIfOnBattery; } - if (MyInvocation.BoundParameters.ContainsKey("ContinueIfGoingOnBattery")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(ContinueIfGoingOnBattery))) { _jobOptions.StopIfGoingOnBatteries = !ContinueIfGoingOnBattery; } - if (MyInvocation.BoundParameters.ContainsKey("WakeToRun")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(WakeToRun))) { _jobOptions.WakeToRun = WakeToRun; } - if (MyInvocation.BoundParameters.ContainsKey("StartIfIdle")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(StartIfIdle))) { _jobOptions.StartIfNotIdle = !StartIfIdle; } - if (MyInvocation.BoundParameters.ContainsKey("StopIfGoingOffIdle")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(StopIfGoingOffIdle))) { _jobOptions.StopIfGoingOffIdle = StopIfGoingOffIdle; } - if (MyInvocation.BoundParameters.ContainsKey("RestartOnIdleResume")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RestartOnIdleResume))) { _jobOptions.RestartOnIdleResume = RestartOnIdleResume; } - if (MyInvocation.BoundParameters.ContainsKey("HideInTaskScheduler")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(HideInTaskScheduler))) { _jobOptions.ShowInTaskScheduler = !HideInTaskScheduler; } - if (MyInvocation.BoundParameters.ContainsKey("RunElevated")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RunElevated))) { _jobOptions.RunElevated = RunElevated; } - if (MyInvocation.BoundParameters.ContainsKey("RequireNetwork")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(RequireNetwork))) { _jobOptions.RunWithoutNetwork = !RequireNetwork; } - if (MyInvocation.BoundParameters.ContainsKey("DoNotAllowDemandStart")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(DoNotAllowDemandStart))) { _jobOptions.DoNotAllowDemandStart = DoNotAllowDemandStart; } - if (MyInvocation.BoundParameters.ContainsKey("IdleDuration")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(IdleDuration))) { _jobOptions.IdleDuration = IdleDuration; } - if (MyInvocation.BoundParameters.ContainsKey("IdleTimeout")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(IdleTimeout))) { _jobOptions.IdleTimeout = IdleTimeout; } - if (MyInvocation.BoundParameters.ContainsKey("MultipleInstancePolicy")) + if (MyInvocation.BoundParameters.ContainsKey(nameof(MultipleInstancePolicy))) { _jobOptions.MultipleInstancePolicy = MultipleInstancePolicy; } diff --git a/src/System.Management.Automation/engine/remoting/commands/GetJob.cs b/src/System.Management.Automation/engine/remoting/commands/GetJob.cs index f967dba67ef..96106647f19 100644 --- a/src/System.Management.Automation/engine/remoting/commands/GetJob.cs +++ b/src/System.Management.Automation/engine/remoting/commands/GetJob.cs @@ -194,7 +194,7 @@ protected List FindJobs() /// return the list of jobs after applying HasMoreData filter private List ApplyHasMoreDataFiltering(List jobList) { - bool hasMoreDataParameter = MyInvocation.BoundParameters.ContainsKey("HasMoreData"); + bool hasMoreDataParameter = MyInvocation.BoundParameters.ContainsKey(nameof(HasMoreData)); if (!hasMoreDataParameter) { @@ -221,8 +221,8 @@ private List ApplyHasMoreDataFiltering(List jobList) /// returns job list including all child jobs with ChildJobState or all if IncludeChildJob is specified private List FindChildJobs(List jobList) { - bool childJobStateParameter = MyInvocation.BoundParameters.ContainsKey("ChildJobState"); - bool includeChildJobParameter = MyInvocation.BoundParameters.ContainsKey("IncludeChildJob"); + bool childJobStateParameter = MyInvocation.BoundParameters.ContainsKey(nameof(ChildJobState)); + bool includeChildJobParameter = MyInvocation.BoundParameters.ContainsKey(nameof(IncludeChildJob)); List matches = new List(); @@ -267,9 +267,9 @@ private List FindChildJobs(List jobList) /// private List ApplyTimeFiltering(List jobList) { - bool beforeParameter = MyInvocation.BoundParameters.ContainsKey("Before"); - bool afterParameter = MyInvocation.BoundParameters.ContainsKey("After"); - bool newestParameter = MyInvocation.BoundParameters.ContainsKey("Newest"); + bool beforeParameter = MyInvocation.BoundParameters.ContainsKey(nameof(Before)); + bool afterParameter = MyInvocation.BoundParameters.ContainsKey(nameof(After)); + bool newestParameter = MyInvocation.BoundParameters.ContainsKey(nameof(Newest)); if (!beforeParameter && !afterParameter && !newestParameter) { diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index 92a461c7c5d..4ef3b02a97a 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -782,7 +782,7 @@ protected override void BeginProcessing() throw new InvalidOperationException(RemotingErrorIdStrings.AsJobAndDisconnectedError); } - if (MyInvocation.BoundParameters.ContainsKey("SessionName") && !this.InvokeAndDisconnect) + if (MyInvocation.BoundParameters.ContainsKey(nameof(SessionName)) && !this.InvokeAndDisconnect) { throw new InvalidOperationException(RemotingErrorIdStrings.SessionNameWithoutInvokeDisconnected); }