Skip to content

Commit

Permalink
unsubscribe data reveicers and make executable OS specific
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz.wojtania committed Jan 8, 2025
1 parent ce29d06 commit 2995ef4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Text;

Expand Down Expand Up @@ -65,31 +66,30 @@ private static PowerShellResult ExecuteProcess(string scriptPath, PowerShellPara
{
List<dynamic> results = new();
List<string> errors = new();




using Process process = new();
process.StartInfo = new()
{
FileName = "powershell.exe",
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "powershell" : "pwsh",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File \"{scriptPath}\" ",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};

process.OutputDataReceived += (sender, args) =>
void ProcessOutputReceiver(object sender, DataReceivedEventArgs args)
{
if (!string.IsNullOrWhiteSpace(args.Data))
results.Add(args.Data);
};

process.ErrorDataReceived += (sender, args) =>
void ProcessErrorReceiver(object sender, DataReceivedEventArgs args)
{
if (!string.IsNullOrWhiteSpace(args.Data))
errors.Add(args.Data);
};
process.OutputDataReceived += ProcessOutputReceiver;
process.ErrorDataReceived += ProcessErrorReceiver;

try
{
Expand All @@ -105,6 +105,8 @@ private static PowerShellResult ExecuteProcess(string scriptPath, PowerShellPara
finally
{
if (!process.HasExited) process.Kill();
process.OutputDataReceived -= ProcessOutputReceiver;
process.ErrorDataReceived -= ProcessErrorReceiver;
process.Close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Text;

Expand Down Expand Up @@ -72,25 +73,26 @@ private static PowerShellResult ExecuteProcess(string scriptPath, PowerShellPara
using Process process = new();
process.StartInfo = new()
{
FileName = "powershell.exe",
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "powershell" : "pwsh",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File \"{scriptPath}\" {parameterString}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};

process.OutputDataReceived += (sender, args) =>
void ProcessOutputReceiver(object sender, DataReceivedEventArgs args)
{
if (!string.IsNullOrWhiteSpace(args.Data))
results.Add(args.Data);
};

process.ErrorDataReceived += (sender, args) =>
void ProcessErrorReceiver(object sender, DataReceivedEventArgs args)
{
if (!string.IsNullOrWhiteSpace(args.Data))
errors.Add(args.Data);
};
process.OutputDataReceived += ProcessOutputReceiver;
process.ErrorDataReceived += ProcessErrorReceiver;

try
{
Expand All @@ -106,6 +108,8 @@ private static PowerShellResult ExecuteProcess(string scriptPath, PowerShellPara
finally
{
if (!process.HasExited) process.Kill();
process.OutputDataReceived -= ProcessOutputReceiver;
process.ErrorDataReceived -= ProcessErrorReceiver;
process.Close();
}

Expand Down

0 comments on commit 2995ef4

Please sign in to comment.