Пример #1
0
 public virtual bool ShouldProcess(string target)
 {
     return(cmdlet.ShouldProcess(target));
 }
Пример #2
0
 public Task <bool> ShouldProcessAsync(string target, string action)
 => Scheduler.Invoke(() => Cmdlet.ShouldProcess(target, action));
Пример #3
0
 protected bool ShouldProcess(WebApiTeam target, string action)
 {
     return(Cmdlet.ShouldProcess($"Team '{target.Name}'", action));
 }
Пример #4
0
 protected bool ShouldProcess(Models.Connection target, string action)
 {
     return(Cmdlet.ShouldProcess($"Team Project Collection '{target.DisplayName}'", action));
 }
Пример #5
0
 protected bool ShouldProcess(string target, string action)
 {
     return(Cmdlet.ShouldProcess(target, action));
 }
Пример #6
0
        /// <summary>Method containing the core logic for the Copy-RegKey and Move-RegKey commands.</summary>
        internal static void CopyRegistryCommand(Cmdlet cmdlet, Dictionary <RegistryHive, List <string> > groupedRegKeysToProcess)
        {
            string whatIfText        = "Will overwrite \"{0}\" with data from \"{1}\"";
            string confirmText       = "Overwrite \"{0}\" with data from \"{1}\"?";
            string whatIfTextNewKey  = "Will create \"{0}\" and copy data from \"{1}\"";
            string confirmTextNewKey = "Create \"{0}\" and copy data from \"{1}\"?";

            RegistryKey regKey;

            string[]     computerName;
            RegistryView view;
            bool         deleteSourceKey;
            bool         disposeKeys = true;


            if (cmdlet is CopyRegKeyCommand)
            {
                CopyRegKeyCommand tempCmdlet = cmdlet as CopyRegKeyCommand;
                regKey          = tempCmdlet.Key;
                computerName    = tempCmdlet.ComputerName;
                view            = tempCmdlet.View;
                deleteSourceKey = false;
                if (tempCmdlet.DontDisposeKey)
                {
                    disposeKeys = false;
                }
            }
            else
            {
                whatIfText        = "Will move \"{1}\" to \"{0}\" - replacing any existing data.";
                confirmText       = "Move \"{1}\" to \"{0}\" - replacing any existing data?";
                whatIfTextNewKey  = "Will move \"{1}\" to \"{0}\"";
                confirmTextNewKey = "Move \"{1}\" to \"{0}\"?";

                MoveRegKeyCommand tempCmdlet = cmdlet as MoveRegKeyCommand;
                regKey       = tempCmdlet.Key;
                computerName = new string[1] {
                    tempCmdlet.ComputerName
                };
                view            = tempCmdlet.View;
                deleteSourceKey = true;
                if (tempCmdlet.DontDisposeKey)
                {
                    disposeKeys = false;
                }
            }


            foreach (string pcName in computerName)
            {
                foreach (RegistryHive hive in groupedRegKeysToProcess.Keys)
                {
                    RegistryKey baseKey;
                    try
                    {
                        cmdlet.WriteVerbose($"{pcName}: Opening base key {hive}");
                        baseKey = RegistryKey.OpenRemoteBaseKey(hive, pcName, view);
                    }
                    catch (Exception e) when(e is PipelineStoppedException == false)
                    {
                        cmdlet.WriteError(new ErrorRecord(e, "UnableToOpenBaseKey", GetErrorCategory(e), pcName));
                        continue;
                    }

                    foreach (string subKeyPath in groupedRegKeysToProcess[hive])
                    {
                        string      displayDestPath = $"{baseKey.Name}{_RegPathSeparator}{subKeyPath}";
                        string      actualWhatIfText;
                        string      actualConfirmText;
                        RegistryKey destinationKey = null;

                        try
                        {
                            RegistryRights regRights = RegistryRights.CreateSubKey | RegistryRights.SetValue | RegistryRights.QueryValues;
                            destinationKey = baseKey.OpenSubKey(subKeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, regRights);

                            if (null == destinationKey)
                            {
                                actualWhatIfText  = string.Format(whatIfTextNewKey, displayDestPath, regKey.Name);
                                actualConfirmText = string.Format(confirmTextNewKey, displayDestPath, regKey.Name);
                                if (cmdlet.ShouldProcess(actualWhatIfText, actualConfirmText, _ConfirmPrompt))
                                {
                                    destinationKey = baseKey.CreateSubKey(subKeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
                                    CopyRegistryKey(regKey, destinationKey, deleteSourceKey);
                                }
                            }
                            else
                            {
                                actualWhatIfText  = string.Format(whatIfText, displayDestPath, regKey.Name);
                                actualConfirmText = string.Format(confirmText, displayDestPath, regKey.Name);
                                if (cmdlet.ShouldProcess(actualWhatIfText, actualConfirmText, _ConfirmPrompt))
                                {
                                    CopyRegistryKey(regKey, destinationKey, deleteSourceKey);
                                }
                            }
                        }
                        catch (Exception e) when(e is PipelineStoppedException == false)
                        {
                            cmdlet.WriteError(new ErrorRecord(e, "UnableToCopyKey", GetErrorCategory(e), regKey));
                        }
                        finally
                        {
                            if (null != destinationKey)
                            {
                                destinationKey.Dispose();
                            }
                        }
                    }
                    baseKey.Dispose();
                }
            }
            if (disposeKeys)
            {
                regKey.Dispose();
            }
        }