protected override void ProcessRecord() { Collection <string> collection = UpdateData.Glob(base.PrependPath, "FormatPrependPathException", this); Collection <string> collection2 = UpdateData.Glob(base.AppendPath, "FormatAppendPathException", this); if (((base.PrependPath.Length <= 0) && (base.AppendPath.Length <= 0)) || ((collection.Count != 0) || (collection2.Count != 0))) { string updateFormatDataAction = UpdateDataStrings.UpdateFormatDataAction; string updateTarget = UpdateDataStrings.UpdateTarget; if (base.Context.RunspaceConfiguration != null) { for (int i = collection.Count - 1; i >= 0; i--) { string target = string.Format(Thread.CurrentThread.CurrentCulture, updateTarget, new object[] { collection[i] }); if (base.ShouldProcess(target, updateFormatDataAction)) { base.Context.RunspaceConfiguration.Formats.Prepend(new FormatConfigurationEntry(collection[i])); } } foreach (string str4 in collection2) { string str5 = string.Format(Thread.CurrentThread.CurrentCulture, updateTarget, new object[] { str4 }); if (base.ShouldProcess(str5, updateFormatDataAction)) { base.Context.RunspaceConfiguration.Formats.Append(new FormatConfigurationEntry(str4)); } } try { base.Context.CurrentRunspace.RunspaceConfiguration.Formats.Update(true); return; } catch (RuntimeException exception) { base.WriteError(new ErrorRecord(exception, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, null)); return; } } if (base.Context.InitialSessionState != null) { if (base.Context.InitialSessionState.DisableFormatUpdates) { throw new PSInvalidOperationException(UpdateDataStrings.FormatUpdatesDisabled); } HashSet <string> set = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase); Collection <SessionStateFormatEntry> collection3 = new Collection <SessionStateFormatEntry>(); for (int j = collection.Count - 1; j >= 0; j--) { string str6 = string.Format(Thread.CurrentThread.CurrentCulture, updateTarget, new object[] { collection[j] }); if (base.ShouldProcess(str6, updateFormatDataAction) && !set.Contains(collection[j])) { set.Add(collection[j]); collection3.Add(new SessionStateFormatEntry(collection[j])); } } foreach (SessionStateFormatEntry entry in base.Context.InitialSessionState.Formats) { if (entry.FileName != null) { if (!set.Contains(entry.FileName)) { set.Add(entry.FileName); collection3.Add(entry); } } else { collection3.Add(entry); } } foreach (string str7 in collection2) { string str8 = string.Format(Thread.CurrentThread.CurrentCulture, updateTarget, new object[] { str7 }); if (base.ShouldProcess(str8, updateFormatDataAction) && !set.Contains(str7)) { set.Add(str7); collection3.Add(new SessionStateFormatEntry(str7)); } } try { base.Context.InitialSessionState.Formats.Clear(); Collection <PSSnapInTypeAndFormatErrors> mshsnapins = new Collection <PSSnapInTypeAndFormatErrors>(); foreach (SessionStateFormatEntry entry2 in collection3) { string fileName = entry2.FileName; PSSnapInInfo pSSnapIn = entry2.PSSnapIn; if ((pSSnapIn != null) && !string.IsNullOrEmpty(pSSnapIn.Name)) { fileName = pSSnapIn.Name; } if (entry2.Formattable != null) { PSInvalidOperationException exception2 = new PSInvalidOperationException(UpdateDataStrings.CannotUpdateFormatWithFormatTable); base.WriteError(new ErrorRecord(exception2, "CannotUpdateFormatWithFormatTable", ErrorCategory.InvalidOperation, null)); } else { if (entry2.FormatData != null) { mshsnapins.Add(new PSSnapInTypeAndFormatErrors(fileName, entry2.FormatData)); } else { mshsnapins.Add(new PSSnapInTypeAndFormatErrors(fileName, entry2.FileName)); } base.Context.InitialSessionState.Formats.Add(entry2); } } if (mshsnapins.Count > 0) { base.Context.FormatDBManager.UpdateDataBase(mshsnapins, base.Context.AuthorizationManager, base.Context.EngineHostInterface, false); FormatAndTypeDataHelper.ThrowExceptionOnError("ErrorsUpdatingFormats", null, mshsnapins, RunspaceConfigurationCategory.Formats); } } catch (RuntimeException exception3) { base.WriteError(new ErrorRecord(exception3, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, null)); } } } }
private void ProcessTypeFiles() { Collection <string> collection = UpdateData.Glob(base.PrependPath, "TypesPrependPathException", this); Collection <string> collection2 = UpdateData.Glob(base.AppendPath, "TypesAppendPathException", this); if (((base.PrependPath.Length <= 0) && (base.AppendPath.Length <= 0)) || ((collection.Count != 0) || (collection2.Count != 0))) { string updateTypeDataAction = UpdateDataStrings.UpdateTypeDataAction; string updateTarget = UpdateDataStrings.UpdateTarget; if (base.Context.RunspaceConfiguration != null) { for (int i = collection.Count - 1; i >= 0; i--) { string target = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { collection[i] }); if (base.ShouldProcess(target, updateTypeDataAction)) { base.Context.RunspaceConfiguration.Types.Prepend(new TypeConfigurationEntry(collection[i])); } } foreach (string str4 in collection2) { string str5 = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { str4 }); if (base.ShouldProcess(str5, updateTypeDataAction)) { base.Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(str4)); } } try { base.Context.CurrentRunspace.RunspaceConfiguration.Types.Update(true); return; } catch (RuntimeException exception) { base.WriteError(new ErrorRecord(exception, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null)); return; } } if (base.Context.InitialSessionState != null) { HashSet <string> set = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase); Collection <SessionStateTypeEntry> collection3 = new Collection <SessionStateTypeEntry>(); for (int j = collection.Count - 1; j >= 0; j--) { string str6 = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { collection[j] }); string item = ModuleCmdletBase.ResolveRootedFilePath(collection[j], base.Context) ?? collection[j]; if (base.ShouldProcess(str6, updateTypeDataAction) && !set.Contains(item)) { set.Add(item); collection3.Add(new SessionStateTypeEntry(collection[j])); } } foreach (SessionStateTypeEntry entry in base.Context.InitialSessionState.Types) { if (entry.FileName != null) { string str8 = ModuleCmdletBase.ResolveRootedFilePath(entry.FileName, base.Context) ?? entry.FileName; if (!set.Contains(str8)) { set.Add(str8); collection3.Add(entry); } } else { collection3.Add(entry); } } foreach (string str9 in collection2) { string str10 = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { str9 }); string str11 = ModuleCmdletBase.ResolveRootedFilePath(str9, base.Context) ?? str9; if (base.ShouldProcess(str10, updateTypeDataAction) && !set.Contains(str11)) { set.Add(str11); collection3.Add(new SessionStateTypeEntry(str9)); } } base.Context.InitialSessionState.Types.Clear(); Collection <string> errors = new Collection <string>(); bool clearTable = true; foreach (SessionStateTypeEntry entry2 in collection3) { try { if (entry2.TypeTable != null) { PSInvalidOperationException exception2 = new PSInvalidOperationException(UpdateDataStrings.CannotUpdateTypeWithTypeTable); base.WriteError(new ErrorRecord(exception2, "CannotUpdateTypeWithTypeTable", ErrorCategory.InvalidOperation, null)); continue; } if (entry2.FileName != null) { bool flag2; base.Context.TypeTable.Update(entry2.FileName, errors, clearTable, base.Context.AuthorizationManager, base.Context.InitialSessionState.Host, out flag2); } else { base.Context.TypeTable.Update(entry2.TypeData, errors, entry2.IsRemove, clearTable); } } catch (RuntimeException exception3) { base.WriteError(new ErrorRecord(exception3, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null)); } base.Context.InitialSessionState.Types.Add(entry2); if (errors.Count > 0) { foreach (string str12 in errors) { RuntimeException exception4 = new RuntimeException(str12); base.WriteError(new ErrorRecord(exception4, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null)); } errors.Clear(); } clearTable = false; } } } }
protected override void ProcessRecord() { if (base.ParameterSetName == "RemoveFileSet") { string removeTypeFileAction = UpdateDataStrings.RemoveTypeFileAction; string updateTarget = UpdateDataStrings.UpdateTarget; Collection <string> collection = UpdateData.Glob(this.typeFiles, "TypePathException", this); if (collection.Count != 0) { Dictionary <string, List <int> > fileNameToIndexMap = new Dictionary <string, List <int> >(StringComparer.OrdinalIgnoreCase); List <int> list = new List <int>(); if (base.Context.RunspaceConfiguration != null) { for (int i = 0; i < base.Context.RunspaceConfiguration.Types.Count; i++) { string fileName = base.Context.RunspaceConfiguration.Types[i].FileName; if (fileName != null) { ConstructFileToIndexMap(fileName, i, fileNameToIndexMap); } } } else if (base.Context.InitialSessionState != null) { for (int j = 0; j < base.Context.InitialSessionState.Types.Count; j++) { string filePath = base.Context.InitialSessionState.Types[j].FileName; if (filePath != null) { filePath = ModuleCmdletBase.ResolveRootedFilePath(filePath, base.Context) ?? filePath; ConstructFileToIndexMap(filePath, j, fileNameToIndexMap); } } } foreach (string str5 in collection) { string target = string.Format(CultureInfo.InvariantCulture, updateTarget, new object[] { str5 }); if (base.ShouldProcess(target, removeTypeFileAction)) { List <int> list2; if (fileNameToIndexMap.TryGetValue(str5, out list2)) { list.AddRange(list2); } else { base.WriteError(this.NewError("TypeFileNotExistsInCurrentSession", "TypeFileNotExistsInCurrentSession", null, new object[] { str5 })); } } } if (list.Count > 0) { list.Sort(); for (int k = list.Count - 1; k >= 0; k--) { if (base.Context.RunspaceConfiguration != null) { base.Context.RunspaceConfiguration.Types.RemoveItem(list[k]); } else if (base.Context.InitialSessionState != null) { base.Context.InitialSessionState.Types.RemoveItem(list[k]); } } try { if (base.Context.RunspaceConfiguration != null) { base.Context.RunspaceConfiguration.Types.Update(); } else if (base.Context.InitialSessionState != null) { bool refreshTypeAndFormatSetting = base.Context.InitialSessionState.RefreshTypeAndFormatSetting; try { base.Context.InitialSessionState.RefreshTypeAndFormatSetting = true; base.Context.InitialSessionState.UpdateTypes(base.Context, false, false); } finally { base.Context.InitialSessionState.RefreshTypeAndFormatSetting = refreshTypeAndFormatSetting; } } } catch (RuntimeException exception) { base.WriteError(new ErrorRecord(exception, "TypesFileRemoveException", ErrorCategory.InvalidOperation, null)); } } } } else { string removeTypeDataAction = UpdateDataStrings.RemoveTypeDataAction; string removeTypeDataTarget = UpdateDataStrings.RemoveTypeDataTarget; string typeName = null; if (base.ParameterSetName == "RemoveTypeDataSet") { typeName = this.typeData.TypeName; } else { if (string.IsNullOrWhiteSpace(this.typeName)) { base.ThrowTerminatingError(this.NewError("TargetTypeNameEmpty", "TargetTypeNameEmpty", this.typeName, new object[0])); } typeName = this.typeName; } System.Management.Automation.Runspaces.TypeData type = new System.Management.Automation.Runspaces.TypeData(typeName); string str10 = string.Format(CultureInfo.InvariantCulture, removeTypeDataTarget, new object[] { typeName }); if (base.ShouldProcess(str10, removeTypeDataAction)) { try { Collection <string> errors = new Collection <string>(); base.Context.TypeTable.Update(type, errors, true, false); if (errors.Count > 0) { foreach (string str11 in errors) { RuntimeException exception2 = new RuntimeException(str11); base.WriteError(new ErrorRecord(exception2, "TypesDynamicRemoveException", ErrorCategory.InvalidOperation, null)); } } else if (base.Context.RunspaceConfiguration != null) { base.Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(type, true)); } else if (base.Context.InitialSessionState != null) { base.Context.InitialSessionState.Types.Add(new SessionStateTypeEntry(type, true)); } } catch (RuntimeException exception3) { base.WriteError(new ErrorRecord(exception3, "TypesDynamicRemoveException", ErrorCategory.InvalidOperation, null)); } } } }