Ejemplo n.º 1
0
        /// <summary>
        /// Execute one or more PowerShell script files to get language blocks.
        /// </summary>
        /// <param name="sources"></param>
        /// <returns></returns>
        private static IEnumerable <ILanguageBlock> GetLanguageBlock(Source[] sources)
        {
            var results = new Collection <ILanguageBlock>();

            var runspace = PipelineContext.CurrentThread.GetRunspace();
            var ps       = PowerShell.Create();

            try
            {
                ps.Runspace = runspace;
                PipelineContext.EnableLogging(ps);
                PipelineContext.CurrentThread.Logger.EnterScope("[Discovery.Rule]");
                PipelineContext.CurrentThread.ExecutionScope = ExecutionScope.Script;

                // Process scripts

                foreach (var source in sources)
                {
                    foreach (var file in source.File)
                    {
                        if (file.Type != RuleSourceType.Script)
                        {
                            continue;
                        }

                        ps.Commands.Clear();

                        PipelineContext.CurrentThread.VerboseRuleDiscovery(path: file.Path);
                        PipelineContext.CurrentThread.EnterSourceScope(source: file);

                        var scriptAst = System.Management.Automation.Language.Parser.ParseFile(file.Path, out Token[] tokens, out ParseError[] errors);
Ejemplo n.º 2
0
        protected override void ProcessRecord()
        {
            if (!IsScriptScope())
            {
                throw new RuleRuntimeException(string.Format(PSRuleResources.KeywordScriptScope, LanguageKeywords.Rule));
            }

            var context  = PipelineContext.CurrentThread;
            var metadata = GetMetadata(MyInvocation.ScriptName, MyInvocation.ScriptLineNumber, MyInvocation.OffsetInLine);
            var tag      = GetTag(Tag);
            var source   = context.Source.File;

            context.VerboseFoundRule(ruleName: Name, scriptName: MyInvocation.ScriptName);

            CheckDependsOn();

            var ps = PowerShell.Create();

            ps.Runspace = context.GetRunspace();
            ps.AddCommand(new CmdletInfo(InvokeBlockCmdletName, typeof(InvokeRuleBlockCommand)));
            ps.AddParameter(InvokeBlockCmdlet_TypeParameter, Type);
            ps.AddParameter(InvokeBlockCmdlet_IfParameter, If);
            ps.AddParameter(InvokeBlockCmdlet_BodyParameter, Body);

            PipelineContext.EnableLogging(ps);

            var helpInfo = GetHelpInfo(context: context, name: Name) ?? new RuleHelpInfo(name: Name, displayName: Name, moduleName: source.ModuleName);

            if (helpInfo.Synopsis == null)
            {
                helpInfo.Synopsis = metadata.Synopsis;
            }

            var block = new RuleBlock(
                source: source,
                ruleName: Name,
                info: helpInfo,
                condition: ps,
                tag: tag,
                dependsOn: RuleHelper.ExpandRuleName(DependsOn, MyInvocation.ScriptName, source.ModuleName),
                configuration: Configure
                );

            WriteObject(block);
        }