Пример #1
0
 internal static RuntimeException NewInterpreterExceptionByMessage(Type exceptionType, IScriptExtent errorPosition, string message, string errorId, Exception innerException)
 {
     RuntimeException exception;
     if (exceptionType == typeof(ParseException))
     {
         exception = new ParseException(message, errorId, innerException);
     }
     else if (exceptionType == typeof(IncompleteParseException))
     {
         exception = new IncompleteParseException(message, errorId, innerException);
     }
     else
     {
         exception = new RuntimeException(message, innerException);
         exception.SetErrorId(errorId);
         exception.SetErrorCategory(ErrorCategory.InvalidOperation);
     }
     if (errorPosition != null)
     {
         exception.ErrorRecord.SetInvocationInfo(new InvocationInfo(null, errorPosition));
     }
     return exception;
 }
Пример #2
0
        internal static Hashtable GetModuleManifestProperties(string psDataFilePath, string[] keys)
        {
            string dataFileContents = ScriptAnalysis.ReadScript(psDataFilePath);
            ParseError[] parseErrors;
            var ast = (new Parser()).Parse(psDataFilePath, dataFileContents, null, out parseErrors, ParseMode.ModuleAnalysis);
            if (parseErrors.Length > 0)
            {
                var pe = new ParseException(parseErrors);
                throw PSTraceSource.NewInvalidOperationException(
                    pe,
                    ParserStrings.CannotLoadPowerShellDataFile,
                    psDataFilePath,
                    pe.Message);
            }

            string unused1;
            string unused2;
            var pipeline = ast.GetSimplePipeline(false, out unused1, out unused2);
            if (pipeline != null)
            {
                var hashtableAst = pipeline.GetPureExpression() as HashtableAst;
                if (hashtableAst != null)
                {
                    var result = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    foreach (var pair in hashtableAst.KeyValuePairs)
                    {
                        var key = pair.Item1 as StringConstantExpressionAst;
                        if (key != null && keys.Contains(key.Value, StringComparer.OrdinalIgnoreCase))
                        {
                            try
                            {
                                var val = pair.Item2.SafeGetValue();
                                result[key.Value] = val;
                            }
                            catch
                            {
                                throw PSTraceSource.NewInvalidOperationException(
                                         ParserStrings.InvalidPowerShellDataFile,
                                         psDataFilePath);
                            }
                        }
                    }
                    return result;
                }
            }

            throw PSTraceSource.NewInvalidOperationException(
                     ParserStrings.InvalidPowerShellDataFile,
                     psDataFilePath);
        }