Skip to content
Merged
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
65 changes: 42 additions & 23 deletions src/System.Management.Automation/engine/MshCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,40 +670,46 @@ internal IEnumerable<CommandInfo> GetCommands(string name, CommandTypes commandT
}

/// <summary>
/// Executes a piece of text as a script synchronously.
/// Executes a piece of text as a script synchronously in the caller's session state.
/// The given text will be executed in a child scope rather than dot-sourced.
/// </summary>
/// <param name="script">The script text to evaluate.</param>
/// <returns>A collection of MshCobjects generated by the script.</returns>
/// <returns>A collection of MshCobjects generated by the script. Never null, but may be empty.</returns>
/// <exception cref="ParseException">Thrown if there was a parsing error in the script.</exception>
/// <exception cref="RuntimeException">Represents a script-level exception.</exception>
/// <exception cref="FlowControlException"></exception>
public Collection<PSObject> InvokeScript(string script)
{
return InvokeScript(script, true, PipelineResultTypes.None, null);
return InvokeScript(script, useNewScope: true, PipelineResultTypes.None, input: null);
}

/// <summary>
/// Executes a piece of text as a script synchronously.
/// Executes a piece of text as a script synchronously in the caller's session state.
/// The given text will be executed in a child scope rather than dot-sourced.
/// </summary>
/// <param name="script">The script text to evaluate.</param>
/// <param name="args">The arguments to the script.</param>
/// <returns>A collection of MshCobjects generated by the script.</returns>
/// <param name="args">The arguments to the script, available as $args.</param>
/// <returns>A collection of MshCobjects generated by the script. Never null, but may be empty.</returns>
/// <exception cref="ParseException">Thrown if there was a parsing error in the script.</exception>
/// <exception cref="RuntimeException">Represents a script-level exception.</exception>
/// <exception cref="FlowControlException"></exception>
public Collection<PSObject> InvokeScript(string script, params object[] args)
{
return InvokeScript(script, true, PipelineResultTypes.None, null, args);
return InvokeScript(script, useNewScope: true, PipelineResultTypes.None, input: null, args);
}

/// <summary>
/// Executes a given scriptblock synchonously in the given session state.
/// The scriptblock will be executed in the calling scope (dot-sourced) rather than in a new child scope.
/// </summary>
/// <param name="sessionState"></param>
/// <param name="scriptBlock"></param>
/// <param name="args"></param>
/// <returns></returns>
/// <param name="sessionState">The session state in which to execute the scriptblock.</param>
/// <param name="scriptBlock">The scriptblock to execute.</param>
/// <param name="args">The arguments to the scriptblock, available as $args.</param>
/// <returns>A collection of the PSObjects emitted by the executing scriptblock. Never null, but may be empty.</returns>
public Collection<PSObject> InvokeScript(
SessionState sessionState, ScriptBlock scriptBlock, params object[] args)
SessionState sessionState,
ScriptBlock scriptBlock,
params object[] args)
{
if (scriptBlock == null)
{
Expand Down Expand Up @@ -735,13 +741,18 @@ public Collection<PSObject> InvokeScript(
/// <summary>
/// Invoke a scriptblock in the current runspace, controlling if it gets a new scope.
/// </summary>
/// <param name="useLocalScope">If true, a new scope will be created.</param>
/// <param name="useLocalScope">If true, executes the scriptblock in a new child scope, otherwise the scriptblock is dot-sourced into the calling scope.</param>
/// <param name="scriptBlock">The scriptblock to execute.</param>
/// <param name="input">Optionall input to the command.</param>
/// <param name="args">Arguments to pass to the scriptblock.</param>
/// <returns>The result of the evaluation.</returns>
/// <returns>
/// A collection of the PSObjects generated by executing the script. Never null, but may be empty.
/// </returns>
public Collection<PSObject> InvokeScript(
bool useLocalScope, ScriptBlock scriptBlock, IList input, params object[] args)
bool useLocalScope,
ScriptBlock scriptBlock,
IList input,
params object[] args)
{
if (scriptBlock == null)
{
Expand All @@ -767,21 +778,25 @@ public Collection<PSObject> InvokeScript(
/// </summary>
/// <param name="script">The script to evaluate.</param>
/// <param name="useNewScope">If true, evaluate the script in its own scope.
/// If false, the script will be evaluated in the current scope i.e. it will be "dotted"</param>
/// If false, the script will be evaluated in the current scope i.e. it will be dot-sourced.</param>
/// <param name="writeToPipeline">If set to Output, all output will be streamed
/// to the output pipe of the calling cmdlet. If set to None, the result will be returned
/// to the caller as a collection of PSObjects. No other flags are supported at this time and
/// will result in an exception if used.</param>
/// <param name="input">The list of objects to use as input to the script.</param>
/// <param name="args">The array of arguments to the command.</param>
/// <returns>A collection of MshCobjects generated by the script. This will be
/// empty if output was redirected.</returns>
/// <param name="args">The array of arguments to the command, available as $args.</param>
/// <returns>A collection of PSObjects generated by the script. This will be
/// empty if output was redirected. Never null.</returns>
/// <exception cref="ParseException">Thrown if there was a parsing error in the script.</exception>
/// <exception cref="RuntimeException">Represents a script-level exception.</exception>
/// <exception cref="NotImplementedException">Thrown if any redirect other than output is attempted.</exception>
/// <exception cref="FlowControlException"></exception>
public Collection<PSObject> InvokeScript(string script, bool useNewScope,
PipelineResultTypes writeToPipeline, IList input, params object[] args)
public Collection<PSObject> InvokeScript(
string script,
bool useNewScope,
PipelineResultTypes writeToPipeline,
IList input,
params object[] args)
{
if (script == null)
throw new ArgumentNullException(nameof(script));
Expand All @@ -792,8 +807,12 @@ public Collection<PSObject> InvokeScript(string script, bool useNewScope,
return InvokeScript(sb, useNewScope, writeToPipeline, input, args);
}

private Collection<PSObject> InvokeScript(ScriptBlock sb, bool useNewScope,
PipelineResultTypes writeToPipeline, IList input, params object[] args)
private Collection<PSObject> InvokeScript(
ScriptBlock sb,
bool useNewScope,
PipelineResultTypes writeToPipeline,
IList input,
params object[] args)
{
if (_cmdlet != null)
_cmdlet.ThrowIfStopping();
Expand Down