Adding verbose output for experimental implicit remoting batching feature (#8166)

This commit is contained in:
Paul Higinbotham 2018-11-05 15:22:46 -08:00 committed by Aditya Patwardhan
parent e1d8765f9e
commit acf5eb5053
2 changed files with 72 additions and 28 deletions

View file

@ -1382,6 +1382,10 @@ namespace System.Management.Automation
// c. Commands must be in a simple pipeline // c. Commands must be in a simple pipeline
internal static bool TryRunAsImplicitBatch(string command, Runspace runspace) internal static bool TryRunAsImplicitBatch(string command, Runspace runspace)
{ {
using (var ps = System.Management.Automation.PowerShell.Create())
{
ps.Runspace = runspace;
try try
{ {
var scriptBlock = ScriptBlock.Create(command); var scriptBlock = ScriptBlock.Create(command);
@ -1397,6 +1401,7 @@ namespace System.Management.Automation
scriptBlockAst.GetSimplePipeline(true, out errorId, out errorMsg); scriptBlockAst.GetSimplePipeline(true, out errorId, out errorMsg);
if (errorId != null) if (errorId != null)
{ {
WriteVerbose(ps, ParserStrings.ImplicitRemotingPipelineBatchingNotASimplePipeline);
return false; return false;
} }
@ -1411,9 +1416,6 @@ namespace System.Management.Automation
} }
// We have a valid batching candidate // We have a valid batching candidate
using (var ps = System.Management.Automation.PowerShell.Create())
{
ps.Runspace = runspace;
// Check commands // Check commands
if (!TryGetCommandInfoList(ps, checker.Commands, out Collection<CommandInfo> cmdInfoList)) if (!TryGetCommandInfoList(ps, checker.Commands, out Collection<CommandInfo> cmdInfoList))
@ -1436,6 +1438,7 @@ namespace System.Management.Automation
// Commands must be from implicit remoting module // Commands must be from implicit remoting module
if (cmdInfo.Module == null || string.IsNullOrEmpty(cmdInfo.ModuleName)) if (cmdInfo.Module == null || string.IsNullOrEmpty(cmdInfo.ModuleName))
{ {
WriteVerbose(ps, string.Format(CultureInfo.CurrentCulture, ParserStrings.ImplicitRemotingPipelineBatchingNotImplicitCommand, cmdInfo.Name));
success = false; success = false;
break; break;
} }
@ -1446,6 +1449,7 @@ namespace System.Management.Automation
var sessionIdString = privateData["ImplicitSessionId"] as string; var sessionIdString = privateData["ImplicitSessionId"] as string;
if (string.IsNullOrEmpty(sessionIdString)) if (string.IsNullOrEmpty(sessionIdString))
{ {
WriteVerbose(ps, string.Format(CultureInfo.CurrentCulture, ParserStrings.ImplicitRemotingPipelineBatchingNotImplicitCommand, cmdInfo.Name));
success = false; success = false;
break; break;
} }
@ -1457,12 +1461,14 @@ namespace System.Management.Automation
} }
else if (psSessionId != sessionId) else if (psSessionId != sessionId)
{ {
WriteVerbose(ps, string.Format(CultureInfo.CurrentCulture, ParserStrings.ImplicitRemotingPipelineBatchingWrongSession, cmdInfo.Name));
success = false; success = false;
break; break;
} }
} }
else else
{ {
WriteVerbose(ps, string.Format(CultureInfo.CurrentCulture, ParserStrings.ImplicitRemotingPipelineBatchingNotImplicitCommand, cmdInfo.Name));
success = false; success = false;
break; break;
} }
@ -1491,13 +1497,20 @@ namespace System.Management.Automation
var psSession = ps.Invoke<System.Management.Automation.Runspaces.PSSession>().FirstOrDefault(); var psSession = ps.Invoke<System.Management.Automation.Runspaces.PSSession>().FirstOrDefault();
if (psSession == null || (ps.Streams.Error.Count > 0) || (psSession.Availability != RunspaceAvailability.Available)) if (psSession == null || (ps.Streams.Error.Count > 0) || (psSession.Availability != RunspaceAvailability.Available))
{ {
WriteVerbose(ps, ParserStrings.ImplicitRemotingPipelineBatchingNoPSSession);
return false; return false;
} }
WriteVerbose(ps, ParserStrings.ImplicitRemotingPipelineBatchingSuccess);
// Create and invoke implicit remoting command pipeline // Create and invoke implicit remoting command pipeline
ps.Commands.Clear(); ps.Commands.Clear();
ps.AddCommand("Invoke-Command").AddParameter("Session", psSession).AddParameter("ScriptBlock", scriptBlock).AddParameter("HideComputerName", true) ps.AddCommand("Invoke-Command").AddParameter("Session", psSession).AddParameter("ScriptBlock", scriptBlock).AddParameter("HideComputerName", true)
.AddCommand("Out-Default"); .AddCommand("Out-Default");
foreach (var cmd in ps.Commands.Commands)
{
cmd.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
}
try try
{ {
@ -1514,12 +1527,25 @@ namespace System.Management.Automation
return true; return true;
} }
} }
catch (ImplicitRemotingBatchingNotSupportedException ex)
{
WriteVerbose(ps, string.Format(CultureInfo.CurrentCulture, "{0} : {1}", ex.Message, ex.ErrorId));
}
catch (Exception ex)
{
WriteVerbose(ps, string.Format(CultureInfo.CurrentCulture, ParserStrings.ImplicitRemotingPipelineBatchingException, ex.Message));
}
} }
catch (Exception) { }
return false; return false;
} }
private static void WriteVerbose(PowerShell ps, string msg)
{
ps.Commands.Clear();
ps.AddCommand("Write-Verbose").AddParameter("Message", msg).Invoke();
}
private const string WhereObjectCommandAlias = "?"; private const string WhereObjectCommandAlias = "?";
private static bool TryGetCommandInfoList(PowerShell ps, HashSet<string> commandNames, out Collection<CommandInfo> cmdInfoList) private static bool TryGetCommandInfoList(PowerShell ps, HashSet<string> commandNames, out Collection<CommandInfo> cmdInfoList)
{ {

View file

@ -1470,4 +1470,22 @@ ModuleVersion : Version of module to import. If used, ModuleName must represent
<data name="ImplicitRemotingPipelineBatchingNotSupported" xml:space="preserve"> <data name="ImplicitRemotingPipelineBatchingNotSupported" xml:space="preserve">
<value>Command pipeline not supported for implicit remoting batching.</value> <value>Command pipeline not supported for implicit remoting batching.</value>
</data> </data>
<data name="ImplicitRemotingPipelineBatchingNotASimplePipeline" xml:space="preserve">
<value>Command is not a simple pipeline and cannot be batched.</value>
</data>
<data name="ImplicitRemotingPipelineBatchingNotImplicitCommand" xml:space="preserve">
<value>The pipeline command '{0}' is not an implicit remoting command or an approved batching command.</value>
</data>
<data name="ImplicitRemotingPipelineBatchingWrongSession" xml:space="preserve">
<value>The pipeline command '{0}' is for a different remote session and cannot be batched.</value>
</data>
<data name="ImplicitRemotingPipelineBatchingNoPSSession" xml:space="preserve">
<value>The implicit remoting PSSession for batching could not be retrieved.</value>
</data>
<data name="ImplicitRemotingPipelineBatchingException" xml:space="preserve">
<value>Exception while checking the command for implicit remoting batching: {0}</value>
</data>
<data name="ImplicitRemotingPipelineBatchingSuccess" xml:space="preserve">
<value>Implicit remoting command pipeline has been batched for execution on remote target.</value>
</data>
</root> </root>