-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1042.ps1
More file actions
189 lines (182 loc) · 6.45 KB
/
Copy path1042.ps1
File metadata and controls
189 lines (182 loc) · 6.45 KB
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
/*
* Compile this program by simply doing:
*
* csc.exe PSLauncher.cs
*
*/
// Requires powershell V2
/*
* The Problem:
* .ps1 scripts do not integrate with the shell nicely. You cannot
* simply associate powershell.exe with .ps1 and have them run when u
* double click them. Also, the execution of script you click on may
* be blocked by security policies. Thirdly, some powershell scripts
* require ps to be launched with some command line arguments, to frequently
* run such a script then requires keyboard entry or awkward batch files.
*
* The Solution:
* Compile this program, and associate it as the default launcher for .ps1.
* follow the usage details below, and you will be able to launch .ps1 scripts
* from the shell with mouse alone, and as added benafit you get to put
* ps cmd line options in your script, and you can run scripts invisible.
*
* USAGE:
* Put the first two characters of your script as "#!"
* Follow that with the full path to powershell.exe (incl fname) or
* if powershell.exe is in the path you can just put #!powershell.exe
*
* The remainder part of that line will be passed to powershell.exe as
* arguments. For example, -STA. The filename of the script is also
* passed as "-file %1" along with "-ExecutionPolicy UnRestricted".
*
* The second line of the file, if it is prefixed by "#-" can contain "meta arguments"
* for powershell. Currently the only one allowed is "Hidden" which will supress the
* powershell window and discard any output from the script.
*
* If a .ps1 script without these magics #! is encountered, it will be passed
* to powershell.exe as above, if powershell.exe is in the path or default location.
*
* Example:
* #!powershell.exe -STA
* #-Hidden
* ...rest of script...
*
*
* or
*
*
* #!C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -noexit
* ...rest of a script you don't want to exit when it ends...
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace PSLauncher
{
static class PSLauncher
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string [] args)
{
if (args.Length != 1)
{
throw new ArgumentException("Wrong number of arguments, there must be exactly one.");
}
if (!(new Regex(".ps1$")).Match(args[0]).Success)
{
psl_err("Must have extension of ps1 or powershell.exe will error.");
return;
}
StreamReader fs;
string line_1;
string line_2;
string exe_path;
string cmd_line;
string ps_args;
Regex r;
Match m;
bool hidden = false;
Process ps_proc;
try
{
fs = new StreamReader(args[0]);
}
catch (FileNotFoundException)
{
psl_err("File Not Found.");
return;
}
catch (FileLoadException)
{
psl_err("File found but could not be loaded.");
return;
}
try
{
line_1 = fs.ReadLine();
line_2 = fs.ReadLine();
}
catch (IOException)
{
psl_err("File may not have enough lines.");
return;
}
fs.Close();
r = new Regex(@"^#!(.*)powershell\.exe");
exe_path = "";
if ((m = r.Match(line_1)).Success)
{
line_1 = r.Replace(line_1, "");
exe_path = m.Groups[1].Value;
if (exe_path != "")
{
if (!File.Exists(exe_path + "powershell.exe"))
{
psl_err("specified powershell.exe file not exist.");
}
}
else
{
exe_path = find_powershell();
if (exe_path == "")
{
psl_err("powershell.exe is not found.");
return;
} } }
else
{
line_1 = "";
exe_path = find_powershell();
if (exe_path == "")
{
psl_err("powershell.exe is not found.");
return;
} }
cmd_line = exe_path + "\\" + "powershell.exe";
ps_args = line_1 + " -ExecutionPolicy UnRestricted -File \"" + args[0] + "\"";
r = new Regex("^#-");
if ((m = r.Match(line_2)).Success)
{
line_2 = r.Replace(line_2, "");
if ((new Regex("Hidden")).Match(line_2).Success)
{
hidden = true;
} }
ps_proc = new Process();
if (hidden)
{
ps_proc.StartInfo.RedirectStandardError = true;
ps_proc.StartInfo.RedirectStandardInput = true;
ps_proc.StartInfo.RedirectStandardOutput = true;
ps_proc.StartInfo.CreateNoWindow = true;
ps_proc.StartInfo.UseShellExecute = false;
}
ps_proc.StartInfo.Arguments = ps_args;
ps_proc.StartInfo.FileName = cmd_line;
ps_proc.Start();
}
private static void psl_err(string msg)
{
MessageBox.Show(msg, "PSLauncher Critical Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
private static string find_powershell()
{
string exe_path = "";
foreach (string test_path in
Environment.GetEnvironmentVariable("Path").Split(';'))
{
if (File.Exists(test_path + "\\" + "powershell.exe"))
exe_path = test_path;
}
if (exe_path == "" && File.Exists(
"C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe"))
exe_path = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe";
return exe_path;
} } }