Example #1
0
        public override AstVisitAction VisitCommand(CommandAst commandAst)
        {
            string commandName = commandAst.GetCommandName();

            if (string.IsNullOrEmpty(commandName))
            {
                commandName = commandAst.CommandElements[0].ToString().Trim(new char[] { '"', '\'' });
            }
            if (commandAst.InvocationOperator == TokenKind.Dot)
            {
                commandName = Regex.Replace(commandName, @"\$[^\\]*\\", "", RegexOptions.IgnoreCase);
                RequiredModuleInfo item = new RequiredModuleInfo {
                    Name = commandName,
                    CommandsToPostFilter = new List <string>()
                };
                this.DiscoveredModules.Add(item);
                ModuleIntrinsics.Tracer.WriteLine("Module dots " + commandName, new object[0]);
            }
            if (((string.Equals(commandName, "New-Alias", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, @"Microsoft.PowerShell.Utility\New-Alias", StringComparison.OrdinalIgnoreCase)) || (string.Equals(commandName, "Set-Alias", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, @"Microsoft.PowerShell.Utility\Set-Alias", StringComparison.OrdinalIgnoreCase))) || (string.Equals(commandName, "nal", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, "sal", StringComparison.OrdinalIgnoreCase)))
            {
                string str2 = this.GetParameterByNameOrPosition("Name", 0, commandAst);
                string str3 = this.GetParameterByNameOrPosition("Value", 1, commandAst);
                if (!string.IsNullOrEmpty(str2))
                {
                    this.DiscoveredAliases[str2] = str3;
                    ModuleIntrinsics.Tracer.WriteLine("Module defines alias: " + str2 + "=" + str3, new object[0]);
                }
            }
            if (string.Equals(commandName, "Import-Module", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, "ipmo", StringComparison.OrdinalIgnoreCase))
            {
                List <string> list = new List <string>();
                string        str4 = this.GetParameterByNameOrPosition("Function", -1, commandAst);
                if (!string.IsNullOrEmpty(str4))
                {
                    list.AddRange(this.ProcessExportedCommandList(str4));
                }
                string str5 = this.GetParameterByNameOrPosition("Cmdlet", -1, commandAst);
                if (!string.IsNullOrEmpty(str5))
                {
                    list.AddRange(this.ProcessExportedCommandList(str5));
                }
                string str6 = this.GetParameterByNameOrPosition("Alias", -1, commandAst);
                if (!string.IsNullOrEmpty(str6))
                {
                    list.AddRange(this.ProcessExportedCommandList(str6));
                }
                string str7 = this.GetParameterByNameOrPosition("Name", 0, commandAst);
                if (!string.IsNullOrEmpty(str7))
                {
                    foreach (string str8 in str7.Split(new char[] { ',' }))
                    {
                        ModuleIntrinsics.Tracer.WriteLine("Discovered module import: " + str8, new object[0]);
                        RequiredModuleInfo info2 = new RequiredModuleInfo {
                            Name = str8.Trim(),
                            CommandsToPostFilter = list
                        };
                        this.DiscoveredModules.Add(info2);
                    }
                }
            }
            if ((string.Equals(commandName, "Export-ModuleMember", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, @"Microsoft.PowerShell.Core\Export-ModuleMember", StringComparison.OrdinalIgnoreCase)) || string.Equals(commandName, "$script:ExportModuleMember", StringComparison.OrdinalIgnoreCase))
            {
                List <string> list2     = new List <string>();
                string        arguments = this.GetParameterByNameOrPosition("Function", 0, commandAst);
                list2.AddRange(this.ExtractArgumentList(arguments));
                string str10 = this.GetParameterByNameOrPosition("Cmdlet", -1, commandAst);
                list2.AddRange(this.ExtractArgumentList(str10));
                foreach (string str11 in list2)
                {
                    this.DiscoveredCommandFilters.Add(str11);
                    ModuleIntrinsics.Tracer.WriteLine("Discovered explicit export: " + str11, new object[0]);
                    if (!WildcardPattern.ContainsWildcardCharacters(str11) && !this.DiscoveredExports.Contains(str11))
                    {
                        this.DiscoveredExports.Add(str11);
                    }
                }
                list2 = new List <string>();
                string str12 = this.GetParameterByNameOrPosition("Alias", -1, commandAst);
                list2.AddRange(this.ExtractArgumentList(str12));
                foreach (string str13 in list2)
                {
                    this.DiscoveredCommandFilters.Add(str13);
                    if (!WildcardPattern.ContainsWildcardCharacters(str13) && !this.DiscoveredAliases.ContainsKey(str13))
                    {
                        this.DiscoveredAliases.Add(str13, null);
                    }
                }
            }
            if (string.Equals(commandName, "public", StringComparison.OrdinalIgnoreCase) && (commandAst.CommandElements.Count > 2))
            {
                string str14 = commandAst.CommandElements[2].ToString().Trim();
                this.DiscoveredExports.Add(str14);
                this.DiscoveredCommandFilters.Add(str14);
            }
            return(AstVisitAction.Continue);
        }
