-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpowershell.hta
143 lines (128 loc) · 4.42 KB
/
powershell.hta
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
<html>
<head>
<title>HTA Test</title>
<HTA:APPLICATION
ID="objTest"
APPLICATIONNAME="HTATest"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>
<SCRIPT LANGUAGE="VBScript">
const cSharpCodeFileName = ".\powershell.cs"
const commandsOutputFile = ".\powershell.txt"
const cSharpExeFileName = ".\ps.exe"
Public globalShellObj
Public globalExecObj
Function writeToFile(outFile, contents)
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write contents
objFile.Close
end function
Function compileCSharpCodeFromFile(filename, outFile)
Set compileShell = CreateObject("WScript.Shell")
exitCode = compileShell.run("c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /r:c:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll /unsafe /platform:anycpu /out:" & outFile & " " & filename, visible = false, wait = true)
end function
Function writeOutput(text)
Set div = document.getElementById("output")
div.innerText = div.innerText & text
document.getElementById("right").scrollTop = document.getElementById("right").scrollHeight
end function
sub init
writeToFile cSharpCodeFileName, document.getElementById("cscode").innerText
compileCSharpCodeFromFile cSharpCodeFileName, cSharpExeFileName
end sub
sub checkOutput
std = globalExecObj.stdout.readall()
errr = globalExecObj.StdErr.readall
if InStr(std, "startasdfasdfasdf") then
std = Mid(std ,instr(std, "startasdfasdfasdf")+ len("startasdfasdfasdf"), instr(std, "The uninstall has completed.")- instr(std, "startasdfasdfasdf") -len("startasdfasdfasdf"))
end if
writeOutput vbCrLf & std
writeOutput errr
end Sub
Sub HandleInput
writeToFile commandsOutputFile, BasicTextBox.value
Set globalShellObj = CreateObject("WScript.Shell")
Set globalExecObj = globalShellObj.exec("C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=True /u .\ps.exe")
writeOutput vbCrLf & "> " & BasicTextBox.value
BasicTextbox.value = ""
checkOutput
End Sub
sub runInteractive
writeToFile commandsOutputFile, BasicTextBox.value
Set temp1 = CreateObject("WScript.Shell")
temp2 = temp1.run("C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=True /u .\ps.exe", 1, 0)
end sub
</SCRIPT>
<body>
<div id="left" style="float:left;height=100%;">
<div>
<textarea rows="4" cols="50" id="prompt" type="text" name="BasicTextbox" size="30"></textarea>
</div>
<div>
<input id=runbutton type="button" value="Run and grab stdin/out (non-interactive blocking)" name="run_button" onClick="HandleInput">
<br>
<input id=runbutton type="button" value="Run without grabbing stdin/out (interactive non-blocking)" name="run_button" onClick="runInteractive">
</div>
</div>
<p id="cscode" style="display:none">using System;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.Management.Automation.Runspaces;
public class Program
{
public static void Main()
{
}
}
[System.ComponentModel.RunInstaller(true)]
public class Sample : System.Configuration.Install.Installer
{
public override void Uninstall(System.Collections.IDictionary savedState)
{
Mycode.Exec();
}
}
public class Mycode
{
public static void Exec()
{
string command = System.IO.File.ReadAllText(@".\powershell.txt");
RunspaceConfiguration rspacecfg = RunspaceConfiguration.Create();
Runspace rspace = RunspaceFactory.CreateRunspace(rspacecfg);
rspace.Open();
Pipeline pipeline = rspace.CreatePipeline();
pipeline.Commands.AddScript(command);
pipeline.InvokeAsync();
while(pipeline.PipelineStateInfo.State == PipelineState.Running || pipeline.PipelineStateInfo.State == PipelineState.Stopping) {
System.Threading.Thread.Sleep(50);
}
Console.WriteLine("startasdfasdfasdf");
foreach (object item in pipeline.Output.ReadToEnd())
{
if(item != null) {
Console.WriteLine(item.ToString());
}
}
foreach (object item in pipeline.Error.ReadToEnd())
{
if(item != null) {
Console.WriteLine(item.ToString());
}
}
}
}
</p>
<div id="right" style="background-color:black;font-family:monospace;color:white;height:100%;max-height:100%;overflow:scroll;">
<p id="output">get ready...</p>
</div>
<script>
document.getElementById("prompt").focus()
</script>
<SCRIPT LANGUAGE="VBScript">
init
</SCRIPT>
</body>
</html>