Skip to content

Commit

Permalink
Add using around temp PS instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Mar 13, 2020
1 parent 0b5b7e2 commit 479d77b
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions Agent/Services/PSCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,33 @@ public PSCoreCommandResult WriteInput(string input, string commandID)
PS.AddScript(input);
var results = PS.Invoke();

var ps = PowerShell.Create();
ps.AddScript("$args[0] | Out-String");
ps.AddArgument(results);
var hostOutput = (ps.Invoke()[0].BaseObject as string);
using (var ps = PowerShell.Create())
{
ps.AddScript("$args[0] | Out-String");
ps.AddArgument(results);
var hostOutput = (ps.Invoke()[0].BaseObject as string);

var verboseOut = PS.Streams.Verbose.ReadAll().Select(x => x.Message).ToList();
var debugOut = PS.Streams.Debug.ReadAll().Select(x => x.Message).ToList();
var errorOut = PS.Streams.Error.ReadAll().Select(x => x.Exception.ToString() + Environment.NewLine + x.ScriptStackTrace).ToList();
var infoOut = PS.Streams.Information.Select(x => x.MessageData.ToString()).ToList();
var warningOut = PS.Streams.Warning.Select(x => x.Message).ToList();
var verboseOut = PS.Streams.Verbose.ReadAll().Select(x => x.Message).ToList();
var debugOut = PS.Streams.Debug.ReadAll().Select(x => x.Message).ToList();
var errorOut = PS.Streams.Error.ReadAll().Select(x => x.Exception.ToString() + Environment.NewLine + x.ScriptStackTrace).ToList();
var infoOut = PS.Streams.Information.Select(x => x.MessageData.ToString()).ToList();
var warningOut = PS.Streams.Warning.Select(x => x.Message).ToList();

PS.Streams.ClearStreams();
PS.Commands.Clear();
PS.Streams.ClearStreams();
PS.Commands.Clear();

return new PSCoreCommandResult()
{
CommandResultID = commandID,
DeviceID = ConfigService.GetConnectionInfo().DeviceID,
DebugOutput = debugOut,
ErrorOutput = errorOut,
VerboseOutput = verboseOut,
HostOutput = hostOutput,
InformationOutput = infoOut,
WarningOutput = warningOut
};
return new PSCoreCommandResult()
{
CommandResultID = commandID,
DeviceID = ConfigService.GetConnectionInfo().DeviceID,
DebugOutput = debugOut,
ErrorOutput = errorOut,
VerboseOutput = verboseOut,
HostOutput = hostOutput,
InformationOutput = infoOut,
WarningOutput = warningOut
};
}
}

private void ProcessIdleTimeout_Elapsed(object sender, ElapsedEventArgs e)
Expand Down

0 comments on commit 479d77b

Please sign in to comment.