internal static CommandInfo LookupCommandInfo(string commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context) { if (string.IsNullOrEmpty(commandName)) { return null; } CommandInfo result = null; string command = commandName; Exception lastError = null; CommandLookupEventArgs e = null; EventHandler<CommandLookupEventArgs> preCommandLookupAction = context.EngineIntrinsics.InvokeCommand.PreCommandLookupAction; if (preCommandLookupAction != null) { discoveryTracer.WriteLine("Executing PreCommandLookupAction: {0}", new object[] { commandName }); try { context.CommandDiscovery.RegisterLookupCommandInfoAction("ActivePreLookup", command); e = new CommandLookupEventArgs(command, commandOrigin, context); preCommandLookupAction(command, e); discoveryTracer.WriteLine("PreCommandLookupAction returned: {0}", new object[] { e.Command }); } catch (Exception exception2) { CommandProcessorBase.CheckForSevereException(exception2); } finally { context.CommandDiscovery.UnregisterLookupCommandInfoAction("ActivePreLookup", commandName); } } if ((e == null) || !e.StopSearch) { discoveryTracer.WriteLine("Looking up command: {0}", new object[] { commandName }); result = TryNormalSearch(commandName, context, commandOrigin, searchResolutionOptions, commandTypes, ref lastError); if (result == null) { PSModuleAutoLoadingPreference preference = GetCommandDiscoveryPreference(context, SpecialVariables.PSModuleAutoLoadingPreferenceVarPath, "PSModuleAutoLoadingPreference"); if (preference != PSModuleAutoLoadingPreference.None) { result = TryModuleAutoLoading(commandName, context, command, commandOrigin, result, ref lastError); } if (result == null) { if (preference == PSModuleAutoLoadingPreference.All) { result = TryModuleAutoDiscovery(commandName, context, command, commandOrigin, searchResolutionOptions, commandTypes, ref lastError); } if (result == null) { result = InvokeCommandNotFoundHandler(commandName, context, command, commandOrigin, result); } } } } else if (e.Command != null) { result = e.Command; } if (result != null) { EventHandler<CommandLookupEventArgs> postCommandLookupAction = context.EngineIntrinsics.InvokeCommand.PostCommandLookupAction; if (postCommandLookupAction != null) { discoveryTracer.WriteLine("Executing PostCommandLookupAction: {0}", new object[] { command }); try { context.CommandDiscovery.RegisterLookupCommandInfoAction("ActivePostCommand", command); e = new CommandLookupEventArgs(command, commandOrigin, context) { Command = result }; postCommandLookupAction(command, e); if (e != null) { result = e.Command; discoveryTracer.WriteLine("PreCommandLookupAction returned: {0}", new object[] { e.Command }); } } catch (Exception exception3) { CommandProcessorBase.CheckForSevereException(exception3); } finally { context.CommandDiscovery.UnregisterLookupCommandInfoAction("ActivePostCommand", command); } } } if (result == null) { discoveryTracer.TraceError("'{0}' is not recognized as a cmdlet, function, operable program or script file.", new object[] { commandName }); CommandNotFoundException exception4 = new CommandNotFoundException(command, lastError, "CommandNotFoundException", DiscoveryExceptions.CommandNotFoundException, new object[0]); throw exception4; } return result; }
private static CommandInfo InvokeCommandNotFoundHandler(string commandName, ExecutionContext context, string originalCommandName, CommandOrigin commandOrigin, CommandInfo result) { EventHandler<CommandLookupEventArgs> commandNotFoundAction = context.EngineIntrinsics.InvokeCommand.CommandNotFoundAction; if (commandNotFoundAction != null) { discoveryTracer.WriteLine("Executing CommandNotFoundAction: {0}", new object[] { commandName }); try { context.CommandDiscovery.RegisterLookupCommandInfoAction("ActiveCommandNotFound", originalCommandName); CommandLookupEventArgs e = new CommandLookupEventArgs(originalCommandName, commandOrigin, context); commandNotFoundAction(originalCommandName, e); result = e.Command; } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); } finally { context.CommandDiscovery.UnregisterLookupCommandInfoAction("ActiveCommandNotFound", originalCommandName); } } return result; }