Skip to content

Use API on ScriptBlock to generate PSCommand #1285

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

Merged
merged 2 commits into from
May 6, 2020
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Management.Automation.Host;
Expand All @@ -16,15 +17,15 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices.Services
{
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;

/// <summary>
/// Manages the lifetime and usage of a PowerShell session.
Expand Down Expand Up @@ -1015,8 +1016,29 @@ public Task<IEnumerable<object>> ExecuteScriptStringAsync(
{
Validate.IsNotNull(nameof(scriptString), scriptString);

PSCommand command = null;
if(CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
{
try
{
var scriptBlock = ScriptBlock.Create(scriptString);
PowerShell powerShell = scriptBlock.GetPowerShell(isTrustedInput: false, null);
command = powerShell.Commands;
}
catch (Exception e)
{
logger.LogException("Exception getting trusted/untrusted PSCommand.", e);
}
}

// fall back to old behavior
if(command == null)
{
command = new PSCommand().AddScript(scriptString.Trim());
}

return this.ExecuteCommandAsync<object>(
new PSCommand().AddScript(scriptString.Trim()),
command,
errorMessages,
new ExecutionOptions()
{
Expand Down