-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
118 lines (90 loc) · 2.99 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Text;
using System.Threading;
namespace PowerShell_Test
{
class Program
{
private static int _counter = 0;
private static readonly Random _random = new Random();
const int MaxThreads = 10;
static void Main()
{
Thread[] threads = new Thread[MaxThreads];
for (int i = 0; i < MaxThreads; ++i)
{
threads[i] = new Thread(RunPowerShell);
lock (_random) Console.WriteLine("Starting thread #" + ++_counter);
threads[i].Start();
Thread.Sleep(_random.Next(100, 500));
}
while (_counter > 0) Thread.Sleep(500);
Console.WriteLine("Press any key to continue ...");
while (!Console.KeyAvailable) Thread.Sleep(200);
}
private static void RunPowerShell()
{
using (PowerShell ps = PowerShell.Create())
{
try
{
Collection<PSObject> retVal;
ps.AddScript(File.ReadAllText(@".\Test.ps1"));
retVal = ps.Invoke();
if (!hasFailed(ps) && retVal.Count > 0)
{
lock (_random) Console.WriteLine("Success.");
}
}
catch (RuntimeException rex)
{
lock (_random)
{
ConsoleColor fc = Console.ForegroundColor, bc = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Exception: " + rex.Message);
Console.WriteLine(rex.ErrorRecord.InvocationInfo.PositionMessage);
Console.ForegroundColor = fc;
Console.BackgroundColor = bc;
}
}
catch (Exception ex)
{
ConsoleColor fc = Console.ForegroundColor, bc = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
lock (_random) Console.WriteLine("Exception: " + ex.Message);
Console.ForegroundColor = fc;
Console.BackgroundColor = bc;
}
lock (_random) Console.WriteLine("Finishing thread #" + --_counter);
}
}
private static bool hasFailed(PowerShell ps, string context = null)
{
lock (_random)
{
bool hasErrors = true;
ConsoleColor fc = Console.ForegroundColor, bc = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
if (!string.IsNullOrWhiteSpace(context)) context = " " + context;
if (ps.HadErrors)
{
StringBuilder sb = new StringBuilder(1024);
foreach (ErrorRecord er in ps.Streams.Error.ReadAll()) sb.AppendLine(er.ToString());
Console.WriteLine($"Error{context}:{Environment.NewLine}{sb.ToString()}");
}
else if (ps.InvocationStateInfo.State == PSInvocationState.Failed) Console.WriteLine($"Failed{context}: {ps.InvocationStateInfo.Reason.Message}");
else hasErrors = false;
Console.ForegroundColor = fc;
Console.BackgroundColor = bc;
return hasErrors;
}
}
}
}