Skip to content

Change CommandInfo lookup for commands in the form module\cmdletName #2125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Engine/CommandInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,43 @@ public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes
/// <returns>Returns null if command does not exists</returns>
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
{
string moduleName = null;
string actualCmdName = cmdName;

// Check if cmdName is in the format "moduleName\CmdletName" (exactly one backslash)
int backslashIndex = cmdName.IndexOf('\\');
if (
backslashIndex > 0 &&
backslashIndex == cmdName.LastIndexOf('\\') &&
backslashIndex != cmdName.Length - 1 &&
backslashIndex != 0
)
{
moduleName = cmdName.Substring(0, backslashIndex);
actualCmdName = cmdName.Substring(backslashIndex + 1);
}
// '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);
actualCmdName = WildcardPattern.Escape(actualCmdName);

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

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

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

if (!string.IsNullOrEmpty(moduleName))
{
ps.AddParameter("Module", moduleName);
}

return ps.Invoke<CommandInfo>()
.FirstOrDefault();
}
Expand Down