コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: VariableOps.cs プロジェクト: rsumner31/powershell
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        /// <summary>
        /// The main processing loop of the command.
        /// </summary>
        /// 
        protected override void ProcessRecord()
        {
            string path = GetFilePath();

            if (path == null)
            {
                return;
            }

            if (!File.Exists(path))
            {
                InvalidOperationException ioe =
                    PSTraceSource.NewInvalidOperationException(
                        ImportLocalizedDataStrings.FileNotExist,
                        path);
                WriteError(new ErrorRecord(ioe, "ImportLocalizedData", ErrorCategory.ObjectNotFound, path));
                return;
            }

            // Prevent additional commands in ConstrainedLanguage mode
            if (Context.LanguageMode == PSLanguageMode.ConstrainedLanguage)
            {
                if (_setSupportedCommand)
                {
                    NotSupportedException nse =
                        PSTraceSource.NewNotSupportedException(
                            ImportLocalizedDataStrings.CannotDefineSupportedCommand);
                    ThrowTerminatingError(
                        new ErrorRecord(nse, "CannotDefineSupportedCommand", ErrorCategory.PermissionDenied, null));
                }
            }

            string script = GetScript(path);
            if (script == null)
            {
                return;
            }

            try
            {
                var scriptBlock = Context.Engine.ParseScriptBlock(script, false);
                scriptBlock.CheckRestrictedLanguage(SupportedCommand, null, false);
                object result;
                PSLanguageMode oldLanguageMode = Context.LanguageMode;
                Context.LanguageMode = PSLanguageMode.RestrictedLanguage;
                try
                {
                    result = scriptBlock.InvokeReturnAsIs();
                    if (result == AutomationNull.Value)
                    {
                        result = null;
                    }
                }
                finally
                {
                    Context.LanguageMode = oldLanguageMode;
                }

                if (_bindingVariable != null)
                {
                    VariablePath variablePath = new VariablePath(_bindingVariable);
                    if (variablePath.IsUnscopedVariable)
                    {
                        variablePath = variablePath.CloneAndSetLocal();
                    }

                    if (string.IsNullOrEmpty(variablePath.UnqualifiedPath))
                    {
                        InvalidOperationException ioe = PSTraceSource.NewInvalidOperationException(
                            ImportLocalizedDataStrings.IncorrectVariableName, _bindingVariable);
                        WriteError(new ErrorRecord(ioe, "ImportLocalizedData", ErrorCategory.InvalidArgument,
                                                   _bindingVariable));
                        return;
                    }

                    SessionStateScope scope = null;
                    PSVariable variable = SessionState.Internal.GetVariableItem(variablePath, out scope);

                    if (variable == null)
                    {
                        variable = new PSVariable(variablePath.UnqualifiedPath, result, ScopedItemOptions.None);
                        Context.EngineSessionState.SetVariable(variablePath, variable, false, CommandOrigin.Internal);
                    }
                    else
                    {
                        variable.Value = result;
                    }
                } // end _bindingvariable != null

                // If binding variable is null, write the object to stream
                else
                {
                    WriteObject(result);
                }
            }
            catch (RuntimeException e)
            {
                PSInvalidOperationException ioe = PSTraceSource.NewInvalidOperationException(e,
                    ImportLocalizedDataStrings.ErrorLoadingDataFile,
                    path,
                    e.Message);

                throw ioe;
            }

            return;
        } // ProcessRecord
コード例 #5
0
ファイル: VariableOps.cs プロジェクト: nickchal/pash
 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;
 }
コード例 #6
0
ファイル: VariableOps.cs プロジェクト: 40a/PowerShell
        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;
        }
コード例 #7
0
ファイル: ImportLocalizedData.cs プロジェクト: nickchal/pash
 protected override void ProcessRecord()
 {
     string filePath = this.GetFilePath();
     if (filePath != null)
     {
         if (!File.Exists(filePath))
         {
             InvalidOperationException exception = PSTraceSource.NewInvalidOperationException("ImportLocalizedData", "FileNotExist", new object[] { filePath });
             base.WriteError(new ErrorRecord(exception, "ImportLocalizedData", ErrorCategory.ObjectNotFound, filePath));
         }
         else
         {
             string script = this.GetScript(filePath);
             if (script != null)
             {
                 try
                 {
                     object obj2;
                     ScriptBlock block = base.Context.Engine.ParseScriptBlock(script, false);
                     block.CheckRestrictedLanguage(this.SupportedCommand, null, false);
                     PSLanguageMode languageMode = base.Context.LanguageMode;
                     base.Context.LanguageMode = PSLanguageMode.RestrictedLanguage;
                     try
                     {
                         obj2 = block.InvokeReturnAsIs(new object[0]);
                         if (obj2 == AutomationNull.Value)
                         {
                             obj2 = null;
                         }
                     }
                     finally
                     {
                         base.Context.LanguageMode = languageMode;
                     }
                     if (this._bindingVariable != null)
                     {
                         VariablePath variablePath = new VariablePath(this._bindingVariable);
                         if (variablePath.IsUnscopedVariable)
                         {
                             variablePath = variablePath.CloneAndSetLocal();
                         }
                         if (string.IsNullOrEmpty(variablePath.UnqualifiedPath))
                         {
                             InvalidOperationException exception2 = PSTraceSource.NewInvalidOperationException("ImportLocalizedData", "IncorrectVariableName", new object[] { this._bindingVariable });
                             base.WriteError(new ErrorRecord(exception2, "ImportLocalizedData", ErrorCategory.InvalidArgument, this._bindingVariable));
                         }
                         else
                         {
                             SessionStateScope scope = null;
                             PSVariable variableItem = base.SessionState.Internal.GetVariableItem(variablePath, out scope);
                             if (variableItem == null)
                             {
                                 variableItem = new PSVariable(variablePath.UnqualifiedPath, obj2, ScopedItemOptions.None);
                                 base.Context.EngineSessionState.SetVariable(variablePath, variableItem, false, CommandOrigin.Internal);
                             }
                             else
                             {
                                 variableItem.Value = obj2;
                             }
                         }
                     }
                     else
                     {
                         base.WriteObject(obj2);
                     }
                 }
                 catch (RuntimeException exception3)
                 {
                     throw PSTraceSource.NewInvalidOperationException(exception3, "ImportLocalizedData", "ErrorLoadingDataFile", new object[] { filePath, exception3.Message });
                 }
             }
         }
     }
 }