forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInitialSessionState.cs
5434 lines (4760 loc) · 239 KB
/
InitialSessionState.cs
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Management.Automation.Provider;
using System.Management.Automation.Security;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.Commands;
using Debug = System.Management.Automation.Diagnostics;
namespace System.Management.Automation.Runspaces
{
internal class EarlyStartup
{
internal static void Init()
{
// Code added here should:
// * run every time we start PowerSHell
// * have high CPU cost
// * be ordered from most expensive to least expensive, or at least needed earliest
// * this method should return quickly, so all work should be run in one or more tasks.
// * code called from here should correctly handle being called twice, in case initialization
// is needed in the main code path before the task completes.
//
// Code added here should not:
// * count on running - not all hosts will call this method
// * have high disk cost
// We shouldn't create too many tasks.
#if !UNIX
// Amsi initialize can be a little slow
Task.Run(() =>
{
AmsiUtils.WinScanContent(content: string.Empty, sourceMetadata: string.Empty, warmUp: true);
});
#endif
// One other task for other stuff that's faster, but still a little slow.
Task.Run(() =>
{
// Loading the resources for System.Management.Automation can be expensive, so force that to
// happen early on a background thread.
_ = RunspaceInit.OutputEncodingDescription;
// This will init some tables and could load some assemblies.
_ = TypeAccelerators.builtinTypeAccelerators;
// This will init some tables and could load some assemblies.
LanguagePrimitives.GetEnumerator(null);
});
}
}
/// <summary>
/// Baseclass for defining elements that can be added
/// to an InitialSessionState object.
/// </summary>
public abstract class InitialSessionStateEntry
{
/// <summary>
/// The ctor so that each derived class has a name.
/// </summary>
/// <param name="name"></param>
protected InitialSessionStateEntry(string name)
{
Name = name;
}
/// <summary>
/// The name of this entry.
/// </summary>
public string Name { get; internal set; }
/// <summary>
/// The SnapIn to load from initially.
/// </summary>
public PSSnapInInfo PSSnapIn { get; private set; }
internal void SetPSSnapIn(PSSnapInInfo psSnapIn)
{
PSSnapIn = psSnapIn;
}
/// <summary>
/// The SnapIn to load from initially.
/// </summary>
public PSModuleInfo Module { get; private set; }
internal void SetModule(PSModuleInfo module)
{
Module = module;
}
/// <summary>
/// Shallow-clone this object.
/// </summary>
/// <returns>The cloned object...</returns>
public abstract InitialSessionStateEntry Clone();
}
/// <summary>
/// Class to constrain session state entries.
/// </summary>
public abstract class ConstrainedSessionStateEntry : InitialSessionStateEntry
{
/// <summary>
/// </summary>
/// <param name="name"></param>
/// <param name="visibility"></param>
protected ConstrainedSessionStateEntry(string name, SessionStateEntryVisibility visibility)
: base(name)
{
Visibility = visibility;
}
/// <summary>
/// </summary>
public SessionStateEntryVisibility Visibility { get; set; }
}
/// <summary>
/// Command class so that all the commands can derive off this one.
/// Adds the flexibility of adding additional derived class,
/// such as ProxyCommand for Exchange.
/// Derived classes - Alias, Application, Cmdlet, Function, Script.
/// </summary>
public abstract class SessionStateCommandEntry : ConstrainedSessionStateEntry
{
/// <summary>
/// Base constructor for all SessionState commands.
/// </summary>
/// <param name="name"></param>
protected SessionStateCommandEntry(string name)
: base(name, SessionStateEntryVisibility.Public)
{
}
/// <summary>
/// </summary>
/// <param name="name"></param>
/// <param name="visibility"></param>
protected internal SessionStateCommandEntry(string name, SessionStateEntryVisibility visibility)
: base(name, visibility)
{
}
/// <summary>
/// Returns the type of the command using an enum
/// instead of requiring a full reflection type check.
/// </summary>
public CommandTypes CommandType { get; internal set; }
/// <summary>
/// Is internal so it can be set by the engine code...
/// This is used to specify whether this command was imported or not
/// If noClobber is specified during Import-Module, it is set to false.
/// </summary>
internal bool _isImported = true;
}
/// <summary>
/// Type file configuration entry...
/// </summary>
public sealed class SessionStateTypeEntry : InitialSessionStateEntry
{
/// <summary>
/// Loads all entries from the types file.
/// </summary>
/// <param name="fileName"></param>
public SessionStateTypeEntry(string fileName)
: base(fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw PSTraceSource.NewArgumentException("fileName");
}
FileName = fileName.Trim();
}
/// <summary>
/// Loads all the types specified in the typeTable.
/// </summary>
/// <param name="typeTable"></param>
public SessionStateTypeEntry(TypeTable typeTable)
: base("*")
{
if (typeTable == null)
{
throw PSTraceSource.NewArgumentNullException("typeTable");
}
TypeTable = typeTable;
}
/// <summary>
/// Loads all entries from the typeData.
/// </summary>
/// <param name="typeData"></param>
/// <param name="isRemove"></param>
public SessionStateTypeEntry(TypeData typeData, bool isRemove)
: base("*")
{
if (typeData == null)
{
throw PSTraceSource.NewArgumentNullException("typeData");
}
TypeData = typeData;
IsRemove = isRemove;
}
/// <summary>
/// Shallow-clone this object.
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateTypeEntry entry;
if (FileName != null)
{
entry = new SessionStateTypeEntry(FileName);
}
else if (TypeTable != null)
{
entry = new SessionStateTypeEntry(TypeTable);
}
else
{
entry = new SessionStateTypeEntry(TypeData, IsRemove);
}
entry.SetPSSnapIn(this.PSSnapIn);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// The pathname of the types.ps1xml file. This can be null if
/// TypeTable constructor or TypeData constructor is used.
/// </summary>
public string FileName { get; }
/// <summary>
/// The TypeTable specified with constructor. This can be null if
/// FileName constructor or TypeData constructor is used.
/// </summary>
public TypeTable TypeTable { get; }
/// <summary>
/// The TypeData we want to update with. This can be null if
/// FileName constructor or TypeTable constructor is used.
/// </summary>
public TypeData TypeData { get; }
/// <summary>
/// The operation will be done on the typedata. This is only
/// meaningful when the TypeData constructor is used.
/// </summary>
public bool IsRemove { get; }
// So that we can specify the type information on the fly,
// without using Types.ps1xml file
// public SessionStateTypeEntry(string name, xmlreader definition);
// public string Definition { get; }
}
/// <summary>
/// Format file configuration entry...
/// </summary>
public sealed class SessionStateFormatEntry : InitialSessionStateEntry
{
/// <summary>
/// Loads the entire formats file.
/// </summary>
/// <param name="fileName"></param>
public SessionStateFormatEntry(string fileName)
: base("*")
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw PSTraceSource.NewArgumentException("fileName");
}
FileName = fileName.Trim();
}
/// <summary>
/// Loads all the format data specified in the formatTable.
/// </summary>
/// <param name="formattable"></param>
public SessionStateFormatEntry(FormatTable formattable)
: base("*")
{
if (formattable == null)
{
throw PSTraceSource.NewArgumentNullException("formattable");
}
Formattable = formattable;
}
/// <summary>
/// Loads all the format data specified in the typeDefinition.
/// </summary>
/// <param name="typeDefinition"></param>
public SessionStateFormatEntry(ExtendedTypeDefinition typeDefinition)
: base("*")
{
if (typeDefinition == null)
{
throw PSTraceSource.NewArgumentNullException("typeDefinition");
}
FormatData = typeDefinition;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateFormatEntry entry;
if (FileName != null)
{
entry = new SessionStateFormatEntry(FileName);
}
else if (Formattable != null)
{
entry = new SessionStateFormatEntry(Formattable);
}
else
{
entry = new SessionStateFormatEntry(FormatData);
}
entry.SetPSSnapIn(this.PSSnapIn);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// The name of the format file referenced by this entry...
/// </summary>
public string FileName { get; }
/// <summary>
/// The FormatTable specified with constructor. This can be null if
/// FileName constructor is used.
/// </summary>
public FormatTable Formattable { get; }
/// <summary>
/// The FormatData specified with constructor.
/// This can be null if the FileName or FormatTable constructors are used.
/// </summary>
public ExtendedTypeDefinition FormatData { get; }
// So that we can specify the format information on the fly,
// without using Format.ps1xml file
// public SessionStateFormatEntry(string name, xmlreader definition);
// public string Definition { get; }
}
/// <summary>
/// An assembly to load for this sessionstate...
/// </summary>
public sealed class SessionStateAssemblyEntry : InitialSessionStateEntry
{
/// <summary>
/// Create a named entry for the assembly to load with both the
/// name and the path to the assembly as a backup.
/// </summary>
/// <param name="name">The name of the assembly to load.</param>
/// <param name="fileName">The path to the assembly to use as an alternative.</param>
public SessionStateAssemblyEntry(string name, string fileName)
: base(name)
{
FileName = fileName;
}
/// <summary>
/// Create a named entry for the assembly to load, specifying
/// just the name.
/// </summary>
/// <param name="name">The name of the assembly to load.</param>
public SessionStateAssemblyEntry(string name)
: base(name)
{
}
/// <summary>
/// Shallow-clone this object.
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateAssemblyEntry entry = new SessionStateAssemblyEntry(Name, FileName);
entry.SetPSSnapIn(this.PSSnapIn);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// Return the assembly file name...
/// </summary>
public string FileName { get; }
}
/// <summary>
/// List a cmdlet to add to this session state entry.
/// </summary>
public sealed class SessionStateCmdletEntry : SessionStateCommandEntry
{
/// <summary>
/// </summary>
/// <param name="name"></param>
/// <param name="implementingType"></param>
/// <param name="helpFileName"></param>
public SessionStateCmdletEntry(string name, Type implementingType, string helpFileName)
: base(name, SessionStateEntryVisibility.Public)
{
ImplementingType = implementingType;
HelpFileName = helpFileName;
CommandType = CommandTypes.Cmdlet;
}
/// <summary>
/// </summary>
/// <param name="name"></param>
/// <param name="implementingType"></param>
/// <param name="helpFileName"></param>
/// <param name="visibility"></param>
internal SessionStateCmdletEntry(string name, Type implementingType, string helpFileName, SessionStateEntryVisibility visibility)
: base(name, visibility)
{
ImplementingType = implementingType;
HelpFileName = helpFileName;
CommandType = CommandTypes.Cmdlet;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns></returns>
public override InitialSessionStateEntry Clone()
{
SessionStateCmdletEntry entry = new SessionStateCmdletEntry(Name, ImplementingType, HelpFileName, Visibility);
entry.SetPSSnapIn(this.PSSnapIn);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// </summary>
public Type ImplementingType { get; }
/// <summary>
/// </summary>
public string HelpFileName { get; }
}
/// <summary>
/// </summary>
public sealed class SessionStateProviderEntry : ConstrainedSessionStateEntry
{
/// <summary>
/// </summary>
/// <param name="name"></param>
/// <param name="implementingType"></param>
/// <param name="helpFileName"></param>
public SessionStateProviderEntry(string name, Type implementingType, string helpFileName)
: base(name, SessionStateEntryVisibility.Public)
{
ImplementingType = implementingType;
HelpFileName = helpFileName;
}
internal SessionStateProviderEntry(string name, Type implementingType, string helpFileName, SessionStateEntryVisibility visibility)
: base(name, visibility)
{
ImplementingType = implementingType;
HelpFileName = helpFileName;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateProviderEntry entry = new SessionStateProviderEntry(Name, ImplementingType, HelpFileName, this.Visibility);
entry.SetPSSnapIn(this.PSSnapIn);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// </summary>
public Type ImplementingType { get; }
/// <summary>
/// </summary>
public string HelpFileName { get; }
}
/// <summary>
/// </summary>
public sealed class SessionStateScriptEntry : SessionStateCommandEntry
{
/// <summary>
/// Create a session state command entry instance.
/// </summary>
/// <param name="path">The path to the script.</param>
public SessionStateScriptEntry(string path)
: base(path, SessionStateEntryVisibility.Public)
{
Path = path;
CommandType = CommandTypes.ExternalScript;
}
/// <summary>
/// Create a session state command entry instance with the specified visibility.
/// </summary>
/// <param name="path">The path to the script.</param>
/// <param name="visibility">Visibility of the script.</param>
internal SessionStateScriptEntry(string path, SessionStateEntryVisibility visibility)
: base(path, visibility)
{
Path = path;
CommandType = CommandTypes.ExternalScript;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateScriptEntry entry = new SessionStateScriptEntry(Path, Visibility);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// </summary>
public string Path { get; }
}
/// <summary>
/// </summary>
public sealed class SessionStateAliasEntry : SessionStateCommandEntry
{
/// <summary>
/// Define an alias entry to add to the initial session state.
/// </summary>
/// <param name="name">The name of the alias entry to add.</param>
/// <param name="definition">The name of the command it resolves to.</param>
public SessionStateAliasEntry(string name, string definition)
: base(name, SessionStateEntryVisibility.Public)
{
Definition = definition;
CommandType = CommandTypes.Alias;
}
/// <summary>
/// Define an alias entry to add to the initial session state.
/// </summary>
/// <param name="name">The name of the alias entry to add.</param>
/// <param name="definition">The name of the command it resolves to.</param>
/// <param name="description">A description of the purpose of the alias.</param>
public SessionStateAliasEntry(string name, string definition, string description)
: base(name, SessionStateEntryVisibility.Public)
{
Definition = definition;
CommandType = CommandTypes.Alias;
Description = description;
}
/// <summary>
/// Define an alias entry to add to the initial session state.
/// </summary>
/// <param name="name">The name of the alias entry to add.</param>
/// <param name="definition">The name of the command it resolves to.</param>
/// <param name="description">A description of the purpose of the alias.</param>
/// <param name="options">Options defining the scope visibility, readonly and constant.</param>
public SessionStateAliasEntry(string name, string definition, string description, ScopedItemOptions options)
: base(name, SessionStateEntryVisibility.Public)
{
Definition = definition;
CommandType = CommandTypes.Alias;
Description = description;
Options = options;
}
/// <summary>
/// Define an alias entry to add to the initial session state.
/// </summary>
/// <param name="name">The name of the alias entry to add.</param>
/// <param name="definition">The name of the command it resolves to.</param>
/// <param name="description">A description of the purpose of the alias.</param>
/// <param name="options">Options defining the scope visibility, readonly and constant.</param>
/// <param name="visibility"></param>
internal SessionStateAliasEntry(string name, string definition, string description,
ScopedItemOptions options, SessionStateEntryVisibility visibility)
: base(name, visibility)
{
Definition = definition;
CommandType = CommandTypes.Alias;
Description = description;
Options = options;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateAliasEntry entry = new SessionStateAliasEntry(Name, Definition, Description, Options, Visibility);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// The string defining the body of this alias...
/// </summary>
public string Definition { get; }
/// <summary>
/// A string describing this alias...
/// </summary>
public string Description { get; } = string.Empty;
/// <summary>
/// Options controling scope visibility and setability for this entry.
/// </summary>
public ScopedItemOptions Options { get; } = ScopedItemOptions.None;
}
/// <summary>
/// </summary>
public sealed class SessionStateApplicationEntry : SessionStateCommandEntry
{
/// <summary>
/// Used to define a permitted script in this session state. If the path is
/// "*", then any path is permitted.
/// </summary>
/// <param name="path">The full path to the application.</param>
public SessionStateApplicationEntry(string path)
: base(path, SessionStateEntryVisibility.Public)
{
Path = path;
CommandType = CommandTypes.Application;
}
/// <summary>
/// Used to define a permitted script in this session state. If the path is
/// "*", then any path is permitted.
/// </summary>
/// <param name="path">The full path to the application.</param>
/// <param name="visibility">Sets the external visibility of the path.</param>
internal SessionStateApplicationEntry(string path, SessionStateEntryVisibility visibility)
: base(path, visibility)
{
Path = path;
CommandType = CommandTypes.Application;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateApplicationEntry entry = new SessionStateApplicationEntry(Path, Visibility);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// The path to this application...
/// </summary>
public string Path { get; }
}
/// <summary>
/// </summary>
public sealed class SessionStateFunctionEntry : SessionStateCommandEntry
{
/// <summary>
/// Represents a function definition in an Initial session state object.
/// </summary>
/// <param name="name">The name of the function.</param>
/// <param name="definition">The definition of the function.</param>
/// <param name="options">Options controlling scope-related elements of this object.</param>
/// <param name="helpFile">The name of the help file associated with the function.</param>
public SessionStateFunctionEntry(string name, string definition, ScopedItemOptions options, string helpFile)
: base(name, SessionStateEntryVisibility.Public)
{
Definition = definition;
CommandType = CommandTypes.Function;
Options = options;
ScriptBlock = ScriptBlock.Create(Definition);
ScriptBlock.LanguageMode = PSLanguageMode.FullLanguage;
HelpFile = helpFile;
}
/// <summary>
/// Represents a function definition in an Initial session state object.
/// </summary>
/// <param name="name">The name of the function.</param>
/// <param name="definition">The definition of the function.</param>
/// <param name="helpFile">The name of the help file associated with the function.</param>
public SessionStateFunctionEntry(string name, string definition, string helpFile)
: this(name, definition, ScopedItemOptions.None, helpFile)
{
}
/// <summary>
/// Represents a function definition in an Initial session state object.
/// </summary>
/// <param name="name">The name of the function.</param>
/// <param name="definition">The definition of the function.</param>
public SessionStateFunctionEntry(string name, string definition)
: this(name, definition, ScopedItemOptions.None, null)
{
}
/// <summary>
/// This is an internal copy constructor.
/// </summary>
internal SessionStateFunctionEntry(string name, string definition, ScopedItemOptions options,
SessionStateEntryVisibility visibility, ScriptBlock scriptBlock, string helpFile)
: base(name, visibility)
{
Definition = definition;
CommandType = CommandTypes.Function;
Options = options;
ScriptBlock = scriptBlock;
HelpFile = helpFile;
}
internal static SessionStateFunctionEntry GetDelayParsedFunctionEntry(string name, string definition, bool isProductCode, PSLanguageMode languageMode)
{
var fnEntry = GetDelayParsedFunctionEntry(name, definition, isProductCode);
fnEntry.ScriptBlock.LanguageMode = languageMode;
return fnEntry;
}
internal static SessionStateFunctionEntry GetDelayParsedFunctionEntry(string name, string definition, bool isProductCode)
{
var sb = ScriptBlock.CreateDelayParsedScriptBlock(definition, isProductCode);
return new SessionStateFunctionEntry(name, definition, ScopedItemOptions.None,
SessionStateEntryVisibility.Public, sb, null);
}
internal static SessionStateFunctionEntry GetDelayParsedFunctionEntry(string name, string definition, ScriptBlock sb)
{
return new SessionStateFunctionEntry(name, definition, ScopedItemOptions.None,
SessionStateEntryVisibility.Public, sb, null);
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
SessionStateFunctionEntry entry = new SessionStateFunctionEntry(Name, Definition, Options, Visibility, ScriptBlock, HelpFile);
entry.SetModule(this.Module);
return entry;
}
/// <summary>
/// Sets the name of the help file associated with the function.
/// </summary>
internal void SetHelpFile(string help)
{
HelpFile = help;
}
/// <summary>
/// The string to use to define this function...
/// </summary>
public string Definition { get; }
/// <summary>
/// The script block for this function.
/// </summary>
internal ScriptBlock ScriptBlock { get; set; }
/// <summary>
/// Options controling scope visibility and setability for this entry.
/// </summary>
public ScopedItemOptions Options { get; } = ScopedItemOptions.None;
/// <summary>
/// The name of the help file associated with the function.
/// </summary>
public string HelpFile { get; private set; }
}
/// <summary>
/// </summary>
public sealed class SessionStateVariableEntry : ConstrainedSessionStateEntry
{
/// <summary>
/// Is used to define a variable that should be created when
/// the runspace is opened. Note - if this object is cloned,
/// then the clone will contain a reference to the original object
/// not a clone of it.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The value to set the variable to.</param>
/// <param name="description">A descriptive string to attach to the variable.</param>
public SessionStateVariableEntry(string name, object value, string description)
: base(name, SessionStateEntryVisibility.Public)
{
Value = value;
Description = description;
}
/// <summary>
/// Is used to define a variable that should be created when
/// the runspace is opened. Note - if this object is cloned,
/// then the clone will contain a reference to the original object
/// not a clone of it.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The value to set the variable to.</param>
/// <param name="description">A descriptive string to attach to the variable.</param>
/// <param name="options">Options like readonly, constant, allscope, etc.</param>
public SessionStateVariableEntry(string name, object value, string description, ScopedItemOptions options)
: base(name, SessionStateEntryVisibility.Public)
{
Value = value;
Description = description;
Options = options;
}
/// <summary>
/// Is used to define a variable that should be created when
/// the runspace is opened. Note - if this object is cloned,
/// then the clone will contain a reference to the original object
/// not a clone of it.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The value to set the variable to.</param>
/// <param name="description">A descriptive string to attach to the variable.</param>
/// <param name="options">Options like readonly, constant, allscope, etc.</param>
/// <param name="attributes">A list of attributes to attach to the variable.</param>
public SessionStateVariableEntry(string name, object value, string description,
ScopedItemOptions options, Collection<Attribute> attributes)
: base(name, SessionStateEntryVisibility.Public)
{
Value = value;
Description = description;
Options = options;
_attributes = attributes;
}
/// <summary>
/// Is used to define a variable that should be created when
/// the runspace is opened. Note - if this object is cloned,
/// then the clone will contain a reference to the original object
/// not a clone of it.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The value to set the variable to.</param>
/// <param name="description">A descriptive string to attach to the variable.</param>
/// <param name="options">Options like readonly, constant, allscope, etc.</param>
/// <param name="attribute">A single attribute to attach to the variable.</param>
public SessionStateVariableEntry(string name, object value, string description,
ScopedItemOptions options, Attribute attribute)
: base(name, SessionStateEntryVisibility.Public)
{
Value = value;
Description = description;
Options = options;
_attributes = new Collection<Attribute>();
_attributes.Add(attribute);
}
/// <summary>
/// Is used to define a variable that should be created when
/// the runspace is opened. Note - if this object is cloned,
/// then the clone will contain a reference to the original object
/// not a clone of it.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The value to set the variable to.</param>
/// <param name="description">A descriptive string to attach to the variable.</param>
/// <param name="options">Options like readonly, constant, allscope, etc.</param>
/// <param name="attributes">A single attribute to attach to the variable.</param>
/// <param name="visibility"></param>
internal SessionStateVariableEntry(string name, object value, string description,
ScopedItemOptions options, Collection<Attribute> attributes, SessionStateEntryVisibility visibility)
: base(name, visibility)
{
Value = value;
Description = description;
Options = options;
_attributes = attributes;
}
/// <summary>
/// Shallow-clone this object...
/// </summary>
/// <returns>The cloned object.</returns>
public override InitialSessionStateEntry Clone()
{
// Copy the attribute collection if necessary...
Collection<Attribute> attrs = null;
if (_attributes != null && _attributes.Count > 0)
attrs = new Collection<Attribute>(_attributes);
return new SessionStateVariableEntry(Name, Value, Description, Options, attrs, Visibility);
}
/// <summary>
/// The value to bind to this variable.
/// </summary>
public object Value { get; }
/// <summary>
/// The description associated with this variable.
/// </summary>
public string Description { get; } = string.Empty;
/// <summary>
/// The options associated with this variable (e.g. readonly, allscope, etc.)
/// </summary>
public ScopedItemOptions Options { get; } = ScopedItemOptions.None;
/// <summary>
/// The attributes that will be attached to this object.
/// </summary>
public Collection<Attribute> Attributes
{
get { return _attributes ?? (_attributes = new Collection<Attribute>()); }
}
private Collection<Attribute> _attributes;
}
/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class InitialSessionStateEntryCollection<T> : IEnumerable<T> where T : InitialSessionStateEntry
{
/// <summary>
/// Create an empty collection...
/// </summary>
public InitialSessionStateEntryCollection()
{
_internalCollection = new Collection<T>();
}
/// <summary>
/// Create an new collection, copying in the passed items...
/// </summary>
/// <param name="items"></param>
public InitialSessionStateEntryCollection(IEnumerable<T> items)
{
if (items == null)
{
throw new ArgumentNullException("items");
}
_internalCollection = new Collection<T>();
foreach (T item in items)
{
_internalCollection.Add(item);
}
}
/// <summary>
/// Clone this collection.
/// </summary>
/// <returns>The cloned collection.</returns>
public InitialSessionStateEntryCollection<T> Clone()