forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetHostValue.cs
More file actions
602 lines (551 loc) · 22.8 KB
/
Copy pathSetHostValue.cs
File metadata and controls
602 lines (551 loc) · 22.8 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
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
600
601
602
using System;
using System.Reflection;
using System.Activities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Tracing;
using System.ComponentModel;
namespace Microsoft.PowerShell.Activities
{
/// <summary>
/// Activity to set a host value in a Workflow.
/// </summary>
public sealed class SetPSWorkflowData : NativeActivity
{
/// <summary>
/// The variable to set, if not included in the PSWorkflowRuntimeVariable enum.
/// </summary>
[DefaultValue(null)]
public InArgument<string> OtherVariableName
{
get;
set;
}
/// <summary>
///
/// </summary>
[DefaultValue(null)]
public InArgument<Object> Value
{
get;
set;
}
/// <summary>
/// Defines the remoting behavior to use when invoking this activity.
/// </summary>
[ConnectivityCategory]
[DefaultValue(RemotingBehavior.PowerShell)]
public InArgument<RemotingBehavior> PSRemotingBehavior { get; set; }
/// <summary>
/// Defines the number of retries that the activity will make to connect to a remote
/// machine when it encounters an error. The default is to not retry.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSConnectionRetryCount
{
get;
set;
}
/// <summary>
/// Defines the delay, in seconds, between connection retry attempts.
/// The default is one second.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSConnectionRetryIntervalSec
{
get;
set;
}
/// <summary>
/// The Input stream for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<PSDataCollection<PSObject>> Input
{
get;
set;
}
/// <summary>
/// Determines whether to connect the input stream for this activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(false)]
public bool UseDefaultInput
{
get;
set;
}
/// <summary>
/// The output stream from the activity
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<PSObject>> Result
{
get;
set;
}
/// <summary>
/// Determines whether to append output to Result.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public bool? AppendOutput
{
get;
set;
}
/// <summary>
/// The Error stream / collection for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<ErrorRecord>> PSError
{
get;
set;
}
/// <summary>
/// The Progress stream / collection for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<ProgressRecord>> PSProgress
{
get;
set;
}
/// <summary>
/// The Verbose stream / collection for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<VerboseRecord>> PSVerbose
{
get;
set;
}
/// <summary>
/// The Debug stream / collection for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<DebugRecord>> PSDebug
{
get;
set;
}
/// <summary>
/// The Warning stream / collection for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<WarningRecord>> PSWarning
{
get;
set;
}
/// <summary>
/// The Information stream / collection for the activity.
/// </summary>
[InputAndOutputCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InOutArgument<PSDataCollection<InformationRecord>> PSInformation
{
get;
set;
}
/// <summary>
/// The computer name to invoke this activity on.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<string[]> PSComputerName
{
get;
set;
}
/// <summary>
/// Defines the credential to use in the remote connection.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<PSCredential> PSCredential
{
get;
set;
}
/// <summary>
/// Forces the activity to return non-serialized objects. Resulting objects
/// have functional methods and properties (as opposed to serialized versions
/// of them), but will not survive persistence when the Workflow crashes or is
/// persisted.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<bool?> PSDisableSerialization
{
get;
set;
}
/// <summary>
/// Forces the activity to not call the persist functionality, which will be responsible for
/// persisting the workflow state onto the disk.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<bool?> PSPersist
{
get;
set;
}
/// <summary>
/// Determines whether to merge error data to the output stream
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<bool?> MergeErrorToOutput
{
get;
set;
}
/// <summary>
/// Defines the maximum amount of time, in seconds, that this activity may run.
/// The default is unlimited.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSActionRunningTimeoutSec
{
get;
set;
}
/// <summary>
/// Defines the maximum amount of time that the workflow engine should wait for a bookmark
/// to be resumed.
/// The default is unlimited.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSBookmarkTimeoutSec
{
get;
set;
}
/// <summary>
/// This the list of module names (or paths) that are required to run this Activity successfully.
/// The default is null.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<string[]> PSRequiredModules
{
get;
set;
}
/// <summary>
/// Defines the number of retries that the activity will make when it encounters
/// an error during execution of its action. The default is to not retry.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSActionRetryCount
{
get;
set;
}
/// <summary>
/// Defines the delay, in seconds, between action retry attempts.
/// The default is one second.
/// </summary>
[BehaviorCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSActionRetryIntervalSec
{
get;
set;
}
/// <summary>
/// The port to use in a remote connection attempt. The default is:
/// HTTP: 5985, HTTPS: 5986.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<uint?> PSPort { get; set; }
/// <summary>
/// Determines whether to use SSL in the connection attempt. The default is false.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<bool?> PSUseSsl { get; set; }
/// <summary>
/// Determines whether to allow redirection by the remote computer. The default is false.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<bool?> PSAllowRedirection { get; set; }
/// <summary>
/// Defines the remote application name to connect to. The default is "wsman".
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<string> PSApplicationName { get; set; }
/// <summary>
/// Defines the remote configuration name to connect to. The default is "Microsoft.PowerShell".
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<string> PSConfigurationName { get; set; }
/// <summary>
/// Defines the fully-qualified remote URI to connect to. When specified, the PSComputerName,
/// PSApplicationName, PSConfigurationName, and PSPort are not used.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<string[]> PSConnectionUri { get; set; }
/// <summary>
/// Defines the authentication type to be used in the remote connection.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This is forced by the interaction of PowerShell and Workflow.")]
public InArgument<AuthenticationMechanism?> PSAuthentication { get; set; }
/// <summary>
/// Defines the certificate thumbprint to be used in the remote connection.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<string> PSCertificateThumbprint { get; set; }
/// <summary>
/// Defines any session options to be used in the remote connection.
/// </summary>
[ConnectivityCategory]
[DefaultValue(null)]
public InArgument<PSSessionOption> PSSessionOption { get; set; }
/// <summary>
/// Execute the logic for this activity...
/// </summary>
/// <param name="context"></param>
protected override void Execute(NativeActivityContext context)
{
// Retrieve our host overrides
HostParameterDefaults hostValues = context.GetExtension<HostParameterDefaults>();
SetHostValuesByVariableName(context, hostValues);
SetHostValuesByProperty(context, hostValues);
}
private void SetHostValuesByProperty(NativeActivityContext context, HostParameterDefaults hostValues)
{
Type currentType = this.GetType();
// Populate any of our parameters into the host defaults.
foreach (PropertyInfo field in currentType.GetProperties())
{
// See if it's an argument
if (typeof(Argument).IsAssignableFrom(field.PropertyType))
{
// Skip the ones that are specific to this activity
if (String.Equals("VariableToSet", field.Name, StringComparison.OrdinalIgnoreCase) ||
String.Equals("OtherVariableName", field.Name, StringComparison.OrdinalIgnoreCase) ||
String.Equals("Value", field.Name, StringComparison.OrdinalIgnoreCase))
{
continue;
}
// Handle Bookmark timeouts, but don't set them as a host default.
if (String.Equals("PSBookmarkTimeoutSec", field.Name, StringComparison.OrdinalIgnoreCase))
{
// See if this is trying to change the bookmark timeout
if (PSBookmarkTimeoutSec.Get(context).HasValue)
{
SafelySetResumeBookmarkTimeout(TimeSpan.FromSeconds(PSBookmarkTimeoutSec.Get(context).Value));
}
else
{
SafelySetResumeBookmarkTimeout(TimeSpan.FromSeconds(0));
}
continue;
}
// Get the argument
Argument currentArgument = (Argument)field.GetValue(this, null);
if (currentArgument.Expression != null)
{
if (currentArgument.Get(context) == null)
{
hostValues.Parameters.Remove(field.Name);
}
else
{
hostValues.Parameters[field.Name] = currentArgument.Get(context);
}
}
}
}
}
private void SetHostValuesByVariableName(NativeActivityContext context, HostParameterDefaults hostValues)
{
// Set the Command / host metadata
string variableName = null;
if (OtherVariableName.Get(context) != null)
{
if (OtherVariableName.Expression != null)
{
string value = OtherVariableName.Get(context);
if (!string.IsNullOrWhiteSpace(value))
{
variableName = value;
}
}
if (String.Equals(variableName, "Position", StringComparison.OrdinalIgnoreCase))
{
HostSettingCommandMetadata metadata = hostValues.HostCommandMetadata;
// The position should come in as line:column:command
string positionMessage = (string)Value.Get(context);
string[] positionElements = positionMessage.Split(new char[] { ':' }, 3);
string line = positionElements[0].Trim();
string column = positionElements[1].Trim();
string commandName = positionElements[2].Trim();
if (!String.IsNullOrEmpty(line))
{
metadata.StartLineNumber = Int32.Parse(line, CultureInfo.InvariantCulture);
}
if (!String.IsNullOrEmpty(column))
{
metadata.StartColumnNumber = Int32.Parse(line, CultureInfo.InvariantCulture);
}
if (!String.IsNullOrEmpty(commandName))
{
metadata.CommandName = commandName;
}
}
else
{
if (Value.Get(context) == null)
{
hostValues.Parameters.Remove(variableName);
}
else
{
hostValues.Parameters[variableName] = Value.Get(context);
}
}
}
}
/// <summary>
/// Internal reflection call to set the ResumeBookmarkTimeout property, which
/// controls how much time Workflow allows an activity to run while a bookmark is
/// being resumed. The workflow default is 30 seconds, which can be exceeded
/// on heavily loaded systems especially on parallel workflows.
/// There is not a public property for this value, so the implementation below is
/// the one recommended by the workflow team.
/// </summary>
/// <param name="timeout">How long to wait.</param>
private static void SafelySetResumeBookmarkTimeout(TimeSpan timeout)
{
Type activityDefaults = Type.GetType("System.Activities.ActivityDefaults, System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
if (activityDefaults != null)
{
FieldInfo resumeBookmarkTimeout = activityDefaults.GetField("ResumeBookmarkTimeout");
if (resumeBookmarkTimeout != null)
{
// This is an attempt to reset the workflow default.
if (timeout.TotalSeconds == 0)
{
// First see if it's been explicitly set. If so, don't reset it.
TimeSpan currentTimeout = (TimeSpan)resumeBookmarkTimeout.GetValue(null);
if (currentTimeout.TotalSeconds == 30)
{
resumeBookmarkTimeout.SetValue(null, TimeSpan.MaxValue);
}
}
else
{
// They've specified a value. Use it.
resumeBookmarkTimeout.SetValue(null, timeout);
}
}
else
{
System.Diagnostics.Debug.Fail("Could not find ResumeBookmarkTimeout property");
}
}
else
{
System.Diagnostics.Debug.Fail("Could not find ResumeBookmarkTimeout type");
}
}
}
}