| skipped 110 lines |
111 | 111 | | Console.WriteLine(ps.exe(cmd)); |
112 | 112 | | } |
113 | 113 | | } |
114 | | - | public static string LoadScript(string filename) |
| 114 | + | public string LoadScript(string filename) |
115 | 115 | | { |
116 | 116 | | try |
117 | 117 | | { |
| skipped 11 lines |
129 | 129 | | catch (Exception e) |
130 | 130 | | { |
131 | 131 | | string errorText = e.Message + "\n"; |
| 132 | + | pp = Process.GetCurrentProcess().Parent(); |
| 133 | + | PSConsole.stealConsole(pp); |
| 134 | + | Console.CancelKeyPress += delegate { |
| 135 | + | this.cleanup(); |
| 136 | + | }; |
| 137 | + | Console.SetCursorPosition(0, Console.CursorTop + 2); |
132 | 138 | | Console.WriteLine(errorText); |
133 | 139 | | return ("error"); |
134 | 140 | | } |
| skipped 11 lines |
146 | 152 | | Console.WriteLine("rundll32 PowerShdll,main -f <path>\t Run the script passed as argument"); |
147 | 153 | | Console.WriteLine("rundll32 PowerShdll,main -w\t Start an interactive console in a new window"); |
148 | 154 | | Console.WriteLine("rundll32 PowerShdll,main -i\t Start an interactive console in this console"); |
| 155 | + | Console.WriteLine("\nIf you do not have an interractive console, use -n to avoid crashes on ouput"); |
| 156 | + | |
149 | 157 | | } |
150 | 158 | | public void start(string[] args) { |
151 | | - | if (args[0]=="") { usage(); return; } |
152 | | - | else if (args[0] == "-h") |
| 159 | + | int i=0; |
| 160 | + | bool useConsole = true; |
| 161 | + | string ret; |
| 162 | + | if (args[i]=="") { usage(); return; } |
| 163 | + | if (args[i] == "-n") |
| 164 | + | { |
| 165 | + | i++; |
| 166 | + | useConsole = false; |
| 167 | + | } |
| 168 | + | if (args[i] == "-h") |
153 | 169 | | { |
154 | 170 | | usage(); |
155 | 171 | | } |
156 | | - | else if (args[0] == "-w") |
| 172 | + | else if (args[i] == "-w") |
157 | 173 | | { |
158 | 174 | | PSConsole.getNewConsole(); |
159 | 175 | | this.interact(); |
160 | 176 | | } |
161 | | - | else if (args[0] == "-i") |
| 177 | + | else if (args[i] == "-i") |
162 | 178 | | { |
163 | 179 | | pp = Process.GetCurrentProcess().Parent(); |
164 | 180 | | pp.Suspend(); |
| skipped 9 lines |
174 | 190 | | ps.close(); |
175 | 191 | | pp.Resume(); |
176 | 192 | | } |
177 | | - | else if (args[0] == "-f") |
| 193 | + | else if (args[i] == "-f") |
178 | 194 | | { |
179 | | - | if (args.Length < 2) {usage(); return; } |
180 | | - | pp = Process.GetCurrentProcess().Parent(); |
181 | | - | PSConsole.stealConsole(pp); |
182 | | - | Console.CancelKeyPress += delegate { |
183 | | - | this.cleanup(); |
184 | | - | }; |
185 | | - | Console.SetCursorPosition(0, Console.CursorTop + 1); |
186 | | - | string script = PowerShdll.LoadScript(args[1]); |
| 195 | + | i++; |
| 196 | + | if (args.Length < 2) { usage(); return; } |
| 197 | + | if (args[i] == "-n") |
| 198 | + | { |
| 199 | + | if (args.Length < 3) { usage(); return; } |
| 200 | + | i++; |
| 201 | + | useConsole = false; |
| 202 | + | } |
| 203 | + | string script = LoadScript(args[i]); |
187 | 204 | | if (script != "error") |
188 | 205 | | { |
189 | | - | Console.Write(ps.exe(script)); |
190 | | - | } |
| 206 | + | ret = ps.exe(script); |
| 207 | + | if (useConsole) { |
| 208 | + | pp = Process.GetCurrentProcess().Parent(); |
| 209 | + | PSConsole.stealConsole(pp); |
| 210 | + | Console.CancelKeyPress += delegate { |
| 211 | + | this.cleanup(); |
| 212 | + | }; |
| 213 | + | Console.SetCursorPosition(0, Console.CursorTop + 1); |
| 214 | + | Console.WriteLine(ret); |
| 215 | + | } |
| 216 | + | } |
191 | 217 | | } |
192 | 218 | | else |
| 219 | + | { |
| 220 | + | string script = string.Join(" ",args, i, args.Length-i); |
| 221 | + | if (script[0] == '"' && script[script.Length - 1] == '"') { |
| 222 | + | script = script.Substring(1, script.Length - 2); |
| 223 | + | } |
| 224 | + | ret = ps.exe(script); |
| 225 | + | if (useConsole) |
193 | 226 | | { |
194 | 227 | | pp = Process.GetCurrentProcess().Parent(); |
195 | 228 | | PSConsole.stealConsole(pp); |
| skipped 1 lines |
197 | 230 | | this.cleanup(); |
198 | 231 | | }; |
199 | 232 | | Console.SetCursorPosition(0, Console.CursorTop + 1); |
200 | | - | string script = string.Join(" ", args); |
201 | | - | Console.Write(ps.exe(script)); |
202 | | - | ps.close(); |
| 233 | + | Console.WriteLine(ret); |
| 234 | + | } |
| 235 | + | ps.close(); |
203 | 236 | | } |
204 | 237 | | return; |
205 | 238 | | } |
| skipped 112 lines |