Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
CommonCommandParameters.cs285 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Management.Automation.Remoting;6using System.Management.Automation.Runspaces;7 8namespace System.Management.Automation.Internal9{10    /// <summary>11    /// Defines the parameters that are present on all Cmdlets.12    /// </summary>13    public sealed class CommonParameters14    {15        #region ctor16 17        /// <summary>18        /// Constructs an instance with the specified command instance.19        /// </summary>20        /// <param name="commandRuntime">21        /// The instance of the command that the parameters should set the22        /// user feedback properties on when the parameters get bound.23        /// </param>24        /// <exception cref="ArgumentNullException">25        /// If <paramref name="cmdlet"/> is null.26        /// </exception>27        internal CommonParameters(MshCommandRuntime commandRuntime)28        {29            if (commandRuntime == null)30            {31                throw PSTraceSource.NewArgumentNullException(nameof(commandRuntime));32            }33 34            _commandRuntime = commandRuntime;35        }36 37        #endregion ctor38 39        #region parameters40 41        /// <summary>42        /// Gets or sets the value of the Verbose parameter for the cmdlet.43        /// </summary>44        /// <remarks>45        /// This parameter46        /// tells the command to articulate the actions it performs while executing.47        /// </remarks>48        [Parameter]49        [Alias("vb")]50        public SwitchParameter Verbose51        {52            get { return _commandRuntime.Verbose; }53 54            set { _commandRuntime.Verbose = value; }55        }56 57        /// <summary>58        /// Gets or sets the value of the Debug parameter for the cmdlet.59        /// </summary>60        /// <remarks>61        /// This parameter tells the command to provide Programmer/Support type62        /// messages to understand what is really occurring and give the user the63        /// opportunity to stop or debug the situation.64        /// </remarks>65        [Parameter]66        [Alias("db")]67        public SwitchParameter Debug68        {69            get { return _commandRuntime.Debug; }70 71            set { _commandRuntime.Debug = value; }72        }73 74        /// <summary>75        /// Gets or sets the value of the ErrorAction parameter for the cmdlet.76        /// </summary>77        /// <remarks>78        /// This parameter tells the command what to do when an error occurs.79        /// </remarks>80        [Parameter]81        [Alias("ea")]82        public ActionPreference ErrorAction83        {84            get { return _commandRuntime.ErrorAction; }85 86            set { _commandRuntime.ErrorAction = value; }87        }88 89        /// <summary>90        /// Gets or sets the value of the WarningAction parameter for the cmdlet.91        /// </summary>92        /// <remarks>93        /// This parameter tells the command what to do when a warning94        /// occurs.95        /// </remarks>96        [Parameter]97        [Alias("wa")]98        public ActionPreference WarningAction99        {100            get { return _commandRuntime.WarningPreference; }101 102            set { _commandRuntime.WarningPreference = value; }103        }104 105        /// <summary>106        /// Gets or sets the value of the InformationAction parameter for the cmdlet.107        /// </summary>108        /// <remarks>109        /// This parameter tells the command what to do when an informational record occurs.110        /// </remarks>111        /// <!--112        /// NOTE: The "infa" alias name does not follow the same alias naming convention used113        /// with other common parameter aliases that control stream functionality; however,114        /// "ia" was already taken as a parameter alias in other commands when this parameter115        /// was added to PowerShell, so "infa" was chosen instead.116        /// -->117        [Parameter]118        [Alias("infa")]119        public ActionPreference InformationAction120        {121            get { return _commandRuntime.InformationPreference; }122 123            set { _commandRuntime.InformationPreference = value; }124        }125 126        /// <summary>127        /// Gets or sets the value of the ProgressAction parameter for the cmdlet.128        /// </summary>129        /// <remarks>130        /// This parameter tells the command what to do when a progress record occurs.131        /// </remarks>132        /// <!--133        /// NOTE: The "proga" alias name does not follow the same alias naming convention used134        /// with other common parameter aliases that control stream functionality; however,135        /// "pa" was already taken as a parameter alias in other commands when this parameter136        /// was added to PowerShell, so "proga" was chosen instead.137        /// -->138        [Parameter]139        [Alias("proga")]140        public ActionPreference ProgressAction141        {142            get { return _commandRuntime.ProgressPreference; }143 144            set { _commandRuntime.ProgressPreference = value; }145        }146 147        /// <summary>148        /// Gets or sets the value of the ErrorVariable parameter for the cmdlet.149        /// </summary>150        /// <remarks>151        /// This parameter tells the command which variable to populate with the errors.152        /// Use +varname to append to the variable rather than clearing it.153        /// </remarks>154        /// <!--155        /// 897599-2003/10/20-JonN Need to figure out how to get a working156        /// commandline parameter without making it a public property157        /// -->158        [Parameter]159        [Alias("ev")]160        [ValidateVariableName]161        public string ErrorVariable162        {163            get { return _commandRuntime.ErrorVariable; }164 165            set { _commandRuntime.ErrorVariable = value; }166        }167 168        /// <summary>169        /// Gets or sets the value of the WarningVariable parameter for the cmdlet.170        /// </summary>171        /// <remarks>172        /// This parameter tells the command which variable to populate with the warnings.173        /// Use +varname to append to the variable rather than clearing it.174        /// </remarks>175        [Parameter]176        [Alias("wv")]177        [ValidateVariableName]178        public string WarningVariable179        {180            get { return _commandRuntime.WarningVariable; }181 182            set { _commandRuntime.WarningVariable = value; }183        }184 185        /// <summary>186        /// Gets or sets the value of the InformationVariable parameter for the cmdlet.187        /// </summary>188        /// <remarks>189        /// This parameter tells the command which variable to populate with the informational output.190        /// Use +varname to append to the variable rather than clearing it.191        /// </remarks>192        [Parameter]193        [Alias("iv")]194        [ValidateVariableName]195        public string InformationVariable196        {197            get { return _commandRuntime.InformationVariable; }198 199            set { _commandRuntime.InformationVariable = value; }200        }201 202        /// <summary>203        /// Gets or sets the OutVariable parameter for the cmdlet.204        /// </summary>205        /// <remarks>206        /// This parameter tells the command to set all success output in the specified variable.207        /// Similar to the way -errorvariable sets all errors to a variable name.208        /// Semantically this is equivalent to :  command |set-var varname -passthru209        /// but it should be MUCH faster as there is no binding that takes place210        /// </remarks>211        [Parameter]212        [Alias("ov")]213        [ValidateVariableName]214        public string OutVariable215        {216            get { return _commandRuntime.OutVariable; }217 218            set { _commandRuntime.OutVariable = value; }219        }220 221        /// <summary>222        /// Gets or sets the OutBuffer parameter for the cmdlet.223        /// </summary>224        /// <remarks>225        /// This parameter configures the number of objects to buffer before calling the downstream Cmdlet226        /// </remarks>227        [Parameter]228        [ValidateRange(0, Int32.MaxValue)]229        [Alias("ob")]230        public int OutBuffer231        {232            get { return _commandRuntime.OutBuffer; }233 234            set { _commandRuntime.OutBuffer = value; }235        }236 237        /// <summary>238        /// Gets or sets the PipelineVariable parameter for the cmdlet.239        /// </summary>240        /// <remarks>241        /// This parameter defines a variable to hold the current pipeline output the command242        /// as it passes down the pipeline:243        /// Write-Output (1..10) -PipelineVariable WriteOutput | Foreach-Object { "Hello" }  |244        ///     Foreach-Object { $WriteOutput }245        /// </remarks>246        [Parameter]247        [Alias("pv")]248        [ValidateVariableName]249        public string PipelineVariable250        {251            get { return _commandRuntime.PipelineVariable; }252 253            set { _commandRuntime.PipelineVariable = value; }254        }255 256        #endregion parameters257 258        private readonly MshCommandRuntime _commandRuntime;259 260        internal class ValidateVariableName : ValidateArgumentsAttribute261        {262            protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)263            {264                string varName = arguments as string;265                if (varName != null)266                {267                    if (varName.StartsWith('+'))268                    {269                        varName = varName.Substring(1);270                    }271 272                    VariablePath silp = new VariablePath(varName);273                    if (!silp.IsVariable)274                    {275                        throw new ValidationMetadataException(276                            "ArgumentNotValidVariableName",277                            null,278                            Metadata.ValidateVariableName, varName);279                    }280                }281            }282        }283    }284}285