Example #2
0
 public override AstVisitAction VisitCommand(CommandAst commandAst)
 {
     string commandName = commandAst.GetCommandName();
     if (string.IsNullOrEmpty(commandName))
     {
         commandName = commandAst.CommandElements[0].ToString().Trim(new char[] { '"', '\'' });
     }
     if (commandAst.InvocationOperator == TokenKind.Dot)
     {
         commandName = Regex.Replace(commandName, @"\$[^\\]*\\", "", RegexOptions.IgnoreCase);
         RequiredModuleInfo item = new RequiredModuleInfo {
             Name = commandName,
             CommandsToPostFilter = new List<string>()
         };
         this.DiscoveredModules.Add(item);
         ModuleIntrinsics.Tracer.WriteLine("Module dots " + commandName, new object[0]);
     }
     if (((string.Equals(commandName, "New-Alias", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, @"Microsoft.PowerShell.Utility\New-Alias", StringComparison.OrdinalIgnoreCase)) || (string.Equals(commandName, "Set-Alias", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, @"Microsoft.PowerShell.Utility\Set-Alias", StringComparison.OrdinalIgnoreCase))) || (string.Equals(commandName, "nal", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, "sal", StringComparison.OrdinalIgnoreCase)))
     {
         string str2 = this.GetParameterByNameOrPosition("Name", 0, commandAst);
         string str3 = this.GetParameterByNameOrPosition("Value", 1, commandAst);
         if (!string.IsNullOrEmpty(str2))
         {
             this.DiscoveredAliases[str2] = str3;
             ModuleIntrinsics.Tracer.WriteLine("Module defines alias: " + str2 + "=" + str3, new object[0]);
         }
     }
     if (string.Equals(commandName, "Import-Module", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, "ipmo", StringComparison.OrdinalIgnoreCase))
     {
         List<string> list = new List<string>();
         string str4 = this.GetParameterByNameOrPosition("Function", -1, commandAst);
         if (!string.IsNullOrEmpty(str4))
         {
             list.AddRange(this.ProcessExportedCommandList(str4));
         }
         string str5 = this.GetParameterByNameOrPosition("Cmdlet", -1, commandAst);
         if (!string.IsNullOrEmpty(str5))
         {
             list.AddRange(this.ProcessExportedCommandList(str5));
         }
         string str6 = this.GetParameterByNameOrPosition("Alias", -1, commandAst);
         if (!string.IsNullOrEmpty(str6))
         {
             list.AddRange(this.ProcessExportedCommandList(str6));
         }
         string str7 = this.GetParameterByNameOrPosition("Name", 0, commandAst);
         if (!string.IsNullOrEmpty(str7))
         {
             foreach (string str8 in str7.Split(new char[] { ',' }))
             {
                 ModuleIntrinsics.Tracer.WriteLine("Discovered module import: " + str8, new object[0]);
                 RequiredModuleInfo info2 = new RequiredModuleInfo {
                     Name = str8.Trim(),
                     CommandsToPostFilter = list
                 };
                 this.DiscoveredModules.Add(info2);
             }
         }
     }
     if ((string.Equals(commandName, "Export-ModuleMember", StringComparison.OrdinalIgnoreCase) || string.Equals(commandName, @"Microsoft.PowerShell.Core\Export-ModuleMember", StringComparison.OrdinalIgnoreCase)) || string.Equals(commandName, "$script:ExportModuleMember", StringComparison.OrdinalIgnoreCase))
     {
         List<string> list2 = new List<string>();
         string arguments = this.GetParameterByNameOrPosition("Function", 0, commandAst);
         list2.AddRange(this.ExtractArgumentList(arguments));
         string str10 = this.GetParameterByNameOrPosition("Cmdlet", -1, commandAst);
         list2.AddRange(this.ExtractArgumentList(str10));
         foreach (string str11 in list2)
         {
             this.DiscoveredCommandFilters.Add(str11);
             ModuleIntrinsics.Tracer.WriteLine("Discovered explicit export: " + str11, new object[0]);
             if (!WildcardPattern.ContainsWildcardCharacters(str11) && !this.DiscoveredExports.Contains(str11))
             {
                 this.DiscoveredExports.Add(str11);
             }
         }
         list2 = new List<string>();
         string str12 = this.GetParameterByNameOrPosition("Alias", -1, commandAst);
         list2.AddRange(this.ExtractArgumentList(str12));
         foreach (string str13 in list2)
         {
             this.DiscoveredCommandFilters.Add(str13);
             if (!WildcardPattern.ContainsWildcardCharacters(str13) && !this.DiscoveredAliases.ContainsKey(str13))
             {
                 this.DiscoveredAliases.Add(str13, null);
             }
         }
     }
     if (string.Equals(commandName, "public", StringComparison.OrdinalIgnoreCase) && (commandAst.CommandElements.Count > 2))
     {
         string str14 = commandAst.CommandElements[2].ToString().Trim();
         this.DiscoveredExports.Add(str14);
         this.DiscoveredCommandFilters.Add(str14);
     }
     return AstVisitAction.Continue;
 }