private void MergeCustomPSSnapIn(PSSnapInInfo mshsnapinInfo, CustomPSSnapIn customPSSnapIn) { if (mshsnapinInfo == null || customPSSnapIn == null) { return; } s_mshsnapinTracer.WriteLine("Merging configuration from custom mshsnapin {0}", mshsnapinInfo.Name); if (customPSSnapIn.Cmdlets != null) { foreach (CmdletConfigurationEntry entry in customPSSnapIn.Cmdlets) { CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); _cmdlets.AddBuiltInItem(cmdlet); } } if (customPSSnapIn.Providers != null) { foreach (ProviderConfigurationEntry entry in customPSSnapIn.Providers) { ProviderConfigurationEntry provider = new ProviderConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); _providers.AddBuiltInItem(provider); } } if (customPSSnapIn.Types != null) { foreach (TypeConfigurationEntry entry in customPSSnapIn.Types) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, entry.FileName); TypeConfigurationEntry type = new TypeConfigurationEntry(entry.Name, path, mshsnapinInfo); _types.AddBuiltInItem(type); } } if (customPSSnapIn.Formats != null) { foreach (FormatConfigurationEntry entry in customPSSnapIn.Formats) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, entry.FileName); FormatConfigurationEntry format = new FormatConfigurationEntry(entry.Name, path, mshsnapinInfo); _formats.AddBuiltInItem(format); } } AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(assemblyEntry); s_mshsnapinTracer.WriteLine("Configuration from custom mshsnapin {0} merged", mshsnapinInfo.Name); }
private void MergeCustomPSSnapIn(PSSnapInInfo mshsnapinInfo, CustomPSSnapIn customPSSnapIn) { if ((mshsnapinInfo != null) && (customPSSnapIn != null)) { _mshsnapinTracer.WriteLine("Merging configuration from custom mshsnapin {0}", new object[] { mshsnapinInfo.Name }); if (customPSSnapIn.Cmdlets != null) { foreach (CmdletConfigurationEntry entry in customPSSnapIn.Cmdlets) { CmdletConfigurationEntry entry2 = new CmdletConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); this._cmdlets.AddBuiltInItem(entry2); } } if (customPSSnapIn.Providers != null) { foreach (ProviderConfigurationEntry entry3 in customPSSnapIn.Providers) { ProviderConfigurationEntry entry4 = new ProviderConfigurationEntry(entry3.Name, entry3.ImplementingType, entry3.HelpFileName, mshsnapinInfo); this._providers.AddBuiltInItem(entry4); } } if (customPSSnapIn.Types != null) { foreach (TypeConfigurationEntry entry5 in customPSSnapIn.Types) { string fileName = Path.Combine(mshsnapinInfo.ApplicationBase, entry5.FileName); TypeConfigurationEntry entry6 = new TypeConfigurationEntry(entry5.Name, fileName, mshsnapinInfo); this._types.AddBuiltInItem(entry6); } } if (customPSSnapIn.Formats != null) { foreach (FormatConfigurationEntry entry7 in customPSSnapIn.Formats) { string str2 = Path.Combine(mshsnapinInfo.ApplicationBase, entry7.FileName); FormatConfigurationEntry entry8 = new FormatConfigurationEntry(entry7.Name, str2, mshsnapinInfo); this._formats.AddBuiltInItem(entry8); } } AssemblyConfigurationEntry item = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(item); _mshsnapinTracer.WriteLine("Configuration from custom mshsnapin {0} merged", new object[] { mshsnapinInfo.Name }); } }
private void MergeCustomPSSnapIn(PSSnapInInfo mshsnapinInfo, CustomPSSnapIn customPSSnapIn) { if (mshsnapinInfo == null || customPSSnapIn == null) return; s_mshsnapinTracer.WriteLine("Merging configuration from custom mshsnapin {0}", mshsnapinInfo.Name); if (customPSSnapIn.Cmdlets != null) { foreach (CmdletConfigurationEntry entry in customPSSnapIn.Cmdlets) { CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); _cmdlets.AddBuiltInItem(cmdlet); } } if (customPSSnapIn.Providers != null) { foreach (ProviderConfigurationEntry entry in customPSSnapIn.Providers) { ProviderConfigurationEntry provider = new ProviderConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); _providers.AddBuiltInItem(provider); } } if (customPSSnapIn.Types != null) { foreach (TypeConfigurationEntry entry in customPSSnapIn.Types) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, entry.FileName); TypeConfigurationEntry type = new TypeConfigurationEntry(entry.Name, path, mshsnapinInfo); _types.AddBuiltInItem(type); } } if (customPSSnapIn.Formats != null) { foreach (FormatConfigurationEntry entry in customPSSnapIn.Formats) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, entry.FileName); FormatConfigurationEntry format = new FormatConfigurationEntry(entry.Name, path, mshsnapinInfo); _formats.AddBuiltInItem(format); } } AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(assemblyEntry); s_mshsnapinTracer.WriteLine("Configuration from custom mshsnapin {0} merged", mshsnapinInfo.Name); }
private void LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) { if (mshsnapinInfo == null) return; #if !CORECLR // CustomPSSnapIn Not Supported On CSS. if (!String.IsNullOrEmpty(mshsnapinInfo.CustomPSSnapInType)) { LoadCustomPSSnapIn(mshsnapinInfo); return; } #endif Assembly assembly = null; s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0}", mshsnapinInfo.Name); assembly = LoadMshSnapinAssembly(mshsnapinInfo); if (assembly == null) { s_mshsnapinTracer.TraceError("Loading assembly for mshsnapin {0} failed", mshsnapinInfo.Name); return; } s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0} succeeded", mshsnapinInfo.Name); Dictionary<string, SessionStateCmdletEntry> cmdlets = null; Dictionary<string, List<SessionStateAliasEntry>> aliases = null; Dictionary<string, SessionStateProviderEntry> providers = null; string throwAwayHelpFile = null; PSSnapInHelpers.AnalyzePSSnapInAssembly(assembly, assembly.Location, mshsnapinInfo, null, false, out cmdlets, out aliases, out providers, out throwAwayHelpFile); if (cmdlets != null) { foreach (var c in cmdlets) { CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(c.Key, c.Value.ImplementingType, c.Value.HelpFileName, mshsnapinInfo); _cmdlets.AddBuiltInItem(cmdlet); } } if (providers != null) { foreach (var p in providers) { ProviderConfigurationEntry provider = new ProviderConfigurationEntry(p.Key, p.Value.ImplementingType, p.Value.HelpFileName, mshsnapinInfo); _providers.AddBuiltInItem(provider); } } foreach (string file in mshsnapinInfo.Types) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); TypeConfigurationEntry typeEntry = new TypeConfigurationEntry(path, path, mshsnapinInfo); this.Types.AddBuiltInItem(typeEntry); } foreach (string file in mshsnapinInfo.Formats) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); FormatConfigurationEntry formatEntry = new FormatConfigurationEntry(path, path, mshsnapinInfo); this.Formats.AddBuiltInItem(formatEntry); } AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(assemblyEntry); return; }
private string GetProviderName(ProviderConfigurationEntry entry) { string name = entry.Name; if (entry.PSSnapIn != null) { name = string.Format( System.Globalization.CultureInfo.InvariantCulture, "{0}\\{1}", entry.PSSnapIn.Name, entry.Name); } return name; }
private void RemoveProvider(ProviderConfigurationEntry entry) { try { CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); string providerName = GetProviderName(entry); RemoveProvider(providerName, true, context); context.ThrowFirstErrorOrDoNothing(); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout { CommandProcessorBase.CheckForSevereException(e); // NTRAID#Windows OS Bugs-1009281-2004/02/11-JeffJon this.ExecutionContext.ReportEngineStartupError(e); } }
/// <summary> /// Internal method used by RunspaceConfig for updating providers. /// </summary> /// <param name="providerConfig"></param> private ProviderInfo AddProvider(ProviderConfigurationEntry providerConfig) { return AddProvider(providerConfig.ImplementingType, providerConfig.Name, providerConfig.HelpFileName, providerConfig.PSSnapIn, null ); }
private void AnalyzeMshSnapinAssembly(Assembly assembly, PSSnapInInfo mshsnapinInfo) { if (assembly != null) { _mshsnapinTracer.WriteLine("Analyzing assembly {0} for cmdlet and providers", new object[] { assembly.Location }); string helpFile = GetHelpFile(assembly.Location); Type[] exportedTypes = null; try { exportedTypes = assembly.GetExportedTypes(); } catch (ReflectionTypeLoadException exception) { string errorMessageFormat = exception.Message + "\nLoader Exceptions: \n"; if (exception.LoaderExceptions != null) { foreach (Exception exception2 in exception.LoaderExceptions) { errorMessageFormat = errorMessageFormat + "\n" + exception2.Message; } } _mshsnapinTracer.TraceError(errorMessageFormat, new object[0]); throw new PSSnapInException(mshsnapinInfo.Name, errorMessageFormat); } Hashtable hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase); Hashtable hashtable2 = new Hashtable(StringComparer.OrdinalIgnoreCase); foreach (Type type in exportedTypes) { string cmdletName = null; string providerName = null; object[] customAttributes = type.GetCustomAttributes(typeof(CmdletAttribute), false); if (customAttributes.Length > 0) { cmdletName = GetCmdletName(customAttributes[0] as CmdletAttribute); } else { customAttributes = type.GetCustomAttributes(typeof(CmdletProviderAttribute), false); if (customAttributes.Length > 0) { providerName = GetProviderName(customAttributes[0] as CmdletProviderAttribute); } } if (!string.IsNullOrEmpty(cmdletName)) { if (IsCmdletClass(type) && HasDefaultConstructor(type)) { if (hashtable.ContainsKey(cmdletName)) { string str5 = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInDuplicateCmdlets, cmdletName, mshsnapinInfo.Name); _mshsnapinTracer.TraceError(str5, new object[0]); throw new PSSnapInException(mshsnapinInfo.Name, str5); } hashtable.Add(cmdletName, null); CmdletConfigurationEntry item = new CmdletConfigurationEntry(cmdletName, type, helpFile, mshsnapinInfo); this._cmdlets.AddBuiltInItem(item); _mshsnapinTracer.WriteLine("{0} from type {1} is added as a cmdlet. ", new object[] { cmdletName, type.FullName }); continue; } _mshsnapinTracer.TraceWarning("{0} is not valid cmdlet because it doesn't derive from the Cmdlet type or it doesn't have a default constructor.", new object[] { cmdletName }); } if (!string.IsNullOrEmpty(providerName)) { if (IsProviderClass(type) && HasDefaultConstructor(type)) { if (hashtable2.ContainsKey(providerName)) { string str6 = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInDuplicateProviders, providerName, mshsnapinInfo.Name); _mshsnapinTracer.TraceError(str6, new object[0]); throw new PSSnapInException(mshsnapinInfo.Name, str6); } hashtable2.Add(providerName, null); ProviderConfigurationEntry entry2 = new ProviderConfigurationEntry(providerName, type, helpFile, mshsnapinInfo); this._providers.AddBuiltInItem(entry2); _mshsnapinTracer.WriteLine("{0} from type {1} is added as a provider. ", new object[] { providerName, type.FullName }); continue; } _mshsnapinTracer.TraceWarning("{0} is not valid provider because it doesn't derive from the provider type or it doesn't have a default constructor.", new object[] { providerName }); } if (typeof(IModuleAssemblyInitializer).IsAssignableFrom(type) && !type.Equals(typeof(IModuleAssemblyInitializer))) { _mshsnapinTracer.TraceWarning("Calling module initializer defined by type {0}", new object[] { type.FullName }); (Activator.CreateInstance(type, true) as IModuleAssemblyInitializer).OnImport(); } } } }
private void RemoveProvider(ProviderConfigurationEntry entry) { try { CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); string providerName = this.GetProviderName(entry); this.RemoveProvider(providerName, true, context); context.ThrowFirstErrorOrDoNothing(); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); this.ExecutionContext.ReportEngineStartupError(exception); } }
private string GetProviderName(ProviderConfigurationEntry entry) { string name = entry.Name; if (entry.PSSnapIn != null) { name = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", new object[] { entry.PSSnapIn.Name, entry.Name }); } return name; }
private void MergeCustomPSSnapIn(PSSnapInInfo mshsnapinInfo, CustomPSSnapIn customPSSnapIn) { if ((mshsnapinInfo != null) && (customPSSnapIn != null)) { _mshsnapinTracer.WriteLine("Merging configuration from custom mshsnapin {0}", new object[] { mshsnapinInfo.Name }); if (customPSSnapIn.Cmdlets != null) { foreach (CmdletConfigurationEntry entry in customPSSnapIn.Cmdlets) { CmdletConfigurationEntry entry2 = new CmdletConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); this._cmdlets.AddBuiltInItem(entry2); } } if (customPSSnapIn.Providers != null) { foreach (ProviderConfigurationEntry entry3 in customPSSnapIn.Providers) { ProviderConfigurationEntry entry4 = new ProviderConfigurationEntry(entry3.Name, entry3.ImplementingType, entry3.HelpFileName, mshsnapinInfo); this._providers.AddBuiltInItem(entry4); } } if (customPSSnapIn.Types != null) { foreach (TypeConfigurationEntry entry5 in customPSSnapIn.Types) { string fileName = Path.Combine(mshsnapinInfo.ApplicationBase, entry5.FileName); TypeConfigurationEntry entry6 = new TypeConfigurationEntry(entry5.Name, fileName, mshsnapinInfo); this._types.AddBuiltInItem(entry6); } } if (customPSSnapIn.Formats != null) { foreach (FormatConfigurationEntry entry7 in customPSSnapIn.Formats) { string str2 = Path.Combine(mshsnapinInfo.ApplicationBase, entry7.FileName); FormatConfigurationEntry entry8 = new FormatConfigurationEntry(entry7.Name, str2, mshsnapinInfo); this._formats.AddBuiltInItem(entry8); } } AssemblyConfigurationEntry item = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(item); _mshsnapinTracer.WriteLine("Configuration from custom mshsnapin {0} merged", new object[] { mshsnapinInfo.Name }); } }
private void AnalyzeMshSnapinAssembly(Assembly assembly, PSSnapInInfo mshsnapinInfo) { if (assembly != null) { _mshsnapinTracer.WriteLine("Analyzing assembly {0} for cmdlet and providers", new object[] { assembly.Location }); string helpFile = GetHelpFile(assembly.Location); Type[] exportedTypes = null; try { exportedTypes = assembly.GetExportedTypes(); } catch (ReflectionTypeLoadException exception) { string errorMessageFormat = exception.Message + "\nLoader Exceptions: \n"; if (exception.LoaderExceptions != null) { foreach (Exception exception2 in exception.LoaderExceptions) { errorMessageFormat = errorMessageFormat + "\n" + exception2.Message; } } _mshsnapinTracer.TraceError(errorMessageFormat, new object[0]); throw new PSSnapInException(mshsnapinInfo.Name, errorMessageFormat); } Hashtable hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase); Hashtable hashtable2 = new Hashtable(StringComparer.OrdinalIgnoreCase); foreach (Type type in exportedTypes) { string cmdletName = null; string providerName = null; object[] customAttributes = type.GetCustomAttributes(typeof(CmdletAttribute), false); if (customAttributes.Length > 0) { cmdletName = GetCmdletName(customAttributes[0] as CmdletAttribute); } else { customAttributes = type.GetCustomAttributes(typeof(CmdletProviderAttribute), false); if (customAttributes.Length > 0) { providerName = GetProviderName(customAttributes[0] as CmdletProviderAttribute); } } if (!string.IsNullOrEmpty(cmdletName)) { if (IsCmdletClass(type) && HasDefaultConstructor(type)) { if (hashtable.ContainsKey(cmdletName)) { string str5 = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInDuplicateCmdlets, cmdletName, mshsnapinInfo.Name); _mshsnapinTracer.TraceError(str5, new object[0]); throw new PSSnapInException(mshsnapinInfo.Name, str5); } hashtable.Add(cmdletName, null); CmdletConfigurationEntry item = new CmdletConfigurationEntry(cmdletName, type, helpFile, mshsnapinInfo); this._cmdlets.AddBuiltInItem(item); _mshsnapinTracer.WriteLine("{0} from type {1} is added as a cmdlet. ", new object[] { cmdletName, type.FullName }); continue; } _mshsnapinTracer.TraceWarning("{0} is not valid cmdlet because it doesn't derive from the Cmdlet type or it doesn't have a default constructor.", new object[] { cmdletName }); } if (!string.IsNullOrEmpty(providerName)) { if (IsProviderClass(type) && HasDefaultConstructor(type)) { if (hashtable2.ContainsKey(providerName)) { string str6 = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInDuplicateProviders, providerName, mshsnapinInfo.Name); _mshsnapinTracer.TraceError(str6, new object[0]); throw new PSSnapInException(mshsnapinInfo.Name, str6); } hashtable2.Add(providerName, null); ProviderConfigurationEntry entry2 = new ProviderConfigurationEntry(providerName, type, helpFile, mshsnapinInfo); this._providers.AddBuiltInItem(entry2); _mshsnapinTracer.WriteLine("{0} from type {1} is added as a provider. ", new object[] { providerName, type.FullName }); continue; } _mshsnapinTracer.TraceWarning("{0} is not valid provider because it doesn't derive from the provider type or it doesn't have a default constructor.", new object[] { providerName }); } if (typeof(IModuleAssemblyInitializer).IsAssignableFrom(type) && !type.Equals(typeof(IModuleAssemblyInitializer))) { _mshsnapinTracer.TraceWarning("Calling module initializer defined by type {0}", new object[] { type.FullName }); (Activator.CreateInstance(type, true) as IModuleAssemblyInitializer).OnImport(); } } } }
private void LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) { if (mshsnapinInfo == null) { return; } #if !CORECLR // CustomPSSnapIn Not Supported On CSS. if (!String.IsNullOrEmpty(mshsnapinInfo.CustomPSSnapInType)) { LoadCustomPSSnapIn(mshsnapinInfo); return; } #endif Assembly assembly = null; s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0}", mshsnapinInfo.Name); assembly = LoadMshSnapinAssembly(mshsnapinInfo); if (assembly == null) { s_mshsnapinTracer.TraceError("Loading assembly for mshsnapin {0} failed", mshsnapinInfo.Name); return; } s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0} succeeded", mshsnapinInfo.Name); Dictionary <string, SessionStateCmdletEntry> cmdlets = null; Dictionary <string, List <SessionStateAliasEntry> > aliases = null; Dictionary <string, SessionStateProviderEntry> providers = null; string throwAwayHelpFile = null; PSSnapInHelpers.AnalyzePSSnapInAssembly(assembly, assembly.Location, mshsnapinInfo, null, false, out cmdlets, out aliases, out providers, out throwAwayHelpFile); if (cmdlets != null) { foreach (var c in cmdlets) { CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(c.Key, c.Value.ImplementingType, c.Value.HelpFileName, mshsnapinInfo); _cmdlets.AddBuiltInItem(cmdlet); } } if (providers != null) { foreach (var p in providers) { ProviderConfigurationEntry provider = new ProviderConfigurationEntry(p.Key, p.Value.ImplementingType, p.Value.HelpFileName, mshsnapinInfo); _providers.AddBuiltInItem(provider); } } foreach (string file in mshsnapinInfo.Types) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); TypeConfigurationEntry typeEntry = new TypeConfigurationEntry(path, path, mshsnapinInfo); this.Types.AddBuiltInItem(typeEntry); } foreach (string file in mshsnapinInfo.Formats) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); FormatConfigurationEntry formatEntry = new FormatConfigurationEntry(path, path, mshsnapinInfo); this.Formats.AddBuiltInItem(formatEntry); } AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(assemblyEntry); return; }