Ejemplo n.º 1
0
        public CommandInfo GetCommandInfoLegacy(string commandOrAliasName, CommandTypes?commandTypes = null)
        {
            string commandName = _helperInstance.GetCmdletNameFromAlias(commandOrAliasName);

            return(string.IsNullOrEmpty(commandName)
                ? GetCommandInfo(commandOrAliasName, commandTypes: commandTypes)
                : GetCommandInfo(commandName, aliasName: commandOrAliasName, commandTypes: commandTypes));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieve a command info object about a command.
        /// </summary>
        /// <param name="commandName">Name of the command to get a commandinfo object for.</param>
        /// <param name="commandTypes">What types of command are needed. If omitted, all types are retrieved.</param>
        /// <returns></returns>
        public CommandInfo GetCommandInfo(string commandName, CommandTypes?commandTypes = null)
        {
            if (string.IsNullOrWhiteSpace(commandName))
            {
                return(null);
            }

            var key = new CommandLookupKey(commandName, commandTypes);

            // Atomically either use PowerShell to query a command info object, or fetch it from the cache
            return(_commandInfoCache.GetOrAdd(key, new Lazy <CommandInfo>(() => GetCommandInfoInternal(commandName, commandTypes))).Value);
        }
Ejemplo n.º 3
0
        }                                       // null means Query
        public UserContext(AuthModel user, IStorageModel model, CommandTypes?type, IAuthStorageModel authStorage, params ApplicationClaim[] claims)
            : base(model)
        {
            string msg = $"ctor of UserContext for [{type.ToString()}]";

            SanityChecks.CheckNull(model, msg);
            SanityChecks.CheckNull(user, msg);
            SanityChecks.CheckNull(authStorage, msg);

            Type        = type;
            Claims      = claims;
            AuthModel   = user;
            AuthStorage = authStorage;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a CommandInfo object of the given command name
        /// </summary>
        /// <returns>Returns null if command does not exists</returns>
        private static CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes?commandType)
        {
            using (var ps = System.Management.Automation.PowerShell.Create())
            {
                ps.AddCommand("Get-Command")
                .AddParameter("Name", cmdName)
                .AddParameter("ErrorAction", "SilentlyContinue");

                if (commandType != null)
                {
                    ps.AddParameter("CommandType", commandType);
                }

                return(ps.Invoke <CommandInfo>()
                       .FirstOrDefault());
            }
        }
Ejemplo n.º 5
0
            private static void AddCommandResult(CommandAndName commandAndName, bool useFullName, bool completingAtStartOfLine, string quote, List <CompletionResult> results)
            {
                string       completionText = useFullName ? commandAndName.CommandName.FullName : commandAndName.CommandName.ShortName;
                string       str2           = AddQuoteIfNecessary(completionText, quote, completingAtStartOfLine);
                CommandTypes?nullable       = SafeGetProperty <CommandTypes?>(commandAndName.Command, "CommandType");

                if (nullable.HasValue)
                {
                    string str3;
                    string listItemText = SafeGetProperty <string>(commandAndName.Command, "Name");
                    if ((((CommandTypes)nullable.Value) == CommandTypes.Cmdlet) || (((CommandTypes)nullable.Value) == CommandTypes.Application))
                    {
                        str3 = SafeGetProperty <string>(commandAndName.Command, "Definition");
                    }
                    else
                    {
                        str3 = listItemText;
                    }
                    results.Add(new CompletionResult(str2, listItemText, CompletionResultType.Command, str3));
                }
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Get a CommandInfo object of the given command name
        /// </summary>
        /// <returns>Returns null if command does not exists</returns>
        private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes?commandType)
        {
            // 'Get-Command ?' would return % for example due to PowerShell interpreting is a single-character-wildcard search and not just the ? alias.
            // For more details see https://github.com/PowerShell/PowerShell/issues/9308
            cmdName = WildcardPattern.Escape(cmdName);

            using (var ps = System.Management.Automation.PowerShell.Create())
            {
                ps.RunspacePool = _runspacePool;

                ps.AddCommand("Get-Command")
                .AddParameter("Name", cmdName)
                .AddParameter("ErrorAction", "SilentlyContinue");

                if (commandType != null)
                {
                    ps.AddParameter("CommandType", commandType);
                }

                return(ps.Invoke <CommandInfo>()
                       .FirstOrDefault());
            }
        }
Ejemplo n.º 7
0
 internal CommandLookupKey(string name, CommandTypes?commandTypes)
 {
     Name         = name;
     CommandTypes = commandTypes ?? CommandTypes.All;
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Retrieve a command info object about a command.
        /// </summary>
        /// <param name="commandName">Name of the command to get a commandinfo object for.</param>
        /// <param name="aliasName">The alias of the command to be used in the cache key. If not given, uses the command name.</param>
        /// <param name="commandTypes">What types of command are needed. If omitted, all types are retrieved.</param>
        /// <returns></returns>
        public CommandInfo GetCommandInfo(string commandName, string aliasName = null, CommandTypes?commandTypes = null)
        {
            if (string.IsNullOrWhiteSpace(commandName))
            {
                return(null);
            }

            // If alias name is given, we store the entry under that, but search with the command name
            var key = new CommandLookupKey(aliasName ?? commandName, commandTypes);

            // Atomically either use PowerShell to query a command info object, or fetch it from the cache
            return(_commandInfoCache.GetOrAdd(key, new Lazy <CommandInfo>(() => GetCommandInfoInternal(commandName, commandTypes))).Value);
        }