forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMshInstaller.cs
More file actions
220 lines (193 loc) · 6.55 KB
/
Copy pathMshInstaller.cs
File metadata and controls
220 lines (193 loc) · 6.55 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration.Install;
using System.Reflection;
using Microsoft.Win32;
using System.IO;
using System.Text;
using System.Threading;
using System.Globalization;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation
{
/// <summary>
///
/// PSInstaller is a class for facilitating installation
/// of monad engine and monad PSSnapin's.
///
/// This class implements installer api from CLR. At install
/// time, installation utilities (like InstallUtil.exe) will
/// call api implementation functions in this class automatically.
/// This includes functions like Install, Uninstall, Rollback
/// and Commit.
///
/// This class is an abstract class for handling installation needs
/// that are common for all monad components, which include,
///
/// 1. accessing system registry
/// 2. support of additional command line parameters.
/// 3. writing registry files
/// 4. automatically extract information like vender, version, etc.
///
/// Different monad component will derive from this class. Two common
/// components that need install include,
///
/// 1. PSSnapin. Installation of PSSnapin will require information
/// about PSSnapin assembly, version, vendor, etc to be
/// written to registry.
///
/// 2. Engine. Installation of monad engine will require information
/// about engine assembly, version, CLR information to be
/// written to registry.
///
/// </summary>
///
/// <remarks>
/// This is an abstract class to be derived by monad engine and PSSnapin installers
/// only. Developer should not directly derive from this class.
/// </remarks>
public abstract class PSInstaller : Installer
{
private static string[] MshRegistryRoots
{
get
{
// For 3.0 PowerShell, we use "3" as the registry version key only for Engine
// related data like ApplicationBase.
// For 3.0 PowerShell, we still use "1" as the registry version key for
// Snapin and Custom shell lookup/discovery.
return new string[] {
"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\PowerShell\\" + PSVersionInfo.RegistryVersion1Key + "\\"
};
}
}
/// <summary>
///
/// </summary>
internal abstract string RegKey
{
get;
}
/// <summary>
///
/// </summary>
internal abstract Dictionary<String, object> RegValues
{
get;
}
/// <summary>
///
/// </summary>
/// <param name="stateSaver"></param>
public sealed override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
WriteRegistry();
return;
}
private void WriteRegistry()
{
foreach (string root in MshRegistryRoots)
{
RegistryKey key = GetRegistryKey(root + RegKey);
foreach (var pair in RegValues)
{
key.SetValue(pair.Key, pair.Value);
}
}
}
private RegistryKey GetRegistryKey(string keyPath)
{
RegistryKey root = GetRootHive(keyPath);
if (root == null)
return null;
return root.CreateSubKey(GetSubkeyPath(keyPath));
}
private static string GetSubkeyPath(string keyPath)
{
int index = keyPath.IndexOf('\\');
if (index > 0)
{
return keyPath.Substring(index + 1);
}
return null;
}
private static RegistryKey GetRootHive(string keyPath)
{
string root;
int index = keyPath.IndexOf('\\');
if (index > 0)
{
root = keyPath.Substring(0, index);
}
else
{
root = keyPath;
}
switch (root.ToUpperInvariant())
{
case "HKEY_CURRENT_USER":
return Registry.CurrentUser;
case "HKEY_LOCAL_MACHINE":
return Registry.LocalMachine;
case "HKEY_CLASSES_ROOT":
return Registry.ClassesRoot;
case "HKEY_CURRENT_CONFIG":
return Registry.CurrentConfig;
case "HKEY_PERFORMANCE_DATA":
return Registry.PerformanceData;
case "HKEY_USERS":
return Registry.Users;
}
return null;
}
/// <summary>
/// Uninstall this msh component
/// </summary>
/// <param name="savedState"></param>
public sealed override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
if (this.Context != null && this.Context.Parameters != null && this.Context.Parameters.ContainsKey("RegFile"))
{
string regFile = this.Context.Parameters["RegFile"];
// If regfile is specified. Don't uninstall.
if (!String.IsNullOrEmpty(regFile))
return;
}
string keyName;
string parentKey;
int index = RegKey.LastIndexOf('\\');
if (index >= 0)
{
parentKey = RegKey.Substring(0, index);
keyName = RegKey.Substring(index + 1);
}
else
{
parentKey = "";
keyName = RegKey;
}
foreach (string root in MshRegistryRoots)
{
RegistryKey key = GetRegistryKey(root + parentKey);
key.DeleteSubKey(keyName);
}
return;
}
/// <summary>
/// Rollback this msh component
/// </summary>
/// <param name="savedState"></param>
public sealed override void Rollback(IDictionary savedState)
{
Uninstall(savedState);
}
}
}