-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommon.ps1
235 lines (188 loc) · 7.53 KB
/
common.ps1
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
225
226
227
228
229
230
231
232
233
234
235
$moduleName = (Get-Item ([IO.Path]::Combine($PSScriptRoot, '..', 'module', '*.psd1'))).BaseName
$manifestPath = [IO.Path]::Combine($PSScriptRoot, '..', 'output', $moduleName)
if (-not (Get-Module -Name $moduleName -ErrorAction SilentlyContinue)) {
Import-Module $manifestPath
}
$global:exampleDllPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, "NativeExamples", "bin", "NativeExamples.dll"))
if (-not (Test-Path -LiteralPath $exampleDllPath)) {
throw "Failed to find NativeExamples dll at '$exampleDllPath'"
}
Add-Type -TypeDefinition @"
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
namespace PSDetourTest
{
public static class Native
{
[DllImport("Kernel32.dll")]
public static extern bool CloseHandle(IntPtr pHandle);
[DllImport("Kernel32.dll")]
public static extern int GetCurrentProcessId();
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool CreateSymbolicLinkW(
[MarshalAs(UnmanagedType.LPWStr)] string lpSymlinkFileName,
[MarshalAs(UnmanagedType.LPWStr)] string lpTargetFileName,
int dwFlags);
[DllImport("Kernel32.dll")]
public static extern IntPtr OpenProcess(
int dwDesiredAccess,
bool bInheritHandle,
int dwProcessId);
[DllImport("Advapi32.dll")]
public static extern bool OpenProcessToken(
IntPtr hProcess,
int dwAccess,
out IntPtr hToken);
[DllImport("$($exampleDllPath -replace '\\', '\\\\')")]
public static extern void VoidWithArg(int arg1);
}
public static class TestHelpers
{
public static Task VoidWithArg(int milliseconds, EventWaitHandle waitHandle)
{
return Task.Run(() => VoidWithArgAnotherThread(milliseconds, waitHandle));
}
private static void VoidWithArgAnotherThread(int milliseconds, EventWaitHandle waitHandle)
{
waitHandle.WaitOne();
Native.VoidWithArg(milliseconds);
}
}
public class Host : PSHost, IHostSupportsInteractiveSession
{
private readonly PSHost PSHost;
private readonly HostUI HostUI;
public Host(PSHost host){
PSHost=host;
HostUI = new HostUI(PSHost.UI);
}
public override CultureInfo CurrentCulture => PSHost.CurrentCulture;
public override CultureInfo CurrentUICulture => PSHost.CurrentUICulture;
public override Guid InstanceId => PSHost.InstanceId;
public override string Name => PSHost.Name;
public override PSHostUserInterface UI => HostUI;
public override Version Version => PSHost.Version;
public override void EnterNestedPrompt()
{
PSHost.EnterNestedPrompt();
}
public override void ExitNestedPrompt()
{
PSHost.ExitNestedPrompt();
}
public override void NotifyBeginApplication()
{
PSHost.NotifyBeginApplication();
}
public override void NotifyEndApplication()
{
PSHost.NotifyEndApplication();
}
public override void SetShouldExit(int exitCode)
{
PSHost.SetShouldExit(exitCode);
}
public void PushRunspace(Runspace runspace)
{
((IHostSupportsInteractiveSession)PSHost).PushRunspace(runspace);
}
public void PopRunspace()
{
((IHostSupportsInteractiveSession)PSHost).PopRunspace();
}
public bool IsRunspacePushed => ((IHostSupportsInteractiveSession)PSHost).IsRunspacePushed;
public Runspace Runspace => ((IHostSupportsInteractiveSession)PSHost).Runspace;
}
public class HostUI : PSHostUserInterface, IHostUISupportsMultipleChoiceSelection
{
private readonly PSHostUserInterface PSHostUI;
public readonly Dictionary<string, List<string>> WriteCalls = new Dictionary<string, List<string>>();
public HostUI(PSHostUserInterface psHostUI)
{
PSHostUI = psHostUI;
WriteCalls["Debug"] = new List<string>();
WriteCalls["Error"] = new List<string>();
WriteCalls["Progress"] = new List<string>();
WriteCalls["Verbose"] = new List<string>();
WriteCalls["Warning"] = new List<string>();
WriteCalls["Write"] = new List<string>();
WriteCalls["WriteLine"] = new List<string>();
}
public override PSHostRawUserInterface RawUI => PSHostUI.RawUI;
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
return PSHostUI.Prompt(caption, message, descriptions);
}
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
return PSHostUI.PromptForChoice(caption, message, choices, defaultChoice);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
return PSHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
return PSHostUI.PromptForCredential(caption, message, userName, targetName);
}
public override string ReadLine()
{
return "readline response";
}
public override SecureString ReadLineAsSecureString()
{
return PSHostUI.ReadLineAsSecureString();
}
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
WriteCalls["Write"].Add(value);
}
public override void Write(string value)
{
WriteCalls["Write"].Add(value);
}
public override void WriteDebugLine(string message)
{
WriteCalls["Debug"].Add(message);
}
public override void WriteErrorLine(string value)
{
WriteCalls["Error"].Add(value);
}
public override void WriteLine(string value)
{
WriteCalls["WriteLine"].Add(value);
}
public override void WriteProgress(long sourceId, ProgressRecord record)
{
WriteCalls["Progress"].Add(record.ToString());
}
public override void WriteVerboseLine(string message)
{
WriteCalls["Verbose"].Add(message);
}
public override void WriteWarningLine(string message)
{
WriteCalls["Warning"].Add(message);
}
public Collection<int> PromptForChoice(string caption,
string message,
Collection<ChoiceDescription> choices,
IEnumerable<int> defaultChoices)
{
return ((IHostUISupportsMultipleChoiceSelection)PSHostUI).PromptForChoice(
caption, message, choices, defaultChoices);
}
}
}
"@