/// <summary> /// This is the constructor for script as cmdlet. /// </summary> /// <param name="scriptCommandInfo"> /// The information about the cmdlet. /// </param> /// <param name="context"> /// PowerShell engine execution context for this command. /// </param> /// <param name="useLocalScope"></param> /// <param name="sessionState"></param> /// <param name="fromScriptFile">True when the script to be executed came from a file (as opposed to a function, or interactive input)</param> internal CommandProcessor(IScriptCommandInfo scriptCommandInfo, ExecutionContext context, bool useLocalScope, bool fromScriptFile, SessionStateInternal sessionState) : base(scriptCommandInfo as CommandInfo) { this._context = context; this._useLocalScope = useLocalScope; this._fromScriptFile = fromScriptFile; this.CommandSessionState = sessionState; Init(scriptCommandInfo); }
private void GetMergedCommandParameterMetadata(out MergedCommandParameterMetadata result) { // MSFT:652277 - When invoking cmdlets or advanced functions, MyInvocation.MyCommand.Parameters do not contain the dynamic parameters // When trying to get parameter metadata for a CommandInfo that has dynamic parameters, a new CommandProcessor will be // created out of this CommandInfo and the parameter binding algorithm will be invoked. However, when this happens via // 'MyInvocation.MyCommand.Parameter', it's actually retrieving the parameter metadata of the same cmdlet that is currently // running. In this case, information about the specified parameters are not kept around in 'MyInvocation.MyCommand', so // going through the binding algorithm again won't give us the metadata about the dynamic parameters that should have been // discovered already. // The fix is to check if the CommandInfo is actually representing the currently running cmdlet. If so, the retrieval of parameter // metadata actually stems from the running of the same cmdlet. In this case, we can just use the current CommandProcessor to // retrieve all bindable parameters, which should include the dynamic parameters that have been discovered already. CommandProcessor processor; if (Context.CurrentCommandProcessor != null && Context.CurrentCommandProcessor.CommandInfo == this) { // Accessing the parameters within the invocation of the same cmdlet/advanced function. processor = (CommandProcessor)Context.CurrentCommandProcessor; } else { IScriptCommandInfo scriptCommand = this as IScriptCommandInfo; processor = scriptCommand != null ? new CommandProcessor(scriptCommand, _context, useLocalScope: true, fromScriptFile: false, sessionState: scriptCommand.ScriptBlock.SessionStateInternal ?? Context.EngineSessionState) : new CommandProcessor((CmdletInfo)this, _context) { UseLocalScope = true }; ParameterBinderController.AddArgumentsToCommandProcessor(processor, Arguments); CommandProcessorBase oldCurrentCommandProcessor = Context.CurrentCommandProcessor; try { Context.CurrentCommandProcessor = processor; processor.SetCurrentScopeToExecutionScope(); processor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(processor.arguments); } catch (ParameterBindingException) { // Ignore the binding exception if no argument is specified if (processor.arguments.Count > 0) { throw; } } finally { Context.CurrentCommandProcessor = oldCurrentCommandProcessor; processor.RestorePreviousScope(); } } result = processor.CmdletParameterBinderController.BindableParameters; }
internal CommandProcessor( IScriptCommandInfo scriptCommandInfo, ExecutionContext context, bool useLocalScope) : base(scriptCommandInfo as CommandInfo) { this.context = context; this._useLocalScope = useLocalScope; this.Init(scriptCommandInfo); }
protected ScriptCommandProcessorBase(IScriptCommandInfo commandInfo, ExecutionContext context, bool useLocalScope, SessionStateInternal sessionState) : base((CommandInfo)commandInfo) { Diagnostics.Assert(commandInfo != null, "commandInfo cannot be null"); Diagnostics.Assert(commandInfo.ScriptBlock != null, "scriptblock cannot be null"); this._fromScriptFile = (this.CommandInfo is ExternalScriptInfo || this.CommandInfo is ScriptInfo); this._dontUseScopeCommandOrigin = true; CommonInitialization(commandInfo.ScriptBlock, context, useLocalScope, CommandOrigin.Internal, sessionState); }
private void Init(IScriptCommandInfo scriptCommandInfo) { InternalCommand command = new PSScriptCmdlet(scriptCommandInfo.ScriptBlock, base._useLocalScope, base.FromScriptFile, base._context); base.Command = command; base.CommandScope = base._useLocalScope ? base.CommandSessionState.NewScope(base._fromScriptFile) : base.CommandSessionState.CurrentScope; this.InitCommon(); if (!base.UseLocalScope) { CommandProcessorBase.ValidateCompatibleLanguageMode(scriptCommandInfo.ScriptBlock, base._context.LanguageMode, base.Command.MyInvocation); } }
private void Init(IScriptCommandInfo scriptCommandInfo) { PSScriptCmdlet psScriptCmdlet = new PSScriptCmdlet(scriptCommandInfo.ScriptBlock); psScriptCmdlet.UseLocalScope = this._useLocalScope; if (scriptCommandInfo.ScriptBlock.SessionStateInternal != null) { this.CommandSessionState = scriptCommandInfo.ScriptBlock.SessionStateInternal; } this.Command = (InternalCommand)psScriptCmdlet; this.InitCommon(); }
internal override IEnumerable <HelpInfo> ExactMatchHelp(HelpRequest helpRequest) { int iteratorVariable0 = 0; string target = helpRequest.Target; Hashtable iteratorVariable2 = new Hashtable(StringComparer.OrdinalIgnoreCase); CommandSearcher commandSearcherForExactMatch = this.GetCommandSearcherForExactMatch(target, this._context); Label_PostSwitchInIterator :; while (commandSearcherForExactMatch.MoveNext()) { CommandInfo current = commandSearcherForExactMatch.Current; if (SessionState.IsVisible(helpRequest.CommandOrigin, current)) { CmdletInfo cmdletInfo = current as CmdletInfo; HelpInfo helpInfo = null; string key = null; if (cmdletInfo != null) { helpInfo = this.GetHelpInfo(cmdletInfo, true); key = cmdletInfo.FullName; } else { IScriptCommandInfo scriptCommandInfo = current as IScriptCommandInfo; if (scriptCommandInfo != null) { key = current.Name; helpInfo = this.GetHelpInfo(scriptCommandInfo, true, false); } } if ((helpInfo != null) && (key != null)) { if ((helpInfo.ForwardHelpCategory == helpRequest.HelpCategory) && helpInfo.ForwardTarget.Equals(helpRequest.Target, StringComparison.OrdinalIgnoreCase)) { throw new PSInvalidOperationException(HelpErrors.CircularDependencyInHelpForwarding); } if (!iteratorVariable2.ContainsKey(key) && Match(helpInfo, helpRequest, current)) { iteratorVariable0++; iteratorVariable2.Add(key, null); yield return(helpInfo); if ((iteratorVariable0 >= helpRequest.MaxResults) && (helpRequest.MaxResults > 0)) { break; } goto Label_PostSwitchInIterator; } } } } }
private static CommandProcessorBase GetScriptAsCmdletProcessor( IScriptCommandInfo scriptCommandInfo, ExecutionContext context, bool useNewScope, bool fromScriptFile) { if (scriptCommandInfo.ScriptBlock == null || !scriptCommandInfo.ScriptBlock.UsesCmdletBinding) { return((CommandProcessorBase)null); } CommandProcessor commandProcessor = new CommandProcessor(scriptCommandInfo, context, useNewScope); commandProcessor.FromScriptFile = fromScriptFile; ((PSScriptCmdlet)commandProcessor.Command).FromScriptFile = fromScriptFile; return((CommandProcessorBase)commandProcessor); }
internal RemoteHelpInfo GetRemoteHelpInfo(ExecutionContext context, CommandInfo commandInfo) { PSSession session; if (string.IsNullOrEmpty(this._sections.ForwardHelpTargetName) || string.IsNullOrEmpty(this._sections.RemoteHelpRunspace)) { return(null); } IScriptCommandInfo info = (IScriptCommandInfo)commandInfo; object valueToConvert = info.ScriptBlock.SessionState.PSVariable.GetValue(this._sections.RemoteHelpRunspace); if ((valueToConvert == null) || !LanguagePrimitives.TryConvertTo <PSSession>(valueToConvert, out session)) { throw new InvalidOperationException(HelpErrors.RemoteRunspaceNotAvailable); } return(new RemoteHelpInfo(context, (RemoteRunspace)session.Runspace, commandInfo.Name, this._sections.ForwardHelpTargetName, this._sections.ForwardHelpCategory, commandInfo.HelpCategory)); }
private void Init(IScriptCommandInfo scriptCommandInfo) { InternalCommand scriptCmdlet = new PSScriptCmdlet(scriptCommandInfo.ScriptBlock, _useLocalScope, FromScriptFile, _context); this.Command = scriptCmdlet; this.CommandScope = _useLocalScope ? this.CommandSessionState.NewScope(_fromScriptFile) : this.CommandSessionState.CurrentScope; InitCommon(); // If the script has been dotted, throw an error if it's from a different language mode. if (!this.UseLocalScope) { ValidateCompatibleLanguageMode(scriptCommandInfo.ScriptBlock, _context.LanguageMode, Command.MyInvocation); } }
internal ParameterBinderController NewParameterBinderController(InternalCommand command) { ParameterBinderBase base2; Cmdlet cmdlet = command as Cmdlet; if (cmdlet == null) { throw PSTraceSource.NewArgumentException("command"); } IScriptCommandInfo commandInfo = base.CommandInfo as IScriptCommandInfo; if (commandInfo != null) { base2 = new ScriptParameterBinder(commandInfo.ScriptBlock, cmdlet.MyInvocation, base._context, cmdlet, base.CommandScope); } else { base2 = new ReflectionParameterBinder(cmdlet, cmdlet); } this._cmdletParameterBinderController = new System.Management.Automation.CmdletParameterBinderController(cmdlet, base.CommandInfo.CommandMetadata, base2); return(this._cmdletParameterBinderController); }
/// <summary> /// Returns a CmdletParameterBinderController for the specified command. /// </summary> /// <param name="command"> /// The cmdlet to bind parameters to. /// </param> /// <returns> /// A new instance of a CmdletParameterBinderController. /// </returns> /// <exception cref="ArgumentException"> /// if <paramref name="command"/> is not a Cmdlet. /// </exception> internal ParameterBinderController NewParameterBinderController(InternalCommand command) { if (!(command is Cmdlet cmdlet)) { throw PSTraceSource.NewArgumentException(nameof(command)); } ParameterBinderBase parameterBinder; IScriptCommandInfo scriptCommandInfo = CommandInfo as IScriptCommandInfo; if (scriptCommandInfo != null) { parameterBinder = new ScriptParameterBinder(scriptCommandInfo.ScriptBlock, cmdlet.MyInvocation, this._context, cmdlet, CommandScope); } else { parameterBinder = new ReflectionParameterBinder(cmdlet, cmdlet); } _cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, CommandInfo.CommandMetadata, parameterBinder); return(_cmdletParameterBinderController); }
private void Init(IScriptCommandInfo scriptCommandInfo) { var scriptCmdlet = new PSScriptCmdlet(scriptCommandInfo.ScriptBlock, UseLocalScope, FromScriptFile, _context); this.Command = scriptCmdlet; this.CommandScope = UseLocalScope ? this.CommandSessionState.NewScope(_fromScriptFile) : this.CommandSessionState.CurrentScope; if (UseLocalScope) { // Set the 'LocalsTuple' of the new scope to that of the scriptCmdlet scriptCmdlet.SetLocalsTupleForNewScope(CommandScope); } InitCommon(); // If the script has been dotted, throw an error if it's from a different language mode. if (!this.UseLocalScope) { ValidateCompatibleLanguageMode(scriptCommandInfo.ScriptBlock, _context.LanguageMode, Command.MyInvocation); } }
protected ScriptCommandProcessorBase(IScriptCommandInfo commandInfo, ExecutionContext context, bool useLocalScope, SessionStateInternal sessionState) : base((CommandInfo) commandInfo) { base._fromScriptFile = (base.CommandInfo is ExternalScriptInfo) || (base.CommandInfo is ScriptInfo); this._dontUseScopeCommandOrigin = true; this.CommonInitialization(commandInfo.ScriptBlock, context, useLocalScope, sessionState); }
private static CommandProcessorBase GetScriptAsCmdletProcessor(IScriptCommandInfo scriptCommandInfo, ExecutionContext context, bool useNewScope, bool fromScriptFile, SessionStateInternal sessionState) { if ((scriptCommandInfo.ScriptBlock == null) || !scriptCommandInfo.ScriptBlock.UsesCmdletBinding) { return null; } sessionState = sessionState ?? (scriptCommandInfo.ScriptBlock.SessionStateInternal ?? context.EngineSessionState); return new CommandProcessor(scriptCommandInfo, context, useNewScope, fromScriptFile, sessionState); }
private HelpInfo GetHelpInfo(IScriptCommandInfo scriptCommandInfo, bool reportErrors, bool searchOnlyContent) { CommandInfo commandInfo = (CommandInfo) scriptCommandInfo; HelpInfo helpInfoFromWorkflow = null; ScriptBlock scriptBlock = null; try { scriptBlock = scriptCommandInfo.ScriptBlock; } catch (RuntimeException) { return null; } if (scriptBlock != null) { string helpFile = null; string str2 = null; string helpUriFromDotLink = null; helpInfoFromWorkflow = scriptBlock.GetHelpInfo(this._context, commandInfo, searchOnlyContent, base.HelpSystem.ScriptBlockTokenCache, out helpFile, out helpUriFromDotLink); if (!string.IsNullOrEmpty(helpUriFromDotLink)) { try { new Uri(helpUriFromDotLink); str2 = helpUriFromDotLink; } catch (UriFormatException) { } } if (helpInfoFromWorkflow != null) { Uri uriForOnlineHelp = helpInfoFromWorkflow.GetUriForOnlineHelp(); if (uriForOnlineHelp != null) { str2 = uriForOnlineHelp.ToString(); } } if (helpFile != null) { if (!this._helpFiles.Contains(helpFile)) { this.LoadHelpFile(helpFile, helpFile, commandInfo.Name, reportErrors); } helpInfoFromWorkflow = this.GetFromCommandCache(helpFile, commandInfo) ?? helpInfoFromWorkflow; } if (helpInfoFromWorkflow == null) { if ((commandInfo.CommandType == CommandTypes.ExternalScript) || (commandInfo.CommandType == CommandTypes.Script)) { helpInfoFromWorkflow = SyntaxHelpInfo.GetHelpInfo(commandInfo.Name, commandInfo.Syntax, commandInfo.HelpCategory); } else { if (commandInfo.CommandType == CommandTypes.Workflow) { helpInfoFromWorkflow = this.GetHelpInfoFromWorkflow(commandInfo, reportErrors); } if (helpInfoFromWorkflow == null) { PSObject pSObjectFromCmdletInfo = DefaultCommandHelpObjectBuilder.GetPSObjectFromCmdletInfo(commandInfo); pSObjectFromCmdletInfo.TypeNames.Clear(); pSObjectFromCmdletInfo.TypeNames.Add(DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp); pSObjectFromCmdletInfo.TypeNames.Add("CmdletHelpInfo"); pSObjectFromCmdletInfo.TypeNames.Add("HelpInfo"); helpInfoFromWorkflow = new MamlCommandHelpInfo(pSObjectFromCmdletInfo, commandInfo.HelpCategory); } } } if (helpInfoFromWorkflow.GetUriForOnlineHelp() == null) { if (!string.IsNullOrEmpty(commandInfo.CommandMetadata.HelpUri)) { DefaultCommandHelpObjectBuilder.AddRelatedLinksProperties(helpInfoFromWorkflow.FullHelp, commandInfo.CommandMetadata.HelpUri); } else if (!string.IsNullOrEmpty(str2)) { DefaultCommandHelpObjectBuilder.AddRelatedLinksProperties(helpInfoFromWorkflow.FullHelp, str2); } } } if ((helpInfoFromWorkflow != null) && (helpInfoFromWorkflow.FullHelp.Properties["ModuleName"] == null)) { helpInfoFromWorkflow.FullHelp.Properties.Add(new PSNoteProperty("ModuleName", commandInfo.ModuleName)); } return helpInfoFromWorkflow; }
protected ScriptCommandProcessorBase(IScriptCommandInfo commandInfo, ExecutionContext context, bool useLocalScope, SessionStateInternal sessionState) : base((CommandInfo)commandInfo) { base._fromScriptFile = (base.CommandInfo is ExternalScriptInfo) || (base.CommandInfo is ScriptInfo); this._dontUseScopeCommandOrigin = true; this.CommonInitialization(commandInfo.ScriptBlock, context, useLocalScope, sessionState); }
internal override IEnumerable <HelpInfo> SearchHelp(HelpRequest helpRequest, bool searchOnlyContent) { string item = helpRequest.Target; Collection <string> iteratorVariable1 = new Collection <string>(); WildcardPattern pattern = null; bool iteratorVariable3 = !WildcardPattern.ContainsWildcardCharacters(helpRequest.Target); if (!searchOnlyContent) { if (iteratorVariable3) { if (item.IndexOf('-') >= 0) { iteratorVariable1.Add(item + "*"); } else { iteratorVariable1.Add("*" + item + "*"); } } else { iteratorVariable1.Add(item); } } else { iteratorVariable1.Add("*"); string target = helpRequest.Target; if (iteratorVariable3) { target = "*" + helpRequest.Target + "*"; } pattern = new WildcardPattern(target, WildcardOptions.IgnoreCase | WildcardOptions.Compiled); } int iteratorVariable4 = 0; Hashtable iteratorVariable5 = new Hashtable(StringComparer.OrdinalIgnoreCase); Hashtable iteratorVariable6 = new Hashtable(StringComparer.OrdinalIgnoreCase); foreach (string iteratorVariable7 in iteratorVariable1) { CommandSearcher commandSearcherForSearch = this.GetCommandSearcherForSearch(iteratorVariable7, this._context); while (commandSearcherForSearch.MoveNext()) { if (this._context.CurrentPipelineStopping) { break; } CommandInfo current = commandSearcherForSearch.Current; CmdletInfo cmdletInfo = current as CmdletInfo; HelpInfo helpInfo = null; string key = null; if (cmdletInfo != null) { helpInfo = this.GetHelpInfo(cmdletInfo, !iteratorVariable3); key = cmdletInfo.FullName; } else { IScriptCommandInfo scriptCommandInfo = current as IScriptCommandInfo; if (scriptCommandInfo != null) { key = current.Name; helpInfo = this.GetHelpInfo(scriptCommandInfo, !iteratorVariable3, searchOnlyContent); } } if (helpInfo != null) { if (!SessionState.IsVisible(helpRequest.CommandOrigin, current)) { if (!iteratorVariable6.ContainsKey(key)) { iteratorVariable6.Add(key, null); } } else if ((!iteratorVariable5.ContainsKey(key) && Match(helpInfo, helpRequest, current)) && (!searchOnlyContent || helpInfo.MatchPatternInContent(pattern))) { iteratorVariable5.Add(key, null); iteratorVariable4++; yield return(helpInfo); if ((iteratorVariable4 < helpRequest.MaxResults) || (helpRequest.MaxResults <= 0)) { continue; } break; } } } if (this.HelpCategory == (System.Management.Automation.HelpCategory.Cmdlet | System.Management.Automation.HelpCategory.Alias)) { foreach (CommandInfo iteratorVariable13 in ModuleUtils.GetMatchingCommands(iteratorVariable7, this._context, helpRequest.CommandOrigin, false)) { if (this._context.CurrentPipelineStopping) { break; } if (SessionState.IsVisible(helpRequest.CommandOrigin, iteratorVariable13)) { CmdletInfo iteratorVariable14 = iteratorVariable13 as CmdletInfo; HelpInfo iteratorVariable15 = null; string fullName = null; if (iteratorVariable14 != null) { iteratorVariable15 = this.GetHelpInfo(iteratorVariable14, !iteratorVariable3); fullName = iteratorVariable14.FullName; } else { IScriptCommandInfo info2 = iteratorVariable13 as IScriptCommandInfo; if (info2 != null) { fullName = iteratorVariable13.Name; iteratorVariable15 = this.GetHelpInfo(info2, !iteratorVariable3, searchOnlyContent); } } if ((((iteratorVariable15 != null) && !iteratorVariable5.ContainsKey(fullName)) && (!iteratorVariable6.ContainsKey(fullName) && Match(iteratorVariable15, helpRequest, iteratorVariable13))) && (!searchOnlyContent || iteratorVariable15.MatchPatternInContent(pattern))) { iteratorVariable5.Add(fullName, null); iteratorVariable4++; yield return(iteratorVariable15); if ((iteratorVariable4 >= helpRequest.MaxResults) && (helpRequest.MaxResults > 0)) { break; } } } } } } }
private HelpInfo GetHelpInfo(IScriptCommandInfo scriptCommandInfo, bool reportErrors, bool searchOnlyContent) { CommandInfo commandInfo = (CommandInfo)scriptCommandInfo; HelpInfo helpInfoFromWorkflow = null; ScriptBlock scriptBlock = null; try { scriptBlock = scriptCommandInfo.ScriptBlock; } catch (RuntimeException) { return(null); } if (scriptBlock != null) { string helpFile = null; string str2 = null; string helpUriFromDotLink = null; helpInfoFromWorkflow = scriptBlock.GetHelpInfo(this._context, commandInfo, searchOnlyContent, base.HelpSystem.ScriptBlockTokenCache, out helpFile, out helpUriFromDotLink); if (!string.IsNullOrEmpty(helpUriFromDotLink)) { try { new Uri(helpUriFromDotLink); str2 = helpUriFromDotLink; } catch (UriFormatException) { } } if (helpInfoFromWorkflow != null) { Uri uriForOnlineHelp = helpInfoFromWorkflow.GetUriForOnlineHelp(); if (uriForOnlineHelp != null) { str2 = uriForOnlineHelp.ToString(); } } if (helpFile != null) { if (!this._helpFiles.Contains(helpFile)) { this.LoadHelpFile(helpFile, helpFile, commandInfo.Name, reportErrors); } helpInfoFromWorkflow = this.GetFromCommandCache(helpFile, commandInfo) ?? helpInfoFromWorkflow; } if (helpInfoFromWorkflow == null) { if ((commandInfo.CommandType == CommandTypes.ExternalScript) || (commandInfo.CommandType == CommandTypes.Script)) { helpInfoFromWorkflow = SyntaxHelpInfo.GetHelpInfo(commandInfo.Name, commandInfo.Syntax, commandInfo.HelpCategory); } else { if (commandInfo.CommandType == CommandTypes.Workflow) { helpInfoFromWorkflow = this.GetHelpInfoFromWorkflow(commandInfo, reportErrors); } if (helpInfoFromWorkflow == null) { PSObject pSObjectFromCmdletInfo = DefaultCommandHelpObjectBuilder.GetPSObjectFromCmdletInfo(commandInfo); pSObjectFromCmdletInfo.TypeNames.Clear(); pSObjectFromCmdletInfo.TypeNames.Add(DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp); pSObjectFromCmdletInfo.TypeNames.Add("CmdletHelpInfo"); pSObjectFromCmdletInfo.TypeNames.Add("HelpInfo"); helpInfoFromWorkflow = new MamlCommandHelpInfo(pSObjectFromCmdletInfo, commandInfo.HelpCategory); } } } if (helpInfoFromWorkflow.GetUriForOnlineHelp() == null) { if (!string.IsNullOrEmpty(commandInfo.CommandMetadata.HelpUri)) { DefaultCommandHelpObjectBuilder.AddRelatedLinksProperties(helpInfoFromWorkflow.FullHelp, commandInfo.CommandMetadata.HelpUri); } else if (!string.IsNullOrEmpty(str2)) { DefaultCommandHelpObjectBuilder.AddRelatedLinksProperties(helpInfoFromWorkflow.FullHelp, str2); } } } if ((helpInfoFromWorkflow != null) && (helpInfoFromWorkflow.FullHelp.Properties["ModuleName"] == null)) { helpInfoFromWorkflow.FullHelp.Properties.Add(new PSNoteProperty("ModuleName", commandInfo.ModuleName)); } return(helpInfoFromWorkflow); }