Example #1
0
 private void ValidatePSTypeName(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, bool retryOtherBindingAfterFailure, object parameterValue)
 {
     if (parameterValue != null)
     {
         IEnumerable <string> internalTypeNames = PSObject.AsPSObject(parameterValue).InternalTypeNames;
         string pSTypeName = parameterMetadata.PSTypeName;
         if (!internalTypeNames.Contains <string>(pSTypeName, StringComparer.OrdinalIgnoreCase))
         {
             ParameterBindingException exception2;
             object[] arguments = new object[] { ((this.invocationInfo != null) && (this.invocationInfo.MyCommand != null)) ? this.invocationInfo.MyCommand.Name : string.Empty, parameterMetadata.Name, parameterMetadata.Type, parameterValue.GetType(), 0, 0, pSTypeName };
             PSInvalidCastException innerException = new PSInvalidCastException(ErrorCategory.InvalidArgument.ToString(), null, ResourceManagerCache.GetResourceString("ParameterBinderStrings", "MismatchedPSTypeName"), arguments);
             if (!retryOtherBindingAfterFailure)
             {
                 exception2 = new ParameterBindingArgumentTransformationException(innerException, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, parameterValue.GetType(), "ParameterBinderStrings", "MismatchedPSTypeName", new object[] { pSTypeName });
             }
             else
             {
                 exception2 = new ParameterBindingException(innerException, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, parameterValue.GetType(), "ParameterBinderStrings", "MismatchedPSTypeName", new object[] { pSTypeName });
             }
             throw exception2;
         }
     }
 }
        /// <summary>
        /// Verify if all arguments from the command line are bound.
        /// </summary>
        /// <param name="originalBindingException">
        /// Previous binding exceptions that possibly causes the failure
        /// </param>
        private void VerifyArgumentsProcessed(ParameterBindingException originalBindingException)
        {
            // Now verify that all the arguments that were passed in were processed.

            if (UnboundArguments.Count > 0)
            {
                ParameterBindingException bindingException;
                CommandParameterInternal parameter = UnboundArguments[0];

                // Get the argument type that was specified

                Type specifiedType = null;
                object argumentValue = parameter.ArgumentValue;
                if (argumentValue != null && argumentValue != UnboundParameter.Value)
                {
                    specifiedType = argumentValue.GetType();
                }

                if (parameter.ParameterNameSpecified)
                {
                    bindingException =
                        new ParameterBindingException(
                            ErrorCategory.InvalidArgument,
                            this.Command.MyInvocation,
                            GetParameterErrorExtent(parameter),
                            parameter.ParameterName,
                            null,
                            specifiedType,
                            ParameterBinderStrings.NamedParameterNotFound,
                            "NamedParameterNotFound");
                }
                else
                {
                    // If this was a positional parameter, and we have the original exception,
                    // report on the original error
                    if (originalBindingException != null)
                    {
                        bindingException = originalBindingException;
                    }
                    // Otherwise, give a generic error.
                    else
                    {
                        string argument = StringLiterals.DollarNull;
                        if (null != parameter.ArgumentValue)
                        {
                            try
                            {
                                argument = parameter.ArgumentValue.ToString();
                            }
                            catch (Exception e)
                            {
                                CommandProcessorBase.CheckForSevereException(e);
                                bindingException =
                                    new ParameterBindingArgumentTransformationException(
                                        e,
                                        ErrorCategory.InvalidData,
                                        this.InvocationInfo,
                                        null,
                                        null,
                                        null,
                                        parameter.ArgumentValue.GetType(),
                                        ParameterBinderStrings.ParameterArgumentTransformationErrorMessageOnly,
                                        "ParameterArgumentTransformationErrorMessageOnly",
                                        e.Message);

                                if (!DefaultParameterBindingInUse)
                                {
                                    throw bindingException;
                                }
                                else
                                {
                                    ThrowElaboratedBindingException(bindingException);
                                }
                            }
                        }

                        bindingException =
                            new ParameterBindingException(
                                ErrorCategory.InvalidArgument,
                                this.Command.MyInvocation,
                                null,
                                argument,
                                null,
                                specifiedType,
                                ParameterBinderStrings.PositionalParameterNotFound,
                                "PositionalParameterNotFound");
                    }
                }
                if (!DefaultParameterBindingInUse)
                {
                    throw bindingException;
                }
                else
                {
                    ThrowElaboratedBindingException(bindingException);
                }
            }
        }
Example #3
0
 private void ValidatePSTypeName(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, bool retryOtherBindingAfterFailure, object parameterValue)
 {
     if (parameterValue != null)
     {
         IEnumerable<string> internalTypeNames = PSObject.AsPSObject(parameterValue).InternalTypeNames;
         string pSTypeName = parameterMetadata.PSTypeName;
         if (!internalTypeNames.Contains<string>(pSTypeName, StringComparer.OrdinalIgnoreCase))
         {
             ParameterBindingException exception2;
             object[] arguments = new object[] { ((this.invocationInfo != null) && (this.invocationInfo.MyCommand != null)) ? this.invocationInfo.MyCommand.Name : string.Empty, parameterMetadata.Name, parameterMetadata.Type, parameterValue.GetType(), 0, 0, pSTypeName };
             PSInvalidCastException innerException = new PSInvalidCastException(ErrorCategory.InvalidArgument.ToString(), null, ResourceManagerCache.GetResourceString("ParameterBinderStrings", "MismatchedPSTypeName"), arguments);
             if (!retryOtherBindingAfterFailure)
             {
                 exception2 = new ParameterBindingArgumentTransformationException(innerException, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, parameterValue.GetType(), "ParameterBinderStrings", "MismatchedPSTypeName", new object[] { pSTypeName });
             }
             else
             {
                 exception2 = new ParameterBindingException(innerException, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, parameterValue.GetType(), "ParameterBinderStrings", "MismatchedPSTypeName", new object[] { pSTypeName });
             }
             throw exception2;
         }
     }
 }
Example #4
0
 internal virtual bool BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
 {
     bool flag = false;
     bool flag2 = (flags & ParameterBindingFlags.ShouldCoerceType) != ParameterBindingFlags.None;
     if (parameter == null)
     {
         throw PSTraceSource.NewArgumentNullException("parameter");
     }
     if (parameterMetadata == null)
     {
         throw PSTraceSource.NewArgumentNullException("parameterMetadata");
     }
     using (bindingTracer.TraceScope("BIND arg [{0}] to parameter [{1}]", new object[] { parameter.ArgumentValue, parameterMetadata.Name }))
     {
         parameter.ParameterName = parameterMetadata.Name;
         object argumentValue = parameter.ArgumentValue;
         ScriptParameterBinder binder = this as ScriptParameterBinder;
         bool bindingScriptCmdlet = false;
         if (binder != null)
         {
             bindingScriptCmdlet = binder.Script.UsesCmdletBinding;
         }
         foreach (ArgumentTransformationAttribute attribute in parameterMetadata.ArgumentTransformationAttributes)
         {
             using (bindingTracer.TraceScope("Executing DATA GENERATION metadata: [{0}]", new object[] { attribute.GetType() }))
             {
                 try
                 {
                     ArgumentTypeConverterAttribute attribute2 = attribute as ArgumentTypeConverterAttribute;
                     if (attribute2 != null)
                     {
                         if (flag2)
                         {
                             argumentValue = attribute2.Transform(this.engine, argumentValue, true, bindingScriptCmdlet);
                         }
                     }
                     else
                     {
                         argumentValue = attribute.Transform(this.engine, argumentValue);
                     }
                     bindingTracer.WriteLine("result returned from DATA GENERATION: {0}", new object[] { argumentValue });
                 }
                 catch (Exception exception)
                 {
                     CommandProcessorBase.CheckForSevereException(exception);
                     bindingTracer.WriteLine("ERROR: DATA GENERATION: {0}", new object[] { exception.Message });
                     ParameterBindingException exception2 = new ParameterBindingArgumentTransformationException(exception, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentTransformationError", new object[] { exception.Message });
                     throw exception2;
                 }
             }
         }
         if (flag2)
         {
             argumentValue = this.CoerceTypeAsNeeded(parameter, parameterMetadata.Name, parameterMetadata.Type, parameterMetadata.CollectionTypeInformation, argumentValue);
         }
         else if (!this.ShouldContinueUncoercedBind(parameter, parameterMetadata, flags, ref argumentValue))
         {
             goto Label_040E;
         }
         if ((parameterMetadata.PSTypeName != null) && (argumentValue != null))
         {
             IEnumerable enumerable = LanguagePrimitives.GetEnumerable(argumentValue);
             if (enumerable != null)
             {
                 foreach (object obj3 in enumerable)
                 {
                     this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, obj3);
                 }
             }
             else
             {
                 this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, argumentValue);
             }
         }
         if ((flags & ParameterBindingFlags.IsDefaultValue) == ParameterBindingFlags.None)
         {
             foreach (ValidateArgumentsAttribute attribute3 in parameterMetadata.ValidationAttributes)
             {
                 using (bindingTracer.TraceScope("Executing VALIDATION metadata: [{0}]", new object[] { attribute3.GetType() }))
                 {
                     try
                     {
                         attribute3.InternalValidate(argumentValue, this.engine);
                     }
                     catch (Exception exception3)
                     {
                         CommandProcessorBase.CheckForSevereException(exception3);
                         bindingTracer.WriteLine("ERROR: VALIDATION FAILED: {0}", new object[] { exception3.Message });
                         ParameterBindingValidationException exception4 = new ParameterBindingValidationException(exception3, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationError", new object[] { exception3.Message });
                         throw exception4;
                     }
                     tracer.WriteLine("Validation attribute on {0} returned {1}.", new object[] { parameterMetadata.Name, flag });
                 }
             }
             if (IsParameterMandatory(parameterMetadata))
             {
                 this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.Type, argumentValue, true);
             }
         }
         Exception innerException = null;
         try
         {
             this.BindParameter(parameter.ParameterName, argumentValue);
             flag = true;
         }
         catch (SetValueException exception6)
         {
             innerException = exception6;
         }
         if (innerException != null)
         {
             Type typeSpecified = (argumentValue == null) ? null : argumentValue.GetType();
             ParameterBindingException exception7 = new ParameterBindingException(innerException, ErrorCategory.WriteError, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, typeSpecified, "ParameterBinderStrings", "ParameterBindingFailed", new object[] { innerException.Message });
             throw exception7;
         }
     Label_040E:;
         bindingTracer.WriteLine("BIND arg [{0}] to param [{1}] {2}", new object[] { argumentValue, parameter.ParameterName, flag ? "SUCCESSFUL" : "SKIPPED" });
         if (flag)
         {
             if (this.RecordBoundParameters)
             {
                 this.CommandLineParameters.Add(parameter.ParameterName, argumentValue);
             }
             MshCommandRuntime commandRuntime = this.Command.commandRuntime as MshCommandRuntime;
             if ((commandRuntime != null) && commandRuntime.LogPipelineExecutionDetail)
             {
                 IEnumerable source = LanguagePrimitives.GetEnumerable(argumentValue);
                 if (source != null)
                 {
                     string parameterValue = string.Join(", ", source.Cast<object>().ToArray<object>());
                     commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, parameterValue);
                 }
                 else
                 {
                     commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, (argumentValue == null) ? "" : argumentValue.ToString());
                 }
             }
         }
         return flag;
     }
 }
 private void VerifyArgumentsProcessed(ParameterBindingException originalBindingException)
 {
     if (base.UnboundArguments.Count > 0)
     {
         ParameterBindingException exception;
         CommandParameterInternal cpi = base.UnboundArguments[0];
         Type typeSpecified = null;
         object argumentValue = cpi.ArgumentValue;
         if ((argumentValue != null) && (argumentValue != UnboundParameter.Value))
         {
             typeSpecified = argumentValue.GetType();
         }
         if (cpi.ParameterNameSpecified)
         {
             exception = new ParameterBindingException(ErrorCategory.InvalidArgument, this.Command.MyInvocation, base.GetParameterErrorExtent(cpi), cpi.ParameterName, null, typeSpecified, "ParameterBinderStrings", "NamedParameterNotFound", new object[0]);
         }
         else if (originalBindingException != null)
         {
             exception = originalBindingException;
         }
         else
         {
             string parameterName = "$null";
             if (cpi.ArgumentValue != null)
             {
                 try
                 {
                     parameterName = cpi.ArgumentValue.ToString();
                 }
                 catch (Exception exception2)
                 {
                     CommandProcessorBase.CheckForSevereException(exception2);
                     exception = new ParameterBindingArgumentTransformationException(exception2, ErrorCategory.InvalidData, base.InvocationInfo, null, null, null, cpi.ArgumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentTransformationErrorMessageOnly", new object[] { exception2.Message });
                     if (!base.DefaultParameterBindingInUse)
                     {
                         throw exception;
                     }
                     base.ThrowElaboratedBindingException(exception);
                 }
             }
             exception = new ParameterBindingException(ErrorCategory.InvalidArgument, this.Command.MyInvocation, null, parameterName, null, typeSpecified, "ParameterBinderStrings", "PositionalParameterNotFound", new object[0]);
         }
         if (!base.DefaultParameterBindingInUse)
         {
             throw exception;
         }
         base.ThrowElaboratedBindingException(exception);
     }
 }
Example #6
0
        internal virtual bool BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
        {
            bool flag  = false;
            bool flag2 = (flags & ParameterBindingFlags.ShouldCoerceType) != ParameterBindingFlags.None;

            if (parameter == null)
            {
                throw PSTraceSource.NewArgumentNullException("parameter");
            }
            if (parameterMetadata == null)
            {
                throw PSTraceSource.NewArgumentNullException("parameterMetadata");
            }
            using (bindingTracer.TraceScope("BIND arg [{0}] to parameter [{1}]", new object[] { parameter.ArgumentValue, parameterMetadata.Name }))
            {
                parameter.ParameterName = parameterMetadata.Name;
                object argumentValue         = parameter.ArgumentValue;
                ScriptParameterBinder binder = this as ScriptParameterBinder;
                bool bindingScriptCmdlet     = false;
                if (binder != null)
                {
                    bindingScriptCmdlet = binder.Script.UsesCmdletBinding;
                }
                foreach (ArgumentTransformationAttribute attribute in parameterMetadata.ArgumentTransformationAttributes)
                {
                    using (bindingTracer.TraceScope("Executing DATA GENERATION metadata: [{0}]", new object[] { attribute.GetType() }))
                    {
                        try
                        {
                            ArgumentTypeConverterAttribute attribute2 = attribute as ArgumentTypeConverterAttribute;
                            if (attribute2 != null)
                            {
                                if (flag2)
                                {
                                    argumentValue = attribute2.Transform(this.engine, argumentValue, true, bindingScriptCmdlet);
                                }
                            }
                            else
                            {
                                argumentValue = attribute.Transform(this.engine, argumentValue);
                            }
                            bindingTracer.WriteLine("result returned from DATA GENERATION: {0}", new object[] { argumentValue });
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            bindingTracer.WriteLine("ERROR: DATA GENERATION: {0}", new object[] { exception.Message });
                            ParameterBindingException exception2 = new ParameterBindingArgumentTransformationException(exception, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentTransformationError", new object[] { exception.Message });
                            throw exception2;
                        }
                    }
                }
                if (flag2)
                {
                    argumentValue = this.CoerceTypeAsNeeded(parameter, parameterMetadata.Name, parameterMetadata.Type, parameterMetadata.CollectionTypeInformation, argumentValue);
                }
                else if (!this.ShouldContinueUncoercedBind(parameter, parameterMetadata, flags, ref argumentValue))
                {
                    goto Label_040E;
                }
                if ((parameterMetadata.PSTypeName != null) && (argumentValue != null))
                {
                    IEnumerable enumerable = LanguagePrimitives.GetEnumerable(argumentValue);
                    if (enumerable != null)
                    {
                        foreach (object obj3 in enumerable)
                        {
                            this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, obj3);
                        }
                    }
                    else
                    {
                        this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, argumentValue);
                    }
                }
                if ((flags & ParameterBindingFlags.IsDefaultValue) == ParameterBindingFlags.None)
                {
                    foreach (ValidateArgumentsAttribute attribute3 in parameterMetadata.ValidationAttributes)
                    {
                        using (bindingTracer.TraceScope("Executing VALIDATION metadata: [{0}]", new object[] { attribute3.GetType() }))
                        {
                            try
                            {
                                attribute3.InternalValidate(argumentValue, this.engine);
                            }
                            catch (Exception exception3)
                            {
                                CommandProcessorBase.CheckForSevereException(exception3);
                                bindingTracer.WriteLine("ERROR: VALIDATION FAILED: {0}", new object[] { exception3.Message });
                                ParameterBindingValidationException exception4 = new ParameterBindingValidationException(exception3, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationError", new object[] { exception3.Message });
                                throw exception4;
                            }
                            tracer.WriteLine("Validation attribute on {0} returned {1}.", new object[] { parameterMetadata.Name, flag });
                        }
                    }
                    if (IsParameterMandatory(parameterMetadata))
                    {
                        this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.Type, argumentValue, true);
                    }
                }
                Exception innerException = null;
                try
                {
                    this.BindParameter(parameter.ParameterName, argumentValue);
                    flag = true;
                }
                catch (SetValueException exception6)
                {
                    innerException = exception6;
                }
                if (innerException != null)
                {
                    Type typeSpecified = (argumentValue == null) ? null : argumentValue.GetType();
                    ParameterBindingException exception7 = new ParameterBindingException(innerException, ErrorCategory.WriteError, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, typeSpecified, "ParameterBinderStrings", "ParameterBindingFailed", new object[] { innerException.Message });
                    throw exception7;
                }
                Label_040E :;
                bindingTracer.WriteLine("BIND arg [{0}] to param [{1}] {2}", new object[] { argumentValue, parameter.ParameterName, flag ? "SUCCESSFUL" : "SKIPPED" });
                if (flag)
                {
                    if (this.RecordBoundParameters)
                    {
                        this.CommandLineParameters.Add(parameter.ParameterName, argumentValue);
                    }
                    MshCommandRuntime commandRuntime = this.Command.commandRuntime as MshCommandRuntime;
                    if ((commandRuntime != null) && commandRuntime.LogPipelineExecutionDetail)
                    {
                        IEnumerable source = LanguagePrimitives.GetEnumerable(argumentValue);
                        if (source != null)
                        {
                            string parameterValue = string.Join(", ", source.Cast <object>().ToArray <object>());
                            commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, parameterValue);
                        }
                        else
                        {
                            commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, (argumentValue == null) ? "" : argumentValue.ToString());
                        }
                    }
                }
                return(flag);
            }
        }