forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMshSnapinInfo.cs
More file actions
1345 lines (1188 loc) · 52 KB
/
Copy pathMshSnapinInfo.cs
File metadata and controls
1345 lines (1188 loc) · 52 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
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.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Text;
using Microsoft.PowerShell.Commands;
using Microsoft.Win32;
using Dbg = System.Management.Automation.Diagnostics;
using Regex = System.Text.RegularExpressions.Regex;
namespace System.Management.Automation
{
internal static class RegistryStrings
{
/// <summary>
/// Root key path under HKLM.
/// </summary>
internal const string MonadRootKeyPath = "Software\\Microsoft\\PowerShell";
/// <summary>
/// Root key name.
/// </summary>
internal const string MonadRootKeyName = "PowerShell";
/// <summary>
/// Key for monad engine.
/// </summary>
internal const string MonadEngineKey = "PowerShellEngine";
// Name for various values under PSEngine
internal const string MonadEngine_ApplicationBase = "ApplicationBase";
internal const string MonadEngine_ConsoleHostAssemblyName = "ConsoleHostAssemblyName";
internal const string MonadEngine_ConsoleHostModuleName = "ConsoleHostModuleName";
internal const string MonadEngine_RuntimeVersion = "RuntimeVersion";
internal const string MonadEngine_MonadVersion = "PowerShellVersion";
/// <summary>
/// Key under which all the mshsnapin live.
/// </summary>
internal const string MshSnapinKey = "PowerShellSnapIns";
// Name of various values for each mshsnapin
internal const string MshSnapin_ApplicationBase = "ApplicationBase";
internal const string MshSnapin_AssemblyName = "AssemblyName";
internal const string MshSnapin_ModuleName = "ModuleName";
internal const string MshSnapin_MonadVersion = "PowerShellVersion";
internal const string MshSnapin_BuiltInTypes = "Types";
internal const string MshSnapin_BuiltInFormats = "Formats";
internal const string MshSnapin_Description = "Description";
internal const string MshSnapin_Version = "Version";
internal const string MshSnapin_Vendor = "Vendor";
internal const string MshSnapin_DescriptionResource = "DescriptionIndirect";
internal const string MshSnapin_VendorResource = "VendorIndirect";
internal const string MshSnapin_LogPipelineExecutionDetails = "LogPipelineExecutionDetails";
// Name of default mshsnapins
internal const string CoreMshSnapinName = "Microsoft.PowerShell.Core";
internal const string HostMshSnapinName = "Microsoft.PowerShell.Host";
internal const string ManagementMshSnapinName = "Microsoft.PowerShell.Management";
internal const string SecurityMshSnapinName = "Microsoft.PowerShell.Security";
internal const string UtilityMshSnapinName = "Microsoft.PowerShell.Utility";
}
/// <summary>
/// Contains information about a mshsnapin.
/// </summary>
public class PSSnapInInfo
{
internal PSSnapInInfo
(
string name,
bool isDefault,
string applicationBase,
string assemblyName,
string moduleName,
Version psVersion,
Version version,
Collection<string> types,
Collection<string> formats,
string descriptionFallback,
string vendorFallback
)
{
if (string.IsNullOrEmpty(name))
{
throw PSTraceSource.NewArgumentNullException("name");
}
if (string.IsNullOrEmpty(applicationBase))
{
throw PSTraceSource.NewArgumentNullException("applicationBase");
}
if (string.IsNullOrEmpty(assemblyName))
{
throw PSTraceSource.NewArgumentNullException("assemblyName");
}
if (string.IsNullOrEmpty(moduleName))
{
throw PSTraceSource.NewArgumentNullException("moduleName");
}
if (psVersion == null)
{
throw PSTraceSource.NewArgumentNullException("psVersion");
}
if (version == null)
{
version = new Version("0.0");
}
if (types == null)
{
types = new Collection<string>();
}
if (formats == null)
{
formats = new Collection<string>();
}
if (descriptionFallback == null)
{
descriptionFallback = string.Empty;
}
if (vendorFallback == null)
{
vendorFallback = string.Empty;
}
Name = name;
IsDefault = isDefault;
ApplicationBase = applicationBase;
AssemblyName = assemblyName;
ModuleName = moduleName;
PSVersion = psVersion;
Version = version;
Types = types;
Formats = formats;
_descriptionFallback = descriptionFallback;
_vendorFallback = vendorFallback;
}
internal PSSnapInInfo
(
string name,
bool isDefault,
string applicationBase,
string assemblyName,
string moduleName,
Version psVersion,
Version version,
Collection<string> types,
Collection<string> formats,
string description,
string descriptionFallback,
string vendor,
string vendorFallback
)
: this(name, isDefault, applicationBase, assemblyName, moduleName, psVersion, version, types, formats, descriptionFallback, vendorFallback)
{
_description = description;
_vendor = vendor;
}
internal PSSnapInInfo
(
string name,
bool isDefault,
string applicationBase,
string assemblyName,
string moduleName,
Version psVersion,
Version version,
Collection<string> types,
Collection<string> formats,
string description,
string descriptionFallback,
string descriptionIndirect,
string vendor,
string vendorFallback,
string vendorIndirect
) : this(name, isDefault, applicationBase, assemblyName, moduleName, psVersion, version, types, formats, description, descriptionFallback, vendor, vendorFallback)
{
// add descriptionIndirect and vendorIndirect only if the mshsnapin is a default mshsnapin
if (isDefault)
{
_descriptionIndirect = descriptionIndirect;
_vendorIndirect = vendorIndirect;
}
}
/// <summary>
/// Unique Name of the mshsnapin.
/// </summary>
public string Name { get; }
/// <summary>
/// Is this mshsnapin default mshsnapin.
/// </summary>
public bool IsDefault { get; }
/// <summary>
/// Returns applicationbase for mshsnapin.
/// </summary>
public string ApplicationBase { get; }
/// <summary>
/// Strong name of mshSnapIn assembly.
/// </summary>
public string AssemblyName { get; }
/// <summary>
/// Name of PSSnapIn module.
/// </summary>
public string ModuleName { get; }
internal string AbsoluteModulePath
{
get
{
if (string.IsNullOrEmpty(ModuleName) || Path.IsPathRooted(ModuleName))
{
return ModuleName;
}
else if (!File.Exists(Path.Combine(ApplicationBase, ModuleName)))
{
return Path.GetFileNameWithoutExtension(ModuleName);
}
return Path.Combine(ApplicationBase, ModuleName);
}
}
/// <summary>
/// Monad version used by mshsnapin.
/// </summary>
public Version PSVersion { get; }
/// <summary>
/// Version of mshsnapin.
/// </summary>
public Version Version { get; }
/// <summary>
/// Collection of file names containing types information for PSSnapIn.
/// </summary>
public Collection<string> Types { get; }
/// <summary>
/// Collection of file names containing format information for PSSnapIn.
/// </summary>
public Collection<string> Formats { get; }
private string _descriptionIndirect;
private string _descriptionFallback = string.Empty;
private string _description;
/// <summary>
/// Description of mshsnapin.
/// </summary>
public string Description
{
get
{
if (_description == null)
{
LoadIndirectResources();
}
return _description;
}
}
private string _vendorIndirect;
private string _vendorFallback = string.Empty;
private string _vendor;
/// <summary>
/// Vendor of mshsnapin.
/// </summary>
public string Vendor
{
get
{
if (_vendor == null)
{
LoadIndirectResources();
}
return _vendor;
}
}
/// <summary>
/// Get/set whether to log Pipeline Execution Detail events.
/// </summary>
public bool LogPipelineExecutionDetails { get; set; } = false;
/// <summary>
/// Overrides ToString.
/// </summary>
/// <returns>
/// Name of the PSSnapIn
/// </returns>
public override string ToString()
{
return Name;
}
internal RegistryKey MshSnapinKey
{
get
{
RegistryKey mshsnapinKey = null;
try
{
mshsnapinKey = PSSnapInReader.GetMshSnapinKey(Name, PSVersion.Major.ToString(CultureInfo.InvariantCulture));
}
catch (ArgumentException)
{
}
catch (SecurityException)
{
}
catch (System.IO.IOException)
{
}
return mshsnapinKey;
}
}
internal void LoadIndirectResources()
{
using (RegistryStringResourceIndirect resourceReader = RegistryStringResourceIndirect.GetResourceIndirectReader())
{
LoadIndirectResources(resourceReader);
}
}
internal void LoadIndirectResources(RegistryStringResourceIndirect resourceReader)
{
if (IsDefault)
{
// For default mshsnapins..resource indirects are hardcoded..
// so dont read from the registry
_description = resourceReader.GetResourceStringIndirect(
AssemblyName,
ModuleName,
_descriptionIndirect);
_vendor = resourceReader.GetResourceStringIndirect(
AssemblyName,
ModuleName,
_vendorIndirect);
}
else
{
RegistryKey mshsnapinKey = MshSnapinKey;
if (mshsnapinKey != null)
{
_description =
resourceReader.GetResourceStringIndirect(
mshsnapinKey,
RegistryStrings.MshSnapin_DescriptionResource,
AssemblyName,
ModuleName);
_vendor =
resourceReader.GetResourceStringIndirect(
mshsnapinKey,
RegistryStrings.MshSnapin_VendorResource,
AssemblyName,
ModuleName);
}
}
if (string.IsNullOrEmpty(_description))
{
_description = _descriptionFallback;
}
if (string.IsNullOrEmpty(_vendor))
{
_vendor = _vendorFallback;
}
}
internal PSSnapInInfo Clone()
{
PSSnapInInfo cloned = new PSSnapInInfo(
Name,
IsDefault,
ApplicationBase,
AssemblyName,
ModuleName,
PSVersion,
Version,
new Collection<string>(Types),
new Collection<string>(Formats),
_description,
_descriptionFallback,
_descriptionIndirect,
_vendor,
_vendorFallback,
_vendorIndirect);
return cloned;
}
/// <summary>
/// Returns true if the PSSnapIn Id is valid. A PSSnapIn is valid iff it contains only
/// "Alpha Numeric","-","_","." characters.
/// </summary>
/// <param name="psSnapinId">PSSnapIn Id to validate.</param>
internal static bool IsPSSnapinIdValid(string psSnapinId)
{
if (string.IsNullOrEmpty(psSnapinId))
{
return false;
}
return Regex.IsMatch(psSnapinId, "^[A-Za-z0-9-_\x2E]*$");
}
/// <summary>
/// Validates the PSSnapIn Id. A PSSnapIn is valid iff it contains only
/// "Alpha Numeric","-","_","." characters.
/// </summary>
/// <param name="psSnapinId">PSSnapIn Id to validate.</param>
/// <exception cref="PSArgumentException">
/// 1. Specified PSSnapIn is not valid
/// </exception>
internal static void VerifyPSSnapInFormatThrowIfError(string psSnapinId)
{
// PSSnapIn do not conform to the naming convention..so throw
// argument exception
if (!IsPSSnapinIdValid(psSnapinId))
{
throw PSTraceSource.NewArgumentException(nameof(psSnapinId),
MshSnapInCmdletResources.InvalidPSSnapInName,
psSnapinId);
}
// Valid SnapId..Just return
return;
}
}
/// <summary>
/// Internal class to read information about a mshsnapin.
/// </summary>
internal static class PSSnapInReader
{
/// <summary>
/// Reads all registered mshsnapin for all monad versions.
/// </summary>
/// <returns>
/// A collection of PSSnapInInfo objects
/// </returns>
/// <exception cref="SecurityException">
/// User doesn't have access to monad/mshsnapin registration information
/// </exception>
/// <exception cref="ArgumentException">
/// Monad key is not installed
/// </exception>
internal static Collection<PSSnapInInfo> ReadAll()
{
Collection<PSSnapInInfo> allMshSnapins = new Collection<PSSnapInInfo>();
RegistryKey monadRootKey = GetMonadRootKey();
string[] versions = monadRootKey.GetSubKeyNames();
if (versions == null)
{
return allMshSnapins;
}
// PS V3 snapin information is stored under 1 registry key..
// so no need to iterate over twice.
Collection<string> filteredVersions = new Collection<string>();
foreach (string version in versions)
{
string temp = PSVersionInfo.GetRegistryVersionKeyForSnapinDiscovery(version);
if (string.IsNullOrEmpty(temp))
{
temp = version;
}
if (!filteredVersions.Contains(temp))
{
filteredVersions.Add(temp);
}
}
foreach (string version in filteredVersions)
{
if (string.IsNullOrEmpty(version))
{
continue;
}
// found a key which is not version
if (!MeetsVersionFormat(version))
{
continue;
}
Collection<PSSnapInInfo> oneVersionMshSnapins = null;
try
{
oneVersionMshSnapins = ReadAll(monadRootKey, version);
}
// If we cannot get information for one version, continue with other
// versions
catch (SecurityException)
{
}
catch (ArgumentException)
{
}
if (oneVersionMshSnapins != null)
{
foreach (PSSnapInInfo info in oneVersionMshSnapins)
{
allMshSnapins.Add(info);
}
}
}
return allMshSnapins;
}
/// <summary>
/// Version should be integer (1, 2, 3 etc)
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
private
static
bool MeetsVersionFormat(string version)
{
bool r = true;
try
{
LanguagePrimitives.ConvertTo(version, typeof(int), CultureInfo.InvariantCulture);
}
catch (PSInvalidCastException)
{
r = false;
}
return r;
}
/// <summary>
/// Reads all registered mshsnapin for specified psVersion.
/// </summary>
/// <returns>
/// A collection of PSSnapInInfo objects
/// </returns>
/// <exception cref="SecurityException">
/// User doesn't have permission to read MonadRoot or Version
/// </exception>
/// <exception cref="ArgumentException">
/// MonadRoot or Version key doesn't exist.
/// </exception>
internal static Collection<PSSnapInInfo> ReadAll(string psVersion)
{
if (string.IsNullOrEmpty(psVersion))
{
throw PSTraceSource.NewArgumentNullException("psVersion");
}
RegistryKey monadRootKey = GetMonadRootKey();
return ReadAll(monadRootKey, psVersion);
}
/// <summary>
/// Reads all the mshsnapins for a given psVersion.
/// </summary>
/// <exception cref="SecurityException">
/// The User doesn't have required permission to read the registry key for this version.
/// </exception>
/// <exception cref="ArgumentException">
/// Specified version doesn't exist.
/// </exception>
/// <exception cref="SecurityException">
/// User doesn't have permission to read specified version
/// </exception>
private static Collection<PSSnapInInfo> ReadAll(RegistryKey monadRootKey, string psVersion)
{
Dbg.Assert(monadRootKey != null, "caller should validate the information");
Dbg.Assert(!string.IsNullOrEmpty(psVersion), "caller should validate the information");
Collection<PSSnapInInfo> mshsnapins = new Collection<PSSnapInInfo>();
RegistryKey versionRoot = GetVersionRootKey(monadRootKey, psVersion);
RegistryKey mshsnapinRoot = GetMshSnapinRootKey(versionRoot, psVersion);
// get name of all mshsnapin for this version
string[] mshsnapinIds = mshsnapinRoot.GetSubKeyNames();
foreach (string id in mshsnapinIds)
{
if (string.IsNullOrEmpty(id))
{
continue;
}
try
{
mshsnapins.Add(ReadOne(mshsnapinRoot, id));
}
// If we cannot read some mshsnapins, we should continue
catch (SecurityException)
{
}
catch (ArgumentException)
{
}
}
return mshsnapins;
}
/// <summary>
/// Read mshsnapin for specified mshsnapinId and psVersion.
/// </summary>
/// <returns>
/// MshSnapin info object
/// </returns>
/// <exception cref="SecurityException">
/// The user does not have the permissions required to read the
/// registry key for one of the following:
/// 1) Monad
/// 2) PSVersion
/// 3) MshSnapinId
/// </exception>
/// <exception cref="ArgumentException">
/// 1) Monad key is not present
/// 2) VersionKey is not present
/// 3) MshSnapin key is not present
/// 4) MshSnapin key is not valid
/// </exception>
internal static PSSnapInInfo Read(string psVersion, string mshsnapinId)
{
if (string.IsNullOrEmpty(psVersion))
{
throw PSTraceSource.NewArgumentNullException("psVersion");
}
if (string.IsNullOrEmpty(mshsnapinId))
{
throw PSTraceSource.NewArgumentNullException("mshsnapinId");
}
// PSSnapIn Reader wont service invalid mshsnapins
// Monad has specific restrictions on the mshsnapinid like
// mshsnapinid should be A-Za-z0-9.-_ etc.
PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(mshsnapinId);
RegistryKey rootKey = GetMonadRootKey();
RegistryKey versionRoot = GetVersionRootKey(rootKey, psVersion);
RegistryKey mshsnapinRoot = GetMshSnapinRootKey(versionRoot, psVersion);
return ReadOne(mshsnapinRoot, mshsnapinId);
}
/// <summary>
/// Reads the mshsnapin info for a specific key under specific monad version.
/// </summary>
/// <remarks>
/// ReadOne will never create a default PSSnapInInfo object.
/// </remarks>
/// <exception cref="SecurityException">
/// The user does not have the permissions required to read the
/// registry key for specified mshsnapin.
/// </exception>
/// <exception cref="ArgumentException">
/// 1) Specified mshsnapin is not installed.
/// 2) Specified mshsnapin is not correctly installed.
/// </exception>
private static PSSnapInInfo ReadOne(RegistryKey mshSnapInRoot, string mshsnapinId)
{
Dbg.Assert(!string.IsNullOrEmpty(mshsnapinId), "caller should validate the parameter");
Dbg.Assert(mshSnapInRoot != null, "caller should validate the parameter");
RegistryKey mshsnapinKey;
mshsnapinKey = mshSnapInRoot.OpenSubKey(mshsnapinId);
if (mshsnapinKey == null)
{
s_mshsnapinTracer.TraceError("Error opening registry key {0}\\{1}.", mshSnapInRoot.Name, mshsnapinId);
throw PSTraceSource.NewArgumentException("mshsnapinId", MshSnapinInfo.MshSnapinDoesNotExist, mshsnapinId);
}
string applicationBase = ReadStringValue(mshsnapinKey, RegistryStrings.MshSnapin_ApplicationBase, true);
string assemblyName = ReadStringValue(mshsnapinKey, RegistryStrings.MshSnapin_AssemblyName, true);
string moduleName = ReadStringValue(mshsnapinKey, RegistryStrings.MshSnapin_ModuleName, true);
Version monadVersion = ReadVersionValue(mshsnapinKey, RegistryStrings.MshSnapin_MonadVersion, true);
Version version = ReadVersionValue(mshsnapinKey, RegistryStrings.MshSnapin_Version, false);
string description = ReadStringValue(mshsnapinKey, RegistryStrings.MshSnapin_Description, false);
if (description == null)
{
s_mshsnapinTracer.WriteLine("No description is specified for mshsnapin {0}. Using empty string for description.", mshsnapinId);
description = string.Empty;
}
string vendor = ReadStringValue(mshsnapinKey, RegistryStrings.MshSnapin_Vendor, false);
if (vendor == null)
{
s_mshsnapinTracer.WriteLine("No vendor is specified for mshsnapin {0}. Using empty string for description.", mshsnapinId);
vendor = string.Empty;
}
bool logPipelineExecutionDetails = false;
string logPipelineExecutionDetailsStr = ReadStringValue(mshsnapinKey, RegistryStrings.MshSnapin_LogPipelineExecutionDetails, false);
if (!string.IsNullOrEmpty(logPipelineExecutionDetailsStr))
{
if (string.Compare("1", logPipelineExecutionDetailsStr, StringComparison.OrdinalIgnoreCase) == 0)
logPipelineExecutionDetails = true;
}
Collection<string> types = ReadMultiStringValue(mshsnapinKey, RegistryStrings.MshSnapin_BuiltInTypes, false);
Collection<string> formats = ReadMultiStringValue(mshsnapinKey, RegistryStrings.MshSnapin_BuiltInFormats, false);
s_mshsnapinTracer.WriteLine("Successfully read registry values for mshsnapin {0}. Constructing PSSnapInInfo object.", mshsnapinId);
PSSnapInInfo mshSnapinInfo = new PSSnapInInfo(mshsnapinId, false, applicationBase, assemblyName, moduleName, monadVersion, version, types, formats, description, vendor);
mshSnapinInfo.LogPipelineExecutionDetails = logPipelineExecutionDetails;
return mshSnapinInfo;
}
/// <summary>
/// Gets multistring value for name.
/// </summary>
/// <param name="mshsnapinKey"></param>
/// <param name="name"></param>
/// <param name="mandatory"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">
/// if value is not present and mandatory is true
/// </exception>
private static Collection<string> ReadMultiStringValue(RegistryKey mshsnapinKey, string name, bool mandatory)
{
object value = mshsnapinKey.GetValue(name);
if (value == null)
{
// If this key should be present..throw error
if (mandatory)
{
s_mshsnapinTracer.TraceError("Mandatory property {0} not specified for registry key {1}",
name, mshsnapinKey.Name);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.MandatoryValueNotPresent, name, mshsnapinKey.Name);
}
else
{
return null;
}
}
// value cannot be null here...
string[] msv = value as string[];
if (msv == null)
{
// Check if the value is in string format
string singleValue = value as string;
if (singleValue != null)
{
msv = new string[1];
msv[0] = singleValue;
}
}
if (msv == null)
{
if (mandatory)
{
s_mshsnapinTracer.TraceError("Cannot get string/multi-string value for mandatory property {0} in registry key {1}",
name, mshsnapinKey.Name);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.MandatoryValueNotInCorrectFormatMultiString, name, mshsnapinKey.Name);
}
else
{
return null;
}
}
s_mshsnapinTracer.WriteLine("Successfully read property {0} from {1}",
name, mshsnapinKey.Name);
return new Collection<string>(msv);
}
/// <summary>
/// Get the value for name.
/// </summary>
/// <param name="mshsnapinKey"></param>
/// <param name="name"></param>
/// <param name="mandatory"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">
/// if no value is available and mandatory is true.
/// </exception>
internal static string ReadStringValue(RegistryKey mshsnapinKey, string name, bool mandatory)
{
Dbg.Assert(!string.IsNullOrEmpty(name), "caller should validate the parameter");
Dbg.Assert(mshsnapinKey != null, "Caller should validate the parameter");
object value = mshsnapinKey.GetValue(name);
if (value == null && mandatory == true)
{
s_mshsnapinTracer.TraceError("Mandatory property {0} not specified for registry key {1}",
name, mshsnapinKey.Name);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.MandatoryValueNotPresent, name, mshsnapinKey.Name);
}
string s = value as string;
if (string.IsNullOrEmpty(s) && mandatory == true)
{
s_mshsnapinTracer.TraceError("Value is null or empty for mandatory property {0} in {1}",
name, mshsnapinKey.Name);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.MandatoryValueNotInCorrectFormat, name, mshsnapinKey.Name);
}
s_mshsnapinTracer.WriteLine("Successfully read value {0} for property {1} from {2}",
s, name, mshsnapinKey.Name);
return s;
}
internal static Version ReadVersionValue(RegistryKey mshsnapinKey, string name, bool mandatory)
{
string temp = ReadStringValue(mshsnapinKey, name, mandatory);
if (temp == null)
{
s_mshsnapinTracer.TraceError("Cannot read value for property {0} in registry key {1}",
name, mshsnapinKey.ToString());
Dbg.Assert(!mandatory, "mandatory is true, ReadStringValue should have thrown exception");
return null;
}
Version v;
try
{
v = new Version(temp);
}
catch (ArgumentOutOfRangeException)
{
s_mshsnapinTracer.TraceError("Cannot convert value {0} to version format", temp);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.VersionValueInCorrect, name, mshsnapinKey.Name);
}
catch (ArgumentException)
{
s_mshsnapinTracer.TraceError("Cannot convert value {0} to version format", temp);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.VersionValueInCorrect, name, mshsnapinKey.Name);
}
catch (OverflowException)
{
s_mshsnapinTracer.TraceError("Cannot convert value {0} to version format", temp);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.VersionValueInCorrect, name, mshsnapinKey.Name);
}
catch (FormatException)
{
s_mshsnapinTracer.TraceError("Cannot convert value {0} to version format", temp);
throw PSTraceSource.NewArgumentException("name", MshSnapinInfo.VersionValueInCorrect, name, mshsnapinKey.Name);
}
s_mshsnapinTracer.WriteLine("Successfully converted string {0} to version format.", v);
return v;
}
internal static void ReadRegistryInfo(out Version assemblyVersion, out string publicKeyToken, out string culture, out string architecture, out string applicationBase, out Version psVersion)
{
applicationBase = Utils.DefaultPowerShellAppBase;
Dbg.Assert(
!string.IsNullOrEmpty(applicationBase),
string.Format(CultureInfo.CurrentCulture, "{0} is empty or null", RegistryStrings.MonadEngine_ApplicationBase));
// Get the PSVersion from Utils..this is hardcoded
psVersion = PSVersionInfo.PSVersion;
Dbg.Assert(
psVersion != null,
string.Format(CultureInfo.CurrentCulture, "{0} is null", RegistryStrings.MonadEngine_MonadVersion));
// Get version number in x.x.x.x format
// This information is available from the executing assembly
//
// PROBLEM: The following code assumes all assemblies have the same version,
// culture, publickeytoken...This will break the scenarios where only one of
// the assemblies is patched. ie., all monad assemblies should have the
// same version number.
Assembly currentAssembly = typeof(PSSnapInReader).Assembly;
AssemblyName assemblyName = currentAssembly.GetName();
assemblyVersion = assemblyName.Version;
byte[] publicTokens = assemblyName.GetPublicKeyToken();
if (publicTokens.Length == 0)
{
throw PSTraceSource.NewArgumentException("PublicKeyToken", MshSnapinInfo.PublicKeyTokenAccessFailed);
}
publicKeyToken = ConvertByteArrayToString(publicTokens);
// save some cpu cycles by hardcoding the culture to neutral
// assembly should never be targeted to a particular culture
culture = "neutral";
// Hardcoding the architecture MSIL as PowerShell assemblies are architecture neutral, this should
// be changed if the assumption is broken. Preferred hardcoded string to using (for perf reasons):
// string architecture = currentAssembly.GetName().ProcessorArchitecture.ToString()
architecture = "MSIL";
}
/// <summary>
/// PublicKeyToken is in the form of byte[]. Use this function to convert to a string.
/// </summary>
/// <param name="tokens">Array of byte's.</param>
/// <returns></returns>
internal static string ConvertByteArrayToString(byte[] tokens)
{
Dbg.Assert(tokens != null, "Input tokens should never be null");
StringBuilder tokenBuilder = new StringBuilder(tokens.Length * 2);
foreach (byte b in tokens)
{
tokenBuilder.Append(b.ToString("x2", CultureInfo.InvariantCulture));
}
return tokenBuilder.ToString();
}
/// <summary>
/// Reads core snapin for monad engine.
/// </summary>
/// <returns>
/// A PSSnapInInfo object
/// </returns>
internal static PSSnapInInfo ReadCoreEngineSnapIn()
{
Version assemblyVersion, psVersion;
string publicKeyToken = null;
string culture = null;
string architecture = null;
string applicationBase = null;
ReadRegistryInfo(out assemblyVersion, out publicKeyToken, out culture, out architecture, out applicationBase, out psVersion);
// System.Management.Automation formats & types files
Collection<string> types = new Collection<string>(new string[] { "types.ps1xml", "typesv3.ps1xml" });
Collection<string> formats = new Collection<string>(new string[]
{"Certificate.format.ps1xml","DotNetTypes.format.ps1xml","FileSystem.format.ps1xml",
"Help.format.ps1xml","HelpV3.format.ps1xml","PowerShellCore.format.ps1xml","PowerShellTrace.format.ps1xml",
"Registry.format.ps1xml"});
string strongName = string.Format(CultureInfo.InvariantCulture, "{0}, Version={1}, Culture={2}, PublicKeyToken={3}, ProcessorArchitecture={4}",
s_coreSnapin.AssemblyName, assemblyVersion, culture, publicKeyToken, architecture);
string moduleName = Path.Combine(applicationBase, s_coreSnapin.AssemblyName + ".dll");
PSSnapInInfo coreMshSnapin = new PSSnapInInfo(
s_coreSnapin.PSSnapInName,
isDefault: true,
applicationBase,
strongName,
moduleName,
psVersion,
assemblyVersion,
types,
formats,
description: null,
s_coreSnapin.Description,
s_coreSnapin.DescriptionIndirect,
vendor: null,
vendorFallback: null,
s_coreSnapin.VendorIndirect);
#if !UNIX
// NOTE: On Unix, logging has to be deferred until after command-line parsing
// complete. On Windows, deferring the call is not needed
// and this is in the startup code path.
SetSnapInLoggingInformation(coreMshSnapin);
#endif
return coreMshSnapin;
}
/// <summary>
/// Reads all registered mshsnapins for currently executing monad engine.
/// </summary>
/// <returns>
/// A collection of PSSnapInInfo objects
/// </returns>
internal static Collection<PSSnapInInfo> ReadEnginePSSnapIns()
{
Version assemblyVersion, psVersion;