/// <summary> /// Constructs a ParameterBindingValidationException. /// </summary> /// <param name="innerException"> /// The inner exception. /// </param> /// <param name="errorCategory"> /// The category for the error. /// </param> /// <param name="invocationInfo"> /// The information about the command that encountered the error. /// /// InvocationInfo.MyCommand.Name == {0} /// </param> /// <param name="errorPosition"> /// The position for the command or parameter that caused the error. /// /// token.LineNumber == {4} /// token.OffsetInLine == {5} /// </param> /// <param name="parameterName"> /// The parameter on which binding caused the error. /// /// parameterName == {1} /// </param> /// <param name="parameterType"> /// The Type the parameter was expecting. /// /// parameterType == {2} /// </param> /// <param name="typeSpecified"> /// The Type that was attempted to be bound to the parameter. /// /// typeSpecified == {3} /// </param> /// <param name="resourceString"> /// The format string for the exception message. /// </param> /// <param name="errorId"> /// The error ID. /// </param> /// <param name="args"> /// Additional arguments to pass to the format string. /// /// starts at {6} /// </param> /// <exception cref="ArgumentNullException"> /// If <paramref name="invocationInfo"/> is null. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="resourceBaseName"/> or <paramref name="errorIdAndResourceId"/> /// is null or empty. /// </exception> internal ParameterBindingValidationException( Exception innerException, ErrorCategory errorCategory, InvocationInfo invocationInfo, IScriptExtent errorPosition, string parameterName, Type parameterType, Type typeSpecified, string resourceString, string errorId, params object[] args) : base( innerException, errorCategory, invocationInfo, errorPosition, parameterName, parameterType, typeSpecified, resourceString, errorId, args) { ValidationMetadataException validationException = innerException as ValidationMetadataException; if (validationException != null && validationException.SwallowException) { _swallowException = true; } }
private void SetValue(object value) { if ((this.options & (ScopedItemOptions.Constant | ScopedItemOptions.ReadOnly)) != ScopedItemOptions.None) { SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(this.name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable); throw exception; } object obj2 = value; if ((this.attributes != null) && (this.attributes.Count > 0)) { obj2 = TransformValue(this.attributes, value); if (!this.IsValidValue(obj2)) { ValidationMetadataException exception2 = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidValueFailure, new object[] { this.name, (obj2 != null) ? obj2.ToString() : "" }); throw exception2; } } if (obj2 != null) { obj2 = CopyMutableValues(obj2); } this._value = obj2; this.DebuggerCheckVariableWrite(); }
private static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts) { SessionStateScope scope; SessionStateInternal engineSessionState = executionContext.EngineSessionState; CommandOrigin scopeOrigin = engineSessionState.CurrentScope.ScopeOrigin; if (!variablePath.IsVariable) { engineSessionState.SetVariable(variablePath, value, true, scopeOrigin); return(value); } if (executionContext.PSDebugTraceLevel > 0) { executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value); } if (variablePath.IsUnscopedVariable) { variablePath = variablePath.CloneAndSetLocal(); } PSVariable newValue = engineSessionState.GetVariableItem(variablePath, out scope, scopeOrigin); if (newValue == null) { Collection <Attribute> attributes = (attributeAsts == null) ? new Collection <Attribute>() : GetAttributeCollection(attributeAsts); newValue = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes); engineSessionState.SetVariable(variablePath, newValue, false, scopeOrigin); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } return(value); } if (attributeAsts != null) { newValue.Attributes.Clear(); Collection <Attribute> attributeCollection = GetAttributeCollection(attributeAsts); value = PSVariable.TransformValue(attributeCollection, value); if (!PSVariable.IsValidValue(attributeCollection, value)) { ValidationMetadataException exception = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidValueFailure, new object[] { newValue.Name, (value != null) ? value.ToString() : "" }); throw exception; } newValue.SetValueRaw(value, true); newValue.AddParameterAttributesNoChecks(attributeCollection); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } return(value); } newValue.Value = value; return(value); }
/// <summary> /// Verifies the constraints and attributes before setting the value /// </summary> /// <param name="value"> /// The value to be set. /// </param> /// <exception cref="SessionStateUnauthorizedAccessException"> /// If the variable is read-only or constant. /// </exception> /// <exception cref="ValidationMetadataException"> /// If the validation metadata throws an exception or the value doesn't /// pass the validation metadata. /// </exception> private void SetValue(object value) { // Check to see if the variable is writable if ((_options & (ScopedItemOptions.ReadOnly | ScopedItemOptions.Constant)) != ScopedItemOptions.None) { SessionStateUnauthorizedAccessException e = new SessionStateUnauthorizedAccessException( Name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable); throw e; } // Now perform all ArgumentTransformations that are needed object transformedValue = value; if (_attributes != null && _attributes.Count > 0) { transformedValue = TransformValue(_attributes, value); // Next check to make sure the value is valid if (!IsValidValue(transformedValue)) { ValidationMetadataException e = new ValidationMetadataException( "ValidateSetFailure", null, Metadata.InvalidValueFailure, Name, ((transformedValue != null) ? transformedValue.ToString() : "$null")); throw e; } } if (transformedValue != null) { transformedValue = CopyMutableValues(transformedValue); } // Set the value before triggering any write breakpoints _value = transformedValue; DebuggerCheckVariableWrite(); }
private object VerifyNewAttribute(Attribute item) { object inputData = this.variable.Value; ArgumentTransformationAttribute attribute = item as ArgumentTransformationAttribute; if (attribute != null) { ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS(); EngineIntrinsics engineIntrinsics = null; if (executionContextFromTLS != null) { engineIntrinsics = executionContextFromTLS.EngineIntrinsics; } inputData = attribute.Transform(engineIntrinsics, inputData); } if (!PSVariable.IsValidValue(inputData, item)) { ValidationMetadataException exception = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidMetadataForCurrentValue, new object[] { this.variable.Name, (this.variable.Value != null) ? this.variable.Value.ToString() : "" }); throw exception; } return(inputData); }
/// <summary> /// Validates and performs any transformations that the new attribute /// implements. /// </summary> /// <param name="item"> /// The new attribute to be added to the collection. /// </param> /// <returns> /// The new variable value. This may change from the original value if the /// new attribute is an ArgumentTransformationAttribute. /// </returns> private object VerifyNewAttribute(Attribute item) { object variableValue = _variable.Value; // Perform transformation before validating ArgumentTransformationAttribute argumentTransformation = item as ArgumentTransformationAttribute; if (argumentTransformation != null) { // Get an EngineIntrinsics instance using the context of the thread. ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS(); EngineIntrinsics engine = null; if (context != null) { engine = context.EngineIntrinsics; } variableValue = argumentTransformation.TransformInternal(engine, variableValue); } if (!PSVariable.IsValidValue(variableValue, item)) { ValidationMetadataException e = new ValidationMetadataException( "ValidateSetFailure", null, Metadata.InvalidMetadataForCurrentValue, _variable.Name, ((_variable.Value != null) ? _variable.Value.ToString() : string.Empty)); throw e; } return(variableValue); }
/// <summary> /// Verifies the constraints and attributes before setting the value /// </summary> /// /// <param name="value"> /// The value to be set. /// </param> /// /// <exception cref="SessionStateUnauthorizedAccessException"> /// If the variable is read-only or constant. /// </exception> /// /// <exception cref="ValidationMetadataException"> /// If the validation metadata throws an exception or the value doesn't /// pass the validation metadata. /// </exception> /// private void SetValue(object value) { // Check to see if the variable is writable if ((_options & (ScopedItemOptions.ReadOnly | ScopedItemOptions.Constant)) != ScopedItemOptions.None) { SessionStateUnauthorizedAccessException e = new SessionStateUnauthorizedAccessException( Name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable); throw e; } // Now perform all ArgumentTransformations that are needed object transformedValue = value; if (_attributes != null && _attributes.Count > 0) { transformedValue = TransformValue(_attributes, value); // Next check to make sure the value is valid if (!IsValidValue(transformedValue)) { ValidationMetadataException e = new ValidationMetadataException( "ValidateSetFailure", null, Metadata.InvalidValueFailure, Name, ((transformedValue != null) ? transformedValue.ToString() : "$null")); throw e; } } if (transformedValue != null) { transformedValue = CopyMutableValues(transformedValue); } // Set the value before triggering any write breakpoints _value = transformedValue; DebuggerCheckVariableWrite(); }
internal static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts) { SessionStateInternal sessionState = executionContext.EngineSessionState; CommandOrigin origin = sessionState.CurrentScope.ScopeOrigin; if (!variablePath.IsVariable) { sessionState.SetVariable(variablePath, value, true, origin); return(value); } // Variable assignment is traced only if trace level 2 is specified. if (executionContext.PSDebugTraceLevel > 1) { executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value); } if (variablePath.IsUnscopedVariable) { variablePath = variablePath.CloneAndSetLocal(); } SessionStateScope scope; PSVariable var = sessionState.GetVariableItem(variablePath, out scope, origin); if (var == null) { var attributes = attributeAsts == null ? new Collection <Attribute>() : GetAttributeCollection(attributeAsts); var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes); sessionState.SetVariable(variablePath, var, false, origin); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } } else if (attributeAsts != null) { // Use bytewise operation directly instead of 'var.IsReadOnly || var.IsConstant' on // a hot path (setting variable with type constraint) to get better performance. if ((var.Options & (ScopedItemOptions.ReadOnly | ScopedItemOptions.Constant)) != ScopedItemOptions.None) { SessionStateUnauthorizedAccessException e = new SessionStateUnauthorizedAccessException( var.Name, SessionStateCategory.Variable, "VariableNotWritable", SessionStateStrings.VariableNotWritable); throw e; } var attributes = GetAttributeCollection(attributeAsts); value = PSVariable.TransformValue(attributes, value); if (!PSVariable.IsValidValue(attributes, value)) { ValidationMetadataException e = new ValidationMetadataException( "ValidateSetFailure", null, Metadata.InvalidValueFailure, var.Name, ((value != null) ? value.ToString() : "$null")); throw e; } var.SetValueRaw(value, true); // Don't update the PSVariable's attributes until we successfully set the value var.Attributes.Clear(); var.AddParameterAttributesNoChecks(attributes); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } } else { // The setter will handle checking for variable writes. var.Value = value; } return(value); }
internal static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts) { SessionStateInternal sessionState = executionContext.EngineSessionState; CommandOrigin origin = sessionState.CurrentScope.ScopeOrigin; if (!variablePath.IsVariable) { sessionState.SetVariable(variablePath, value, true, origin); return(value); } // Variable assignment is traced only if trace level 2 is specified. if (executionContext.PSDebugTraceLevel > 1) { executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value); } if (variablePath.IsUnscopedVariable) { variablePath = variablePath.CloneAndSetLocal(); } SessionStateScope scope; PSVariable var = sessionState.GetVariableItem(variablePath, out scope, origin); if (var == null) { var attributes = attributeAsts == null ? new Collection <Attribute>() : GetAttributeCollection(attributeAsts); var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes); sessionState.SetVariable(variablePath, var, false, origin); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } } else if (attributeAsts != null) { var.Attributes.Clear(); var attributes = GetAttributeCollection(attributeAsts); value = PSVariable.TransformValue(attributes, value); if (!PSVariable.IsValidValue(attributes, value)) { ValidationMetadataException e = new ValidationMetadataException( "ValidateSetFailure", null, Metadata.InvalidValueFailure, var.Name, ((value != null) ? value.ToString() : "$null")); throw e; } var.SetValueRaw(value, true); var.AddParameterAttributesNoChecks(attributes); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } } else { // The setter will handle checking for variable writes. var.Value = value; } return(value); }
private static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts) { SessionStateScope scope; SessionStateInternal engineSessionState = executionContext.EngineSessionState; CommandOrigin scopeOrigin = engineSessionState.CurrentScope.ScopeOrigin; if (!variablePath.IsVariable) { engineSessionState.SetVariable(variablePath, value, true, scopeOrigin); return value; } if (executionContext.PSDebugTraceLevel > 0) { executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value); } if (variablePath.IsUnscopedVariable) { variablePath = variablePath.CloneAndSetLocal(); } PSVariable newValue = engineSessionState.GetVariableItem(variablePath, out scope, scopeOrigin); if (newValue == null) { Collection<Attribute> attributes = (attributeAsts == null) ? new Collection<Attribute>() : GetAttributeCollection(attributeAsts); newValue = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes); engineSessionState.SetVariable(variablePath, newValue, false, scopeOrigin); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } return value; } if (attributeAsts != null) { newValue.Attributes.Clear(); Collection<Attribute> attributeCollection = GetAttributeCollection(attributeAsts); value = PSVariable.TransformValue(attributeCollection, value); if (!PSVariable.IsValidValue(attributeCollection, value)) { ValidationMetadataException exception = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidValueFailure, new object[] { newValue.Name, (value != null) ? value.ToString() : "" }); throw exception; } newValue.SetValueRaw(value, true); newValue.AddParameterAttributesNoChecks(attributeCollection); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } return value; } newValue.Value = value; return value; }
private ErrorRecord GetValidationError(string msg, string errorId, params object[] args) { msg = string.Format(CultureInfo.InvariantCulture, msg, args); var ex = new ValidationMetadataException(msg); var error = new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this); return (error); }
private ErrorRecord GetValidationError(string msg, string errorId) { var ex = new ValidationMetadataException(msg); var error = new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this); return (error); }
internal static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts) { SessionStateInternal sessionState = executionContext.EngineSessionState; CommandOrigin origin = sessionState.CurrentScope.ScopeOrigin; if (!variablePath.IsVariable) { sessionState.SetVariable(variablePath, value, true, origin); return value; } // Variable assignment is traced only if trace level 2 is specified. if (executionContext.PSDebugTraceLevel > 1) { executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value); } if (variablePath.IsUnscopedVariable) { variablePath = variablePath.CloneAndSetLocal(); } SessionStateScope scope; PSVariable var = sessionState.GetVariableItem(variablePath, out scope, origin); if (var == null) { var attributes = attributeAsts == null ? new Collection<Attribute>() : GetAttributeCollection(attributeAsts); var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes); sessionState.SetVariable(variablePath, var, false, origin); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } } else if (attributeAsts != null) { var.Attributes.Clear(); var attributes = GetAttributeCollection(attributeAsts); value = PSVariable.TransformValue(attributes, value); if (!PSVariable.IsValidValue(attributes, value)) { ValidationMetadataException e = new ValidationMetadataException( "ValidateSetFailure", null, Metadata.InvalidValueFailure, var.Name, ((value != null) ? value.ToString() : "$null")); throw e; } var.SetValueRaw(value, true); var.AddParameterAttributesNoChecks(attributes); if (executionContext._debuggingMode > 0) { executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath); } } else { // The setter will handle checking for variable writes. var.Value = value; } return value; }