"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.5/src/Microsoft.PowerShell.ScheduledJob/commands/AddJobTrigger.cs" (21 Jun 2022, 5135 Bytes) of package /linux/misc/PowerShell-7.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddJobTrigger.cs" see the Fossies "Dox" file reference documentation.

    1 // Copyright (c) Microsoft Corporation.
    2 // Licensed under the MIT License.
    3 
    4 using System;
    5 using System.Collections.Generic;
    6 using System.Collections.ObjectModel;
    7 using System.Diagnostics;
    8 using System.Diagnostics.CodeAnalysis;
    9 using System.Management.Automation;
   10 using System.Management.Automation.Host;
   11 using System.Management.Automation.Internal;
   12 using System.Threading;
   13 
   14 namespace Microsoft.PowerShell.ScheduledJob
   15 {
   16     /// <summary>
   17     /// This cmdlet adds ScheduledJobTriggers to ScheduledJobDefinition objects.
   18     /// </summary>
   19     [Cmdlet(VerbsCommon.Add, "JobTrigger", DefaultParameterSetName = AddJobTriggerCommand.JobDefinitionParameterSet,
   20         HelpUri = "https://go.microsoft.com/fwlink/?LinkID=223913")]
   21     public sealed class AddJobTriggerCommand : ScheduleJobCmdletBase
   22     {
   23         #region Parameters
   24 
   25         private const string JobDefinitionParameterSet = "JobDefinition";
   26         private const string JobDefinitionIdParameterSet = "JobDefinitionId";
   27         private const string JobDefinitionNameParameterSet = "JobDefinitionName";
   28 
   29         /// <summary>
   30         /// ScheduledJobTrigger.
   31         /// </summary>
   32         [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true,
   33                    ParameterSetName = AddJobTriggerCommand.JobDefinitionParameterSet)]
   34         [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true,
   35                    ParameterSetName = AddJobTriggerCommand.JobDefinitionIdParameterSet)]
   36         [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true,
   37                    ParameterSetName = AddJobTriggerCommand.JobDefinitionNameParameterSet)]
   38         [ValidateNotNullOrEmpty]
   39         [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
   40         public ScheduledJobTrigger[] Trigger
   41         {
   42             get { return _triggers; }
   43 
   44             set { _triggers = value; }
   45         }
   46 
   47         private ScheduledJobTrigger[] _triggers;
   48 
   49         /// <summary>
   50         /// ScheduledJobDefinition Id.
   51         /// </summary>
   52         [Parameter(Position = 0, Mandatory = true,
   53                    ParameterSetName = AddJobTriggerCommand.JobDefinitionIdParameterSet)]
   54         [ValidateNotNullOrEmpty]
   55         [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
   56         public Int32[] Id
   57         {
   58             get { return _ids; }
   59 
   60             set { _ids = value; }
   61         }
   62 
   63         private Int32[] _ids;
   64 
   65         /// <summary>
   66         /// ScheduledJobDefinition Name.
   67         /// </summary>
   68         [Parameter(Position = 0, Mandatory = true,
   69                    ParameterSetName = AddJobTriggerCommand.JobDefinitionNameParameterSet)]
   70         [ValidateNotNullOrEmpty]
   71         [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
   72         public string[] Name
   73         {
   74             get { return _names; }
   75 
   76             set { _names = value; }
   77         }
   78 
   79         private string[] _names;
   80 
   81         /// <summary>
   82         /// ScheduledJobDefinition.
   83         /// </summary>
   84         [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true,
   85                    ParameterSetName = AddJobTriggerCommand.JobDefinitionParameterSet)]
   86         [ValidateNotNullOrEmpty]
   87         [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
   88         public ScheduledJobDefinition[] InputObject
   89         {
   90             get { return _definitions; }
   91 
   92             set { _definitions = value; }
   93         }
   94 
   95         private ScheduledJobDefinition[] _definitions;
   96 
   97         #endregion
   98 
   99         #region Cmdlet Overrides
  100 
  101         /// <summary>
  102         /// Process input.
  103         /// </summary>
  104         protected override void ProcessRecord()
  105         {
  106             switch (ParameterSetName)
  107             {
  108                 case JobDefinitionParameterSet:
  109                     AddToJobDefinition(_definitions);
  110                     break;
  111 
  112                 case JobDefinitionIdParameterSet:
  113                     AddToJobDefinition(GetJobDefinitionsById(_ids));
  114                     break;
  115 
  116                 case JobDefinitionNameParameterSet:
  117                     AddToJobDefinition(GetJobDefinitionsByName(_names));
  118                     break;
  119             }
  120         }
  121 
  122         #endregion
  123 
  124         #region Private Methods
  125 
  126         private void AddToJobDefinition(IEnumerable<ScheduledJobDefinition> jobDefinitions)
  127         {
  128             foreach (ScheduledJobDefinition definition in jobDefinitions)
  129             {
  130                 try
  131                 {
  132                     definition.AddTriggers(_triggers, true);
  133                 }
  134                 catch (ScheduledJobException e)
  135                 {
  136                     string msg = StringUtil.Format(ScheduledJobErrorStrings.CantAddJobTriggersToDefinition, definition.Name);
  137                     Exception reason = new RuntimeException(msg, e);
  138                     ErrorRecord errorRecord = new ErrorRecord(reason, "CantAddJobTriggersToScheduledJobDefinition", ErrorCategory.InvalidOperation, definition);
  139                     WriteError(errorRecord);
  140                 }
  141             }
  142         }
  143 
  144         #endregion
  145     }
  146 }