Example #1
0
        private bool UninstallPkgHelper()
        {
            var successfullyUninstalled = false;

            GetHelper     getHelper    = new GetHelper(this);
            List <string> dirsToDelete = getHelper.FilterPkgPathsByName(Name, _pathsToSearch);

            // Checking if module or script
            // a module path will look like:
            // ./Modules/TestModule/0.0.1
            // note that the xml file is located in this path, eg: ./Modules/TestModule/0.0.1/PSModuleInfo.xml
            // a script path will look like:
            // ./Scripts/TestScript.ps1
            // note that the xml file is located in ./Scripts/InstalledScriptInfos, eg: ./Scripts/InstalledScriptInfos/TestScript_InstalledScriptInfo.xml

            string pkgName;

            foreach (string pkgPath in getHelper.FilterPkgPathsByVersion(_versionRange, dirsToDelete, selectPrereleaseOnly: Prerelease))
            {
                pkgName = Utils.GetInstalledPackageName(pkgPath);

                if (!ShouldProcess(string.Format("Uninstall resource '{0}' from the machine.", pkgName)))
                {
                    WriteVerbose("ShouldProcess is set to false.");
                    continue;
                }

                ErrorRecord errRecord = null;
                if (pkgPath.EndsWith(PSScriptFileExt))
                {
                    successfullyUninstalled = UninstallScriptHelper(pkgPath, pkgName, out errRecord);
                }
                else
                {
                    successfullyUninstalled = UninstallModuleHelper(pkgPath, pkgName, out errRecord);
                }

                // if we can't find the resource, write non-terminating error and return
                if (!successfullyUninstalled || errRecord != null)
                {
                    if (errRecord == null)
                    {
                        string message = Version == null || Version.Trim().Equals("*") ?
                                         string.Format("Could not find any version of the resource '{0}' in any path", pkgName) :
                                         string.Format("Could not find verison '{0}' of the resource '{1}' in any path", Version, pkgName);

                        errRecord = new ErrorRecord(
                            new PSInvalidOperationException(message),
                            "ErrorRetrievingSpecifiedResource",
                            ErrorCategory.ObjectNotFound,
                            this);
                    }

                    WriteError(errRecord);
                }
            }

            return(successfullyUninstalled);
        }