private void GetFilteredRegistryKeyProperties(string path, Collection<string> propertyNames, bool getAll, bool writeAccess, out IRegistryWrapper key, out Collection<string> filteredCollection) { bool flag = false; if (string.IsNullOrEmpty(path)) { throw PSTraceSource.NewArgumentException("path"); } filteredCollection = new Collection<string>(); key = this.GetRegkeyForPathWriteIfError(path, writeAccess); if (key != null) { if (propertyNames == null) { propertyNames = new Collection<string>(); } if ((propertyNames.Count == 0) && getAll) { propertyNames.Add("*"); flag = true; } string[] valueNames = new string[0]; try { valueNames = key.GetValueNames(); } catch (IOException exception) { base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.ReadError, path)); return; } catch (SecurityException exception2) { base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, path)); return; } catch (UnauthorizedAccessException exception3) { base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, path)); return; } foreach (string str in propertyNames) { WildcardPattern pattern = new WildcardPattern(str, WildcardOptions.IgnoreCase); bool hadAMatch = false; foreach (string str2 in valueNames) { string input = str2; if (string.IsNullOrEmpty(str2) && !string.IsNullOrEmpty(str)) { input = this.GetLocalizedDefaultToken(); } if ((flag || (!base.Context.SuppressWildcardExpansion && pattern.IsMatch(input))) || (base.Context.SuppressWildcardExpansion && string.Equals(input, str, StringComparison.OrdinalIgnoreCase))) { if (string.IsNullOrEmpty(input)) { input = this.GetLocalizedDefaultToken(); } hadAMatch = true; filteredCollection.Add(str2); } } this.WriteErrorIfPerfectMatchNotFound(hadAMatch, path, str); } } }
private bool CopyRegistryKey(IRegistryWrapper key, string path, string destination, bool recurse, bool streamResult, bool streamFirstOnly) { bool flag = true; if (recurse && this.ErrorIfDestinationIsSourceOrChildOfSource(path, destination)) { return false; } tracer.WriteLine("destination = {0}", new object[] { destination }); IRegistryWrapper regkeyForPath = this.GetRegkeyForPath(destination, true); string childName = this.GetChildName(path); string parentPath = destination; if (regkeyForPath == null) { parentPath = this.GetParentPath(destination, null); childName = this.GetChildName(destination); regkeyForPath = this.GetRegkeyForPathWriteIfError(parentPath, true); } if (regkeyForPath == null) { return false; } string str3 = this.MakePath(parentPath, childName); string copyKeyAction = RegistryProviderStrings.CopyKeyAction; string copyKeyResourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate; string target = string.Format(base.Host.CurrentCulture, copyKeyResourceTemplate, new object[] { path, destination }); if (base.ShouldProcess(target, copyKeyAction)) { IRegistryWrapper wrapper2 = null; try { wrapper2 = regkeyForPath.CreateSubKey(childName); } catch (NotSupportedException exception) { base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.InvalidOperation, childName)); } if (wrapper2 != null) { string[] valueNames = key.GetValueNames(); for (int i = 0; i < valueNames.Length; i++) { if (base.Stopping) { regkeyForPath.Close(); wrapper2.Close(); return false; } wrapper2.SetValue(valueNames[i], key.GetValue(valueNames[i], null, RegistryValueOptions.DoNotExpandEnvironmentNames), key.GetValueKind(valueNames[i])); } if (streamResult) { this.WriteRegistryItemObject(wrapper2, str3); if (streamFirstOnly) { streamResult = false; } } } } regkeyForPath.Close(); if (recurse) { string[] subKeyNames = key.GetSubKeyNames(); for (int j = 0; j < subKeyNames.Length; j++) { if (base.Stopping) { return false; } string str7 = this.MakePath(path, subKeyNames[j]); string str8 = this.MakePath(str3, subKeyNames[j]); IRegistryWrapper wrapper3 = this.GetRegkeyForPath(str7, false); bool flag2 = this.CopyRegistryKey(wrapper3, str7, str8, recurse, streamResult, streamFirstOnly); wrapper3.Close(); if (!flag2) { flag = flag2; } } } return flag; }
private void WriteRegistryItemObject(IRegistryWrapper key, string path) { if (key != null) { path = path.Replace('/', '\\'); path = EscapeSpecialChars(path); PSObject item = PSObject.AsPSObject(key.RegistryKey); string[] valueNames = key.GetValueNames(); for (int i = 0; i < valueNames.Length; i++) { if (string.IsNullOrEmpty(valueNames[i])) { valueNames[i] = this.GetLocalizedDefaultToken(); break; } } item.AddOrSetProperty("Property", valueNames); base.WriteItemObject(item, path, true); } }
/// <summary> /// Wraps a registry item in a PSObject and sets the TreatAs to /// Microsoft.Win32.RegistryKey. This way values will be presented /// in the same format as keys /// </summary> /// /// <param name="key"> /// The registry key to be written out. /// </param> /// /// <param name="path"> /// The path to the item being written out. /// </param> /// private void WriteRegistryItemObject( IRegistryWrapper key, string path) { if (key == null) { Dbg.Diagnostics.Assert( key != null, "The RegistryProvider should never attempt to write out a null value"); // Don't error, but don't write out anything either. return; } // Escape any wildcard characters in the path path = EscapeSpecialChars(path); // Wrap the key in an PSObject PSObject outputObject = PSObject.AsPSObject(key.RegistryKey); // Add the registry values to the PSObject string[] valueNames = key.GetValueNames(); for (int index = 0; index < valueNames.Length; ++index) { if (String.IsNullOrEmpty(valueNames[index])) { // The first unnamed value becomes the default value valueNames[index] = GetLocalizedDefaultToken(); break; } } outputObject.AddOrSetProperty("Property", valueNames); WriteItemObject(outputObject, path, true); } // WriteRegistryItemObject
private void GetFilteredRegistryKeyProperties(string path, Collection<string> propertyNames, bool getAll, bool writeAccess, out IRegistryWrapper key, out Collection<string> filteredCollection) { bool expandAll = false; if (String.IsNullOrEmpty(path)) { throw PSTraceSource.NewArgumentException("path"); } filteredCollection = new Collection<string>(); key = GetRegkeyForPathWriteIfError(path, writeAccess); if (key == null) { return; } // If properties were not specified, get all the values if (propertyNames == null) { propertyNames = new Collection<string>(); } if (propertyNames.Count == 0 && getAll) { propertyNames.Add("*"); expandAll = true; } string[] valueNames; try { valueNames = key.GetValueNames(); } catch (System.IO.IOException ioException) { // An exception occurred while trying to get the key. Write // out the error. WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.ReadError, path)); return; } catch (System.Security.SecurityException securityException) { // An exception occurred while trying to get the key. Write // out the error. WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, path)); return; } catch (System.UnauthorizedAccessException unauthorizedAccessException) { // An exception occurred while trying to get the key. Write // out the error. WriteError(new ErrorRecord(unauthorizedAccessException, unauthorizedAccessException.GetType().FullName, ErrorCategory.PermissionDenied, path)); return; } foreach (string requestedValueName in propertyNames) { WildcardPattern valueNameMatcher = WildcardPattern.Get( requestedValueName, WildcardOptions.IgnoreCase); bool hadAMatch = false; foreach (string valueName in valueNames) { string valueNameToMatch = valueName; // Need to convert the default value name to "(default)" if (String.IsNullOrEmpty(valueName)) { // Only do the conversion if the caller isn't asking for // "" or null. if (!String.IsNullOrEmpty(requestedValueName)) { valueNameToMatch = GetLocalizedDefaultToken(); } } if ( expandAll || ((Context.SuppressWildcardExpansion == false) && (valueNameMatcher.IsMatch(valueNameToMatch))) || ((Context.SuppressWildcardExpansion == true) && (String.Equals(valueNameToMatch, requestedValueName, StringComparison.OrdinalIgnoreCase)))) { if (String.IsNullOrEmpty(valueNameToMatch)) { // If the value name is empty then using "(default)" // as the property name when adding the note, as // PSObject does not allow an empty propertyName valueNameToMatch = GetLocalizedDefaultToken(); } hadAMatch = true; filteredCollection.Add(valueName); } } // foreach WriteErrorIfPerfectMatchNotFound(hadAMatch, path, requestedValueName); } // foreach }
} // CopyItem private bool CopyRegistryKey( IRegistryWrapper key, string path, string destination, bool recurse, bool streamResult, bool streamFirstOnly) { bool result = true; // Make sure we are not trying to do a recursive copy of a key // to itself or a child of itself. if (recurse) { if (ErrorIfDestinationIsSourceOrChildOfSource(path, destination)) { return false; } } Dbg.Diagnostics.Assert( key != null, "The key should have been validated by the caller"); Dbg.Diagnostics.Assert( !String.IsNullOrEmpty(path), "The path should have been validated by the caller"); Dbg.Diagnostics.Assert( !String.IsNullOrEmpty(destination), "The destination should have been validated by the caller"); s_tracer.WriteLine("destination = {0}", destination); // Get the parent key of the destination // If the destination already exists and is a key, then it becomes // the container of the source. If the key doesn't already exist // the parent of the destination path becomes the container of source. IRegistryWrapper newParentKey = GetRegkeyForPath(destination, true); string destinationName = GetChildName(path); string destinationParent = destination; if (newParentKey == null) { destinationParent = GetParentPath(destination, null); destinationName = GetChildName(destination); newParentKey = GetRegkeyForPathWriteIfError(destinationParent, true); } if (newParentKey == null) { // The key was not found. // An error should have been written by GetRegkeyForPathWriteIfError return false; } string destinationPath = MakePath(destinationParent, destinationName); // Confirm the copy item with the user string action = RegistryProviderStrings.CopyKeyAction; string resourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate; string resource = String.Format( Host.CurrentCulture, resourceTemplate, path, destination); if (ShouldProcess(resource, action)) { // Create new key under the parent IRegistryWrapper newKey = null; try { newKey = newParentKey.CreateSubKey(destinationName); } catch (NotSupportedException e) { WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.InvalidOperation, destinationName)); } if (newKey != null) { // Now copy all the properties from the source to the destination string[] valueNames = key.GetValueNames(); for (int index = 0; index < valueNames.Length; ++index) { // Making sure to obey the StopProcessing. if (Stopping) { newParentKey.Close(); newKey.Close(); return false; } newKey.SetValue( valueNames[index], key.GetValue(valueNames[index], null, RegistryValueOptions.DoNotExpandEnvironmentNames), key.GetValueKind(valueNames[index])); } if (streamResult) { // Write out the key that was copied WriteRegistryItemObject(newKey, destinationPath); if (streamFirstOnly) { streamResult = false; } } } } newParentKey.Close(); if (recurse) { // Copy all the subkeys string[] subkeyNames = key.GetSubKeyNames(); for (int keyIndex = 0; keyIndex < subkeyNames.Length; ++keyIndex) { // Making sure to obey the StopProcessing. if (Stopping) { return false; } // Make the new path under the copy path. string subKeyPath = MakePath(path, subkeyNames[keyIndex]); string newSubKeyPath = MakePath(destinationPath, subkeyNames[keyIndex]); IRegistryWrapper childKey = GetRegkeyForPath(subKeyPath, false); bool subtreeResult = CopyRegistryKey(childKey, subKeyPath, newSubKeyPath, recurse, streamResult, streamFirstOnly); childKey.Close(); if (!subtreeResult) { result = subtreeResult; } } } return result; } // CopyRegistryKey