-
Notifications
You must be signed in to change notification settings - Fork 57
/
powerpick.cs
60 lines (50 loc) · 1.66 KB
/
powerpick.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
using System;
using System.Text;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace powerpick
{
internal class Program
{
static void Main(string[] args)
{
string cmd = string.Join(" ", args);
PS ps = new PS();
string output = ps.exe(cmd);
ps.close();
Console.Write(output);
}
}
/* class taken from https://github.com/p3nt4/PowerShdll/blob/master/exe/Common.cs */
public class PS
{
Runspace runspace;
public PS()
{
this.runspace = RunspaceFactory.CreateRunspace();
this.runspace.Open();
}
public string exe(string cmd)
{
try {
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(cmd);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
foreach (string line in obj.ToString().Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
stringBuilder.AppendLine(line.TrimEnd());
return stringBuilder.ToString();
} catch (Exception e) {
string errorText = e.Message + "\n";
return (errorText);
}
}
public void close()
{
this.runspace.Close();
}
}
}