/// <summary> /// Execute one or more PowerShell script files to get language blocks. /// </summary> /// <param name="context"></param> /// <param name="sources"></param> /// <returns></returns> private static ILanguageBlock[] GetLanguageBlock(RunspaceContext context, Source[] sources) { var results = new List <ILanguageBlock>(); var ps = context.NewPowerShell(); context.PushScope(RunspaceScope.Source); try { // Process each source foreach (var source in sources) { // Process search file per source foreach (var file in source.File) { if (file.Type != SourceType.Script) { continue; } ps.Commands.Clear(); if (!context.EnterSourceFile(file)) { throw new FileNotFoundException(PSDocsResources.ScriptNotFound, file.Path); } var scriptAst = System.Management.Automation.Language.Parser.ParseFile(file.Path, out Token[] tokens, out ParseError[] errors);
/// <summary> /// Execute one or more PowerShell script files to get language blocks. /// </summary> /// <param name="context"></param> /// <param name="sources"></param> /// <returns></returns> private static ILanguageBlock[] GetLanguageBlock(RunspaceContext context, Source[] sources) { var results = new List <ILanguageBlock>(); var ps = context.NewPowerShell(); try { // Process each source foreach (var source in sources) { // Process search file per source foreach (var file in source.File) { ps.Commands.Clear(); if (!context.EnterSourceFile(file)) { throw new FileNotFoundException(PSDocsResources.ScriptNotFound, file.Path); } try { // Invoke script ps.AddScript(string.Concat("& '", file.Path, "'"), true); var invokeResults = ps.Invoke(); // Discovery has errors so skip this file if (ps.HadErrors) { continue; } foreach (var ir in invokeResults) { if (ir.BaseObject is ScriptDocumentBlock block) { results.Add(block); } } } catch (Exception e) { context.WriteRuntimeException(sourceFile: file.Path, inner: e); } } } } finally { context.ExitSourceFile(); ps.Runspace = null; ps.Dispose(); } return(results.ToArray()); }
/// <summary> /// Execute one or more PowerShell script files to get language blocks. /// </summary> /// <param name="context"></param> /// <param name="sources"></param> /// <returns></returns> private static ILanguageBlock[] GetLanguageBlock(RunspaceContext context, Source[] sources) { var results = new Collection <ILanguageBlock>(); var ps = context.NewPowerShell(); try { //context.EnterScope("[Discovery.Rule]"); // Process scripts foreach (var source in sources) { foreach (var file in source.File) { ps.Commands.Clear(); if (!context.TrySourceFile(file)) { throw new FileNotFoundException(PSDocsResources.ScriptNotFound, file.Path); } //context. //PipelineContext.CurrentThread.Source = source; //PipelineContext.CurrentThread.VerboseRuleDiscovery(path: source.Path); //PipelineContext.CurrentThread.UseSource(source: source); try { // Invoke script ps.AddScript(string.Concat("& '", file.Path, "'"), true); var invokeResults = ps.Invoke(); if (ps.HadErrors) { // Discovery has errors so skip this file continue; } foreach (var ir in invokeResults) { if (ir.BaseObject is ScriptDocumentBlock block) { results.Add(block); } } } catch (Exception e) { context.WriteRuntimeException(sourceFile: file.Path, inner: e); } } } } finally { //context.ExitScope(); context.ExitSourceFile(); //PipelineContext.CurrentThread.Source = null; ps.Runspace = null; ps.Dispose(); } return(results.ToArray()); }