-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvertPS12Exe_V1.ps1
599 lines (512 loc) · 20.6 KB
/
ConvertPS12Exe_V1.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<#
,Synopsis
Ps12EXE - PS1-Dateien in Exe einbetten GUI-Version
.Description
Erzeugt eine Exe-Datei mit einer Ps1-Datei als eingebetteter Ressource, die nach dem Start ausgeführt wird
Version 1.0
.Notes
Original-Autor und Idee: Keith Hill
.Notes
Aktuell funktioniert das Skript nur unter der Powershell 2.0
.Notes
Powershell.exe muss mit -STA gestartet werden
#>
Set-StrictMode -Version 2.0
<#
.Synopsis
Bettet eine Ps1-Datei in eine Exe-Datei ein
.Parameter PS1Path
Der Pfad der Ps1-Datei
.Parameter OutputAssemblyPath
Der Pfad der Exe-Datei, die angelegt werden soll
#>
function Make-Exe
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[String]
$PS1Path,
[Parameter(Mandatory = $true, Position = 1)]
[String]
$OutputAssemblyPath
)
# $PS1PathCS = $PS1Path -replace "\\", "\\"
$Script:PS1Name = Split-Path -Path $PS1Path -Leaf
$AssemblyCode = @"
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Security;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace PS1ToExeTemplate
{
class Program
{
private static object _powerShellLock = new object();
private static PowerShell _powerShellEngine;
private static readonly PS1Host _psHost = new PS1Host();
static void Main(string[] args)
{
string script = GetScript();
RunScript(script, args, null);
}
private static string GetScript()
{
string script = String.Empty;
Assembly ass = Assembly.GetExecutingAssembly();
try
{
using (Stream st = ass.GetManifestResourceStream("$Script:PS1Name"))
{
StreamReader sr = new StreamReader(st);
script = sr.ReadToEnd();
}
}
catch
{
script = "Write-Host -Fore Red -Back White";
}
return script;
}
private static void RunScript(string script, string[] args, object input)
{
Debug.WriteLine("Skript:\n " + script);
lock (_powerShellLock)
{
_powerShellEngine = PowerShell.Create();
}
try
{
_powerShellEngine.Runspace = RunspaceFactory.CreateRunspace(_psHost);
_powerShellEngine.Runspace.ApartmentState = System.Threading.ApartmentState.STA;
_powerShellEngine.Runspace.Open();
_powerShellEngine.AddScript(script);
_powerShellEngine.AddCommand("Out-Default");
_powerShellEngine.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
if (input != null)
{
Collection<PSObject> results = _powerShellEngine.Invoke(new[] { input });
Debug.WriteLine(String.Format("Fertig - {0} Ergebnisse.", results.Count));
}
else
{
Collection<PSObject> results = _powerShellEngine.Invoke();
Debug.WriteLine(String.Format("Fertig - {0} Ergebnisse.", results.Count));
}
}
catch (SystemException ex)
{
Debug.WriteLine("Fehler: " + ex.Message);
}
finally
{
lock (_powerShellLock)
{
_powerShellEngine.Dispose();
_powerShellEngine = null;
}
}
}
}
// Die Klasse PS1Host
class PS1Host : PSHost
{
private PSHostUserInterface _psHostUserInterface = new HostUserInterface();
public override void SetShouldExit(int exitCode)
{
Environment.Exit(exitCode);
}
public override void EnterNestedPrompt()
{
throw new NotImplementedException();
}
public override void ExitNestedPrompt()
{
throw new NotImplementedException();
}
public override void NotifyBeginApplication()
{
throw new NotImplementedException();
}
public override void NotifyEndApplication()
{
throw new NotImplementedException();
}
public override string Name
{
get { return "PS1ToExeHost"; }
}
public override Version Version
{
get { return new Version(1, 0); }
}
public override Guid InstanceId
{
get { return new Guid("E4673B42-84B6-4C43-9589-95FAB8E00EB2"); }
}
public override PSHostUserInterface UI
{
get { return _psHostUserInterface; }
}
public override CultureInfo CurrentCulture
{
get { return Thread.CurrentThread.CurrentCulture; }
}
public override CultureInfo CurrentUICulture
{
get { return Thread.CurrentThread.CurrentUICulture; }
}
}
// Ende der Host-Klasse
// Klasse HostUserInterface
class HostUserInterface : PSHostUserInterface
{
private PSHostRawUserInterface _psRawUserInterface = new HostRawUserInterface();
public override PSHostRawUserInterface RawUI
{
get { return _psRawUserInterface; }
}
public override string ReadLine()
{
return Console.ReadLine();
}
public override SecureString ReadLineAsSecureString()
{
throw new NotImplementedException();
}
public override void Write(string value)
{
string output = value ?? "null";
Console.Write(output);
}
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
string output = value ?? "null";
var origFgColor = Console.ForegroundColor;
var origBgColor = Console.BackgroundColor;
Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor;
Console.Write(output);
Console.ForegroundColor = origFgColor;
Console.BackgroundColor = origBgColor;
}
public override void WriteLine(string value)
{
string output = value ?? "null";
Console.WriteLine(output);
}
public override void WriteErrorLine(string value)
{
string output = value ?? "null";
var origFgColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(output);
Console.ForegroundColor = origFgColor;
}
public override void WriteDebugLine(string message)
{
WriteYellowAnnotatedLine(message, "DEBUG");
}
public override void WriteVerboseLine(string message)
{
WriteYellowAnnotatedLine(message, "VERBOSE");
}
public override void WriteWarningLine(string message)
{
WriteYellowAnnotatedLine(message, "WARNING");
}
private void WriteYellowAnnotatedLine(string message, string annotation)
{
string output = message ?? "null";
var origFgColor = Console.ForegroundColor;
var origBgColor = Console.BackgroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "{0}: {1}", annotation, output));
Console.ForegroundColor = origFgColor;
Console.BackgroundColor = origBgColor;
}
public override void WriteProgress(long sourceId, ProgressRecord record)
{
throw new NotImplementedException();
}
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
throw new NotImplementedException();
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
throw new NotImplementedException();
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
throw new NotImplementedException();
}
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
throw new NotImplementedException();
}
}
// Klasse HostRawUserInterface
class HostRawUserInterface : PSHostRawUserInterface
{
public override KeyInfo ReadKey(ReadKeyOptions options)
{
throw new NotImplementedException();
}
public override void FlushInputBuffer()
{
throw new NotImplementedException();
}
public override void SetBufferContents(Coordinates origin, BufferCell[,] contents)
{
throw new NotImplementedException();
}
public override void SetBufferContents(Rectangle rectangle, BufferCell fill)
{
throw new NotImplementedException();
}
public override BufferCell[,] GetBufferContents(Rectangle rectangle)
{
throw new NotImplementedException();
}
public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill)
{
throw new NotImplementedException();
}
public override ConsoleColor ForegroundColor
{
get { return Console.ForegroundColor; }
set { Console.ForegroundColor = value; }
}
public override ConsoleColor BackgroundColor
{
get { return Console.BackgroundColor; }
set { Console.BackgroundColor = value; }
}
public override Coordinates CursorPosition
{
get { return new Coordinates(Console.CursorLeft, Console.CursorTop); }
set { Console.SetCursorPosition(value.X, value.Y); }
}
public override Coordinates WindowPosition
{
get { return new Coordinates(Console.WindowLeft, Console.WindowTop); }
set { Console.SetWindowPosition(value.X, value.Y); }
}
public override int CursorSize
{
get { return Console.CursorSize; }
set { Console.CursorSize = value; }
}
public override Size BufferSize
{
get { return new Size(Console.BufferWidth, Console.BufferHeight); }
set { Console.SetBufferSize(value.Width, value.Height); }
}
public override Size WindowSize
{
get { return new Size(Console.WindowWidth, Console.WindowHeight); }
set { Console.SetWindowSize(value.Width, value.Height); }
}
public override Size MaxWindowSize
{
get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); }
}
public override Size MaxPhysicalWindowSize
{
get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); }
}
public override bool KeyAvailable
{
get { return Console.KeyAvailable; }
}
public override string WindowTitle
{
get { return Console.Title; }
set { Console.Title = value; }
}
}
}
"@
# Hier beginnt die Function
$ReferenceAssemblies = "System.dll",([PSObject].Assembly.Location)
$Cp = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters -ArgumentList $ReferenceAssemblies,$OutputAssemblyPath,$true
$Cp.TempFiles = New-Object -TypeName System.CodeDom.Compiler.TempFileCollection -ArgumentList ([IO.Path]::GetTempPath())
$Cp.GenerateExecutable = $true
$Cp.GenerateInMemory = $false
$Cp.IncludeDebugInformation = $true
# [void]$Cp.EmbeddedResources.Add($PS1Path)
[void]$Cp.EmbeddedResources.Add($Script:PS1Name)
$Dict = New-Object -Typename 'System.Collections.Generic.Dictionary[string, string]'
$Dict.Add("CompilerVersion","v3.5")
$Provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider -ArgumentList $Dict
$Results = $Provider.CompileAssemblyFromSource($Cp, $AssemblyCode)
$StatusListBox.Items.Add("Exe-Datei wurde erstellt")
$StatusListBox.Items.Add(("Anzahl Fehler: {0:0}" -f $Results.Errors.Count))
if ($Results.Errors.Count)
{
$ErrorLines = ""
foreach ($Error in $results.Errors)
{
$StatusListBox.Items.Add("*** Fehler: " + "`t" + $Error.ErrorText + "(" + $Error.Line + ")")
}
}
else
{
$StatusListBox.Items.Add("*** Erfolg - Exe-Datei wurde fehlerfrei erstellt.")
}
}
<#
.Synopsis
Zeigt das Fenster an
#>
function Show-MainWindow
{
$MainForm = New-Object -Typename System.Windows.Forms.Form
$MainStatusBar = New-Object -Typename System.Windows.Forms.StatusBar
$GroupBox1 = New-Object -Typename System.Windows.Forms.GroupBox
$GroupBox2 = New-Object -Typename System.Windows.Forms.GroupBox
$StatusListBox = New-Object -Typename System.Windows.Forms.ListBox
$ExeErstellenButton = New-Object -Typename System.Windows.Forms.Button
$PS1AuswahlButton = New-Object -Typename System.Windows.Forms.Button
$InitialFormWindowState = New-Object -Typename System.Windows.Forms.FormWindowState
$ExePfadTextBox = New-Object -Typename System.Windows.Forms.TextBox
$Label1 = New-Object -Typename System.Windows.Forms.Label
# Skriptblock für das Anklicken des PS1Auswahl-Buttons
$PS1AuswahlButton_OnClick=
{
$Ofd = New-Object -TypeName System.Windows.Forms.OpenFileDialog
$Ofd.Title = "Auswahl PS1-Datei"
$Ofd.Filter = "PS1-Dateien (*.ps1)|*.ps1|Alle Dateien|*.*"
if ($Ofd.ShowDialog() -eq "OK")
{
$Script:Ps1Pfad = $Ofd.FileName
$StatusListBox.Items.Add($Ps1Pfad + " wurde gewählt")
$ExeErstellenButton.Enabled = $true;
}
}
# Skriptblock für das Anklicken des Exe-Erstellen-Buttons
$ExeErstellenButton_OnClick=
{
$AssPfad = $ExePfadTextBox.Text
# Gibt es den Pfad?
if (!(Test-Path -Path $AssPfad))
{
# Ist es ein gültiger Pfad?
if (Test-Path -Path $AssPfad -IsValid)
{
# Soll das Verzeichnis angelegt werden?
<#
Choice-Dialog wird in der Befehlszeile auch in der Befehlszeile angezeigt
$JaChoice = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription "Ja", "Verzeichnis anlegen"
$NeinChoice = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription "Nein", "Verzeichnis nicht anlegen"
if ($Host.UI.PromptForChoice("Wie soll es weitergehen?", "Verzeichnis $AssPfad anlegen?",
@($JaChoice, $NeinChoice), 0) -eq 0)
#>
if ([System.Windows.MessageBox]::Show("Wie soll es weitergehen?", "Verzeichnis $AssPfad anlegen?","YesNo"))
{
md -Path $AssPfad -ErrorAction Ignore
if (!$?)
{
$StatusListBox.Items.Add("$AssPfad konnte nicht angelegt werden - Skript wird beendet.")
Exit -2
}
else
{
$StatusListBox.Items.Add("Das Verzeichnis $AssPfad wurde angelegt.")
}
}
}
else
{
$StatusListBox.Items.Add("Ungültiges Verzeichnis $AssPfad - Skript wird beendet.")
Exit -3
}
}
$StatusListBox.Items.Add($Ps1Pfad + " wird in Exe-Datei konvertiert.")
$AssName = [System.IO.Path]::GetFileNameWithoutExtension($Ps1Pfad)
$AssName += "_Konvertiert.exe"
$AssPfad = Join-Path -Path $AssPfad -ChildPath $AssName
Make-Exe -PS1Path $Ps1Pfad -OutputAssemblyPath $AssPfad
$ExeErstellenButton.Enabled = $false
}
# Skritblock für das Laden des Fensters
$OnLoadForm=
{
$MainForm.WindowState = $InitialFormWindowState
$ExeErstellenButton.Enabled = $false
$ExePfadTextBox.Text = $env:USERPROFILE + "\PS1Exe"
}
$MainForm.ClientSize = New-Object -Typename System.Drawing.Size -ArgumentList 500, 440
$MainForm.Font = New-Object -Typename System.Drawing.Font("Arial Narrow",12,1,3,0)
$MainForm.Name = "Mainform"
$MainForm.Text = "PS1 in Exe einbetten"
$MainStatusBar.Location = New-Object -Typename System.Drawing.Point -ArgumentList 300, 0
$MainStatusBar.Name = "MainStatusBar"
$MainStatusBar.Size = New-Object -Typename System.Drawing.Size -ArgumentList 414, 22
$MainStatusBar.TabIndex = 3
$MainStatusBar.Text = "OK"
$MainForm.Controls.Add($MainStatusBar)
$GroupBox1.Location = New-Object -Typename System.Drawing.Point -ArgumentList 32, 200
$GroupBox1.Name = "groupBox1"
$GroupBox1.Size = New-Object -Typename System.Drawing.Size -ArgumentList 400,200
$GroupBox1.TabStop = $False
$GroupBox1.Text = "Status"
$MainForm.Controls.Add($GroupBox1)
$ExePfadTextBox.BackColor = [System.Drawing.Color]::FromArgb(255,192,255,192)
$ExePfadTextBox.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 18, 70
$ExePfadTextBox.Name = "ExePfadTextBox"
$ExePfadTextBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList 364, 26
$GroupBox2.Controls.Add($ExePfadTextBox)
$Label1.Location = New-Object -TypeName System.Drawing.Point -ArgumentList 16, 44
$Label1.Name = "label1"
$Label1.Size = New-Object -TypeName System.Drawing.Size -ArgumentList 88,24
$Label1.TabIndex = 1
$Label1.Text = "Exe-Pfad:"
$Groupbox2.Controls.Add($Label1)
$GroupBox2.Location = New-Object -Typename System.Drawing.Point -ArgumentList 32, 72
$GroupBox2.Name = "GroupBox2"
$GroupBox2.Size = New-Object -Typename System.Drawing.Size -ArgumentList 400,120
$GroupBox2.TabStop = $False
$GroupBox2.Text = "Exe-Datei erstellen"
$MainForm.Controls.Add($GroupBox2)
$StatusListBox.ItemHeight = 20
$StatusListBox.Location = New-Object -Typename System.Drawing.Point -ArgumentList 8, 26
$StatusListBox.Size = New-Object -Typename System.Drawing.Size -ArgumentList 380, 164
$StatusListBox.HorizontalScrollbar = $true
$StatusListBox.TabIndex = 0
$GroupBox1.Controls.Add($StatusListBox)
$ExeErstellenButton.Location = New-Object -Typename System.Drawing.Point -ArgumentList 220, 18
$ExeErstellenButton.Name = "ExeErstellenButton"
$ExeErstellenButton.Size = New-Object -Typename System.Drawing.Size -ArgumentList 166, 46
$ExeErstellenButton.TabIndex = 1
$ExeErstellenButton.Text = "Exe-Erstellen"
$ExeErstellenButton.add_Click($ExeErstellenButton_OnClick)
$GroupBox2.Controls.Add($ExeErstellenButton)
$PS1AuswahlButton.Location = New-Object -Typename System.Drawing.Point -ArgumentList 28, 20
$PS1AuswahlButton.Name = "PS1AuswahlButton"
$PS1AuswahlButton.Size = New-Object -Typename System.Drawing.Size -ArgumentList 166, 46
$PS1AuswahlButton.TabIndex = 0
$PS1AuswahlButton.Text = "Ps1-Auswahl"
$PS1AuswahlButton.add_Click($PS1AuswahlButton_OnClick)
$MainForm.Controls.Add($PS1AuswahlButton)
$InitialFormWindowState = $MainForm.WindowState
$MainForm.add_Load($OnLoadForm)
[System.Windows.Forms.Application]::Run($MainForm)
}
# Assemblies nachladen
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
# Fenster anzeigen
Show-MainWindow