1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . Text ;
4
- using System . Threading ;
3
+ using System . Web ;
5
4
using System . Xml . Linq ;
6
5
using Microsoft . Deployment . WindowsInstaller ;
7
6
7
+ using View = Microsoft . Deployment . WindowsInstaller . View ;
8
+
8
9
namespace PowerShellActions
9
10
{
10
11
public class CustomActions
11
12
{
13
+ public const uint TickIncrement = 10000 ;
14
+
15
+ // Specify or calculate the total number of ticks the custom action adds to the length of the ProgressBar
16
+ public const uint TotalTicks = TickIncrement * NumberItems ;
17
+ private const uint NumberItems = 100 ;
18
+
12
19
[ CustomAction ]
13
20
public static ActionResult PowerShellFilesImmediate ( Session session )
14
21
{
@@ -22,26 +29,31 @@ public static ActionResult PowerShellFilesImmediate(Session session)
22
29
View view = db . OpenView ( "SELECT `Id`, `File`, `Arguments` FROM `PowerShellFiles`" ) ;
23
30
view . Execute ( ) ;
24
31
25
- var data = new CustomActionData ( ) ;
26
-
27
- XDocument doc = new XDocument ( new XElement ( "r" ) ) ;
32
+ var doc = new XDocument ( new XDeclaration ( "1.0" , "utf-16" , "yes" ) , new XElement ( "r" ) ) ;
28
33
29
34
foreach ( Record row in view )
30
35
{
31
- doc . Root . Add ( new XElement ( "d" , new XAttribute ( "Id" , row [ "Id" ] ) , new XAttribute ( "file" , session . Format ( row [ "File" ] . ToString ( ) ) ) , new XAttribute ( "args" , session . Format ( row [ "Arguments" ] . ToString ( ) ) ) ) ) ;
36
+ // XML comes in with entities already. We need to decode these before putting them back into XML again
37
+ var args = HttpUtility . HtmlDecode ( session . Format ( row [ "Arguments" ] . ToString ( ) ) ) ;
38
+
39
+ session . Log ( "args '{0}'" , args ) ;
40
+
41
+ doc . Root . Add ( new XElement ( "d" , new XAttribute ( "Id" , row [ "Id" ] ) , new XAttribute ( "file" , session . Format ( row [ "File" ] . ToString ( ) ) ) , new XAttribute ( "args" , args ) ) ) ;
32
42
}
33
43
34
- session [ "PowerShellFilesDeferred" ] = doc . ToString ( ) ;
44
+ var cad = new CustomActionData { { "xml" , doc . ToString ( ) } } ;
45
+
46
+ session [ "PowerShellFilesDeferred" ] = cad . ToString ( ) ;
35
47
36
48
// Tell the installer to increase the value of the final total
37
49
// length of the progress bar by the total number of ticks in
38
50
// the custom action.
39
- Record hProgressRec = new Record ( 2 ) ;
51
+ var hProgressRec = new Record ( 2 ) ;
40
52
41
53
hProgressRec [ 1 ] = 3 ;
42
- hProgressRec [ 2 ] = iTotalTicks ;
54
+ hProgressRec [ 2 ] = TotalTicks ;
43
55
MessageResult iResult = session . Message ( InstallMessage . Progress , hProgressRec ) ;
44
- if ( ( iResult == MessageResult . Cancel ) )
56
+ if ( iResult == MessageResult . Cancel )
45
57
{
46
58
return ActionResult . UserExit ;
47
59
}
@@ -63,20 +75,19 @@ public static ActionResult PowerShellFilesImmediate(Session session)
63
75
public static ActionResult PowerShellFilesDeferred ( Session session )
64
76
{
65
77
session . Log ( "PowerShellFilesDeferred start" ) ;
66
- Record hActionRec = new Record ( 3 ) ;
67
- Record hProgressRec = new Record ( 3 ) ;
78
+ var hActionRec = new Record ( 3 ) ;
79
+ var hProgressRec = new Record ( 3 ) ;
68
80
69
81
// Installer is executing the installation script. Set up a
70
82
// record specifying appropriate templates and text for
71
83
// messages that will inform the user about what the custom
72
84
// action is doing. Tell the installer to use this template and
73
85
// text in progress messages.
74
-
75
86
hActionRec [ 1 ] = "PowerShellFilesDeferred" ;
76
87
hActionRec [ 2 ] = "PowerShell Files" ;
77
88
hActionRec [ 3 ] = "[1] of [2], [3]" ;
78
89
MessageResult iResult = session . Message ( InstallMessage . ActionStart , hActionRec ) ;
79
- if ( ( iResult == MessageResult . Cancel ) )
90
+ if ( iResult == MessageResult . Cancel )
80
91
{
81
92
return ActionResult . UserExit ;
82
93
}
@@ -86,39 +97,38 @@ public static ActionResult PowerShellFilesDeferred(Session session)
86
97
hProgressRec [ 2 ] = 1 ;
87
98
hProgressRec [ 3 ] = 0 ;
88
99
iResult = session . Message ( InstallMessage . Progress , hProgressRec ) ;
89
- if ( ( iResult == MessageResult . Cancel ) )
100
+ if ( iResult == MessageResult . Cancel )
90
101
{
91
102
return ActionResult . UserExit ;
92
103
}
93
104
94
105
try
95
106
{
96
- var doc = XDocument . Parse ( session . CustomActionData . ToString ( ) ) ;
107
+ string content = session . CustomActionData [ "xml" ] ;
97
108
98
- foreach ( var row in doc . Root . Elements ( "d" ) )
109
+ XDocument doc = XDocument . Parse ( content ) ;
110
+
111
+ foreach ( XElement row in doc . Root . Elements ( "d" ) )
99
112
{
100
113
string file = row . Attribute ( "file" ) . Value ;
101
114
102
115
string arguments = row . Attribute ( "args" ) . Value ;
103
116
104
- using ( var sr = new System . IO . StreamReader ( file ) )
117
+ using ( var task = new PowerShellTask ( file , arguments , session ) )
105
118
{
106
- string content = sr . ReadToEnd ( ) ;
107
-
108
- using ( var task = new PowerShellTask ( file , arguments , session ) )
109
- {
110
- task . Execute ( ) ;
111
- }
119
+ task . Execute ( ) ;
112
120
}
113
121
}
122
+
114
123
return ActionResult . Success ;
115
124
}
116
125
catch ( Exception ex )
117
126
{
118
- session . Log ( ex . Message ) ;
127
+ session . Log ( ex . ToString ( ) ) ;
119
128
return ActionResult . Failure ;
120
129
}
121
130
}
131
+
122
132
[ CustomAction ]
123
133
public static ActionResult PowerShellScriptsImmediate ( Session session )
124
134
{
@@ -144,12 +154,12 @@ public static ActionResult PowerShellScriptsImmediate(Session session)
144
154
// Tell the installer to increase the value of the final total
145
155
// length of the progress bar by the total number of ticks in
146
156
// the custom action.
147
- Record hProgressRec = new Record ( 2 ) ;
157
+ var hProgressRec = new Record ( 2 ) ;
148
158
149
159
hProgressRec [ 1 ] = 3 ;
150
- hProgressRec [ 2 ] = iTotalTicks ;
160
+ hProgressRec [ 2 ] = TotalTicks ;
151
161
MessageResult iResult = session . Message ( InstallMessage . Progress , hProgressRec ) ;
152
- if ( ( iResult == MessageResult . Cancel ) )
162
+ if ( iResult == MessageResult . Cancel )
153
163
{
154
164
return ActionResult . UserExit ;
155
165
}
@@ -158,7 +168,7 @@ public static ActionResult PowerShellScriptsImmediate(Session session)
158
168
}
159
169
catch ( Exception ex )
160
170
{
161
- session . Log ( ex . Message ) ;
171
+ session . Log ( ex . ToString ( ) ) ;
162
172
return ActionResult . Failure ;
163
173
}
164
174
finally
@@ -167,40 +177,23 @@ public static ActionResult PowerShellScriptsImmediate(Session session)
167
177
}
168
178
}
169
179
170
- private static void ExtendProgressForCustomAction ( Session session , int total )
171
- {
172
- var record = new Record ( 2 ) ;
173
- record [ 1 ] = 3 ;
174
- record [ 2 ] = total ;
175
- session . Message ( InstallMessage . Progress , record ) ;
176
- }
177
-
178
- // Specify or calculate the number of ticks in an increment
179
- // to the ProgressBar
180
- public const uint iTickIncrement = 10000 ;
181
-
182
- // Specify or calculate the total number of ticks the custom action adds to the length of the ProgressBar
183
- const uint iNumberItems = 100 ;
184
- public const uint iTotalTicks = iTickIncrement * iNumberItems ;
185
-
186
-
180
+ // Specify or calculate the number of ticks in an increment to the ProgressBar
187
181
[ CustomAction ]
188
182
public static ActionResult PowerShellScriptsDeferred ( Session session )
189
183
{
190
- Record hActionRec = new Record ( 3 ) ;
191
- Record hProgressRec = new Record ( 3 ) ;
184
+ var hActionRec = new Record ( 3 ) ;
185
+ var hProgressRec = new Record ( 3 ) ;
192
186
193
187
// Installer is executing the installation script. Set up a
194
188
// record specifying appropriate templates and text for
195
189
// messages that will inform the user about what the custom
196
190
// action is doing. Tell the installer to use this template and
197
191
// text in progress messages.
198
-
199
192
hActionRec [ 1 ] = "PowerShellScriptsDeferred" ;
200
193
hActionRec [ 2 ] = "PowerShell Scripts" ;
201
194
hActionRec [ 3 ] = "[1] of [2], [3]" ;
202
195
MessageResult iResult = session . Message ( InstallMessage . ActionStart , hActionRec ) ;
203
- if ( ( iResult == MessageResult . Cancel ) )
196
+ if ( iResult == MessageResult . Cancel )
204
197
{
205
198
return ActionResult . UserExit ;
206
199
}
@@ -210,7 +203,7 @@ public static ActionResult PowerShellScriptsDeferred(Session session)
210
203
hProgressRec [ 2 ] = 1 ;
211
204
hProgressRec [ 3 ] = 0 ;
212
205
iResult = session . Message ( InstallMessage . Progress , hProgressRec ) ;
213
- if ( ( iResult == MessageResult . Cancel ) )
206
+ if ( iResult == MessageResult . Cancel )
214
207
{
215
208
return ActionResult . UserExit ;
216
209
}
@@ -219,7 +212,7 @@ public static ActionResult PowerShellScriptsDeferred(Session session)
219
212
{
220
213
CustomActionData data = session . CustomActionData ;
221
214
222
- foreach ( KeyValuePair < string , string > datum in data )
215
+ foreach ( var datum in data )
223
216
{
224
217
string script = Encoding . Unicode . GetString ( Convert . FromBase64String ( datum . Value ) ) ;
225
218
@@ -228,20 +221,14 @@ public static ActionResult PowerShellScriptsDeferred(Session session)
228
221
task . Execute ( ) ;
229
222
}
230
223
}
224
+
231
225
return ActionResult . Success ;
232
226
}
233
227
catch ( Exception ex )
234
228
{
235
- session . Log ( ex . Message ) ;
229
+ session . Log ( ex . ToString ( ) ) ;
236
230
return ActionResult . Failure ;
237
231
}
238
232
}
239
-
240
- private static void DisplayWarningMessage ( Session session , string message )
241
- {
242
- var record = new Record ( 0 ) ;
243
- record [ 0 ] = message ;
244
- session . Message ( InstallMessage . Warning , record ) ;
245
- }
246
233
}
247
- }
234
+ }
0 commit comments