This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remoting.cs
224 lines (207 loc) · 8.99 KB
/
Remoting.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace CoApp.PSClient.Remoting
{
public static class Remote
{
/*
* It should be noted that at the time of this writing, there is effectively no existing public
* documentation for the following remote connection methods from Microsoft. These sections
* were completed based upon documentation produced by the trial and error of others.
*
* Base source for connections:
* http://com2kid.wordpress.com/2011/09/22/remotely-executing-commands-in-powershell-using-c/
*
*/
public class ConnectionGenerator
{
public static readonly int PortSSL = 5986;
public static readonly int PortNoSSL = 5985;
public static readonly string DefaultAppName = "/wsman";
public static readonly string DefaultShellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
private readonly bool _UseSSL;
private readonly PSCredential _Credential;
private readonly int _Port;
public ConnectionGenerator(bool UseSSL, PSCredential Credential, int? Port = null)
{
_UseSSL = UseSSL;
_Credential = Credential;
_Port = Port ?? (UseSSL ? PortSSL : PortNoSSL);
}
public Runspace Generate(string RemoteComputer, int? timeout = null, bool CheckCLR = false)
{
WSManConnectionInfo info;
if (timeout != null)
info = new WSManConnectionInfo(_UseSSL, RemoteComputer, _Port, DefaultAppName, DefaultShellUri, _Credential, timeout.Value);
else
info = new WSManConnectionInfo(_UseSSL, RemoteComputer, _Port, DefaultAppName, DefaultShellUri, _Credential);
Runspace runspace = RunspaceFactory.CreateRunspace(info);
runspace.Open();
if (CheckCLR)
{
Pipeline pipe = runspace.CreatePipeline();
pipe.Commands.AddScript("$PSVersionTable.Item('CLRVersion').Major");
Collection<PSObject> res = pipe.Invoke();
pipe.Dispose();
pipe = runspace.CreatePipeline();
if (res != null)
{
int ver = Int32.Parse(res.FirstOrDefault().ToString());
if (ver < 4)
{
// need to write a system file to push the 4.0 CLR
string pathScript = @"$f = (dir env:SystemRoot).value + '\System32\wsmprovhost.exe.config'";
pipe.Commands.AddScript(pathScript);
pipe.Invoke();
pipe.Dispose();
pipe = runspace.CreatePipeline();
string contentScript = @"'<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy=""true"">
<supportedRuntime version=""v4.0""/>
</startup>
</configuration>'";
string commandScript = @"Set-Content -Path $f -Force -Encoding UTF8 -Value ";
pipe.Commands.AddScript(commandScript + contentScript);
pipe.Invoke();
}
}
else // bad things happened
throw new RuntimeException("Error receiving objects from remote connection.");
// Clean up and make a new runspace to hand off.
try
{
pipe.Dispose();
runspace.Close();
}
catch (Exception)
{ }
runspace = RunspaceFactory.CreateRunspace(info);
runspace.Open();
}
return runspace;
}
}
public static Collection<PSObject> Exec(Runspace runspace, Command command)
{
PowerShell shell = PowerShell.Create();
shell.Runspace = runspace;
Command import = new Command("Import-Module");
import.Parameters.Add("Name", "CoApp");
shell.Commands.AddCommand(import);
shell.Invoke();
shell.Commands.Clear();
shell.Commands.AddScript(command.ToString());
Collection<PSObject> ret = shell.Invoke();
try
{
shell.Dispose();
runspace.Close();
}
catch (Exception)
{ }
return ret;
}
public static Collection<PSObject> Exec(Runspace runspace, string script)
{
runspace.Open();
PowerShell shell = PowerShell.Create();
shell.Runspace = runspace;
shell.AddScript(script);
Collection<PSObject> ret = shell.Invoke();
try
{
shell.Dispose();
runspace.Close();
}
catch (Exception)
{ }
return ret;
}
internal class RemoteOutput
{
public string RemoteName;
public List<PSObject> Output;
public RemoteOutput(string Name)
{
RemoteName = Name;
Output = new List<PSObject>();
}
}
[Cmdlet(VerbsLifecycle.Invoke, "RemoteCommand")]
public class Invoke_RemoteCommand : PSCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = false)]
public string[] ComputerName;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public PSCredential Credential = null;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public SwitchParameter UseSSL = false;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public int? Port = null;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public int? Timeout = null;
[Parameter(Mandatory = true, ValueFromPipeline = true)]
public string Command;
protected override void ProcessRecord()
{
ConnectionGenerator generator = new ConnectionGenerator(UseSSL, Credential, Port);
foreach (string comp in ComputerName)
{
Collection<PSObject> result = Exec(generator.Generate(comp, Timeout), this.Command);
RemoteOutput O = new RemoteOutput(comp);
if (result != null)
O.Output.AddRange(result);
else
O.Output = null;
WriteObject(O);
}
}
}
[Cmdlet(VerbsLifecycle.Start, "RemoteSession")]
public class Start_RemoteSession : PSCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = false)]
public string ComputerName;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public PSCredential Credential = null;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public SwitchParameter UseSSL = false;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public int? Port = null;
[Parameter(Mandatory = false, ValueFromPipeline = false)]
public int? Timeout = null;
protected override void ProcessRecord()
{
ConnectionGenerator generator = new ConnectionGenerator(UseSSL, Credential, Port);
Runspace R = generator.Generate(ComputerName, Timeout);
R.Open();
PowerShell shell = PowerShell.Create();
shell.Runspace = R;
string script = Host.UI.ReadLine();
while (!script.Equals("exit", StringComparison.CurrentCultureIgnoreCase))
{
shell.Commands.Clear();
shell.AddScript(script);
Collection<PSObject> ret = shell.Invoke();
if (ret != null)
WriteObject(ret, true);
Host.UI.WriteLine();
Host.UI.Write(ComputerName + "> ");
script = Host.UI.ReadLine();
}
try
{
shell.Dispose();
R.Close();
}
catch (Exception)
{ }
}
}
}
}