Skip to content

Commit dc8786e

Browse files
committed
Change how looking up CommandInfo works when using commands in the form module\cmdletName
1 parent ea70855 commit dc8786e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Engine/CommandInfoCache.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,43 @@ public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes
8080
/// <returns>Returns null if command does not exists</returns>
8181
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
8282
{
83+
string moduleName = null;
84+
string actualCmdName = cmdName;
85+
86+
// Check if cmdName is in the format "moduleName\CmdletName" (exactly one backslash)
87+
int backslashIndex = cmdName.IndexOf('\\');
88+
if (
89+
backslashIndex > 0 &&
90+
backslashIndex == cmdName.LastIndexOf('\\') &&
91+
backslashIndex != cmdName.Length - 1 &&
92+
backslashIndex != 0
93+
)
94+
{
95+
moduleName = cmdName.Substring(0, backslashIndex);
96+
actualCmdName = cmdName.Substring(backslashIndex + 1);
97+
}
8398
// 'Get-Command ?' would return % for example due to PowerShell interpreting is a single-character-wildcard search and not just the ? alias.
8499
// For more details see https://github.com/PowerShell/PowerShell/issues/9308
85-
cmdName = WildcardPattern.Escape(cmdName);
100+
actualCmdName = WildcardPattern.Escape(actualCmdName);
86101

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

91106
ps.AddCommand("Get-Command")
92-
.AddParameter("Name", cmdName)
107+
.AddParameter("Name", actualCmdName)
93108
.AddParameter("ErrorAction", "SilentlyContinue");
94109

95110
if (commandType != null)
96111
{
97112
ps.AddParameter("CommandType", commandType);
98113
}
99114

115+
if (!string.IsNullOrEmpty(moduleName))
116+
{
117+
ps.AddParameter("Module", moduleName);
118+
}
119+
100120
return ps.Invoke<CommandInfo>()
101121
.FirstOrDefault();
102122
}

0 commit comments

Comments
 (0)