internal static bool UnInstallZipPackage(PackageSourceListRequest request, string fastPath) { string sourceLocation; string id; string displayName; string version; string path; var package = request.GetFastReferenceComplex(fastPath); if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out path)) { request.Error(ErrorCategory.InvalidOperation, "package", Constants.Messages.UnableToUninstallPackage); return(false); } if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path)) { path.TryHardToDelete(); request.YieldFromSwidtag(package, path); return(true); } else { request.Error(ErrorCategory.InvalidData, "folder {0} does not exist", path); return(false); } }
private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package) { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath)); string sourceLocation; string id; string displayName; string version; string fastPackageReference; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference)) { //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package. request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } // Normally uninstall-package will be handled by MSI provider. Here we added a special case for handling uninstall-package nodejs // which msi provider unable to deal with (node.js only for msi) if (id != null && id.EqualsIgnoreCase("nodejs")) { var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request); if (provider != null) { if (!_fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name); var p = _fastPackReftable[fastPackageReference]; var installing = provider.UninstallPackage(p, request); foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } return; } } } else { //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } }
internal static bool UnInstallZipPackage(PackageJson package, PackageSourceListRequest request, string fastPath) { string sourceLocation; string id; string displayName; string version; string path; string providerName; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out path, providerName: out providerName)) { request.Error(ErrorCategory.InvalidOperation, "package", Constants.Messages.UnableToUninstallPackage); return(false); } if (request.AddToPath.Value) { request.Warning(Resources.Messages.AddOrRemovePath, Constants.ProviderName, "AddToPath", "Install-Package"); } if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path)) { //Try to delete the directory to see if we can. Error out if we donot have permission if (Directory.Exists(path)) { try { Directory.Delete(path, true); } catch (System.UnauthorizedAccessException) { request.WriteError(ErrorCategory.InvalidOperation, package.Name, Resources.Messages.UninstallFailed, "UnInstall-Package", "UnauthorizedAccessException. The requested operation likely requires elevation, i.e., launch PowerShell as administer"); } } path.TryHardToDelete(); var dir = Path.Combine(package.Destination, package.Name); // delete an empty directory if (Directory.Exists(dir) && (!Directory.GetDirectories(dir).Any()) && !(Directory.GetFiles(dir).Any())) { dir.TryHardToDelete(); } request.YieldFromSwidtag(package, path); RemoveEnvironmentVariable(request, path, SystemEnvironmentKey); RemoveEnvironmentVariable(request, path, UserEnvironmentKey); return(true); } else { request.WriteError(ErrorCategory.InvalidData, path, Resources.Messages.DirectoryNotExist, Constants.ProviderName, path); return(false); } }
internal static void UninstallExePackage(string fastPath, PackageSourceListRequest request) { if (string.IsNullOrWhiteSpace(fastPath)) { return; } if (request == null) { throw new ArgumentNullException("request"); } string sourceLocation; string id; string displayName; string version; string fastPackageReference; string providerName; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference, providerName: out providerName)) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } var ver = (new SemanticVersion(version)).ToString(); var package = request.GetPackage(id, ver) ?? request.GetPackage(displayName, ver); request.Debug("Calling '{0}::UninstallPackage' '{1}'", Constants.ProviderName, fastPackageReference); var path = fastPackageReference.Split(new[] { '\\' }, 3); var uninstallCommand = string.Empty; Dictionary <string, string> properties = null; if (path.Length == 3) { switch (path[0].ToLowerInvariant()) { case "hklm64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hklm32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; } if (properties == null) { return; } var result = false; if (!string.IsNullOrWhiteSpace(uninstallCommand)) { do { if (File.Exists(uninstallCommand)) { result = ExecuteUninstallCommand(fastPackageReference, request, uninstallCommand, package.UnInstallAdditionalArguments); break; } // not a single file. // check if it's just quoted. var c = uninstallCommand.Trim('\"'); if (File.Exists(c)) { result = ExecuteUninstallCommand(fastPackageReference, request, c, package.UnInstallAdditionalArguments); break; } if (uninstallCommand[0] == '"') { var p = uninstallCommand.IndexOf('"', 1); if (p > 0) { var file = uninstallCommand.Substring(1, p - 1); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); } } } else { var p = uninstallCommand.IndexOf(' '); if (p > 0) { var file = uninstallCommand.Substring(0, p); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); continue; } var s = 0; do { s = uninstallCommand.IndexOf(' ', s + 1); if (s == -1) { break; } file = uninstallCommand.Substring(0, s); if (File.Exists(file)) { args = uninstallCommand.Substring(s + 1); args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); break; } } while (s > -1); if (s == -1) { // never found a way to parse the command :( request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); return; } } } } while (false); if (result) { YieldPackage(fastPackageReference, fastPackageReference, properties, request); return; } } request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); } }
internal static void UninstallExePackage(string fastPath, PackageSourceListRequest request) { if (string.IsNullOrWhiteSpace(fastPath)) { return; } if (request == null) { throw new ArgumentNullException("request"); } string sourceLocation; string id; string displayName; string version; string fastPackageReference; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } var ver = (new SemanticVersion(version)).ToString(); var package = request.GetPackage(id, ver) ?? request.GetPackage(displayName, ver); request.Debug("Calling '{0}::UninstallPackage' '{1}'", Constants.ProviderName, fastPackageReference); var path = fastPackageReference.Split(new[] { '\\' }, 3); var uninstallCommand = string.Empty; Dictionary<string, string> properties = null; if (path.Length == 3) { switch (path[0].ToLowerInvariant()) { case "hklm64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hklm32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; } if (properties == null) { return; } var result = false; if (!string.IsNullOrWhiteSpace(uninstallCommand)) { do { if (File.Exists(uninstallCommand)) { result = ExecuteUninstallCommand(fastPackageReference, request, uninstallCommand, package.UnInstallAdditionalArguments); break; } // not a single file. // check if it's just quoted. var c = uninstallCommand.Trim('\"'); if (File.Exists(c)) { result = ExecuteUninstallCommand(fastPackageReference, request, c, package.UnInstallAdditionalArguments); break; } if (uninstallCommand[0] == '"') { var p = uninstallCommand.IndexOf('"', 1); if (p > 0) { var file = uninstallCommand.Substring(1, p - 1); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); } } } else { var p = uninstallCommand.IndexOf(' '); if (p > 0) { var file = uninstallCommand.Substring(0, p); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); continue; } var s = 0; do { s = uninstallCommand.IndexOf(' ', s + 1); if (s == -1) { break; } file = uninstallCommand.Substring(0, s); if (File.Exists(file)) { args = uninstallCommand.Substring(s + 1); args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); break; } } while (s > -1); if (s == -1) { // never found a way to parse the command :( request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); return; } } } } while (false); if (result) { YieldPackage(fastPackageReference, fastPackageReference, properties, request); return; } } request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); } }
private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package) { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath)); string sourceLocation; string id; string displayName; string version; string fastPackageReference; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference)) { //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package. request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } // Normally uninstall-package will be handled by MSI provider. Here we added a special case for handling uninstall-package nodejs // which msi provider unable to deal with (node.js only for msi) if (id != null && id.EqualsIgnoreCase("nodejs")) { var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request); if (provider != null) { if (!_fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name); var p = _fastPackReftable[fastPackageReference]; var installing = provider.UninstallPackage(p, request); foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } return; } } } else { //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } }
private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package) { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}'", package.Name)); string sourceLocation; string id; string displayName; string version; string fastPackageReference; string providerNameFromPipeline; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference, providerName: out providerNameFromPipeline)) { //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package. request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.Msi); return; } // Normally uninstall-package will be handled by MSI provider, so we do not need to handle it here. // But there are some special cases where MSI provider does not work well. // // For example, after installing the nodejs.msi (install-package nodejs), the product name appears to be "node.js". // After installing the PowerShell.msi package (install-package PowerShell), the product name appears to be "PowerShell_<Version>". // The msi provider works well if you pass in the product display name. In this case you need to use "PowerShell_Version" in // the Get-Package and UnInstall-Package. // But the msi provider will not work if you "Uninstall-Package PowerShell" or "Uninstall-Package nodejs" // Here we add some logic to let PSL try both well-known package name and the displayname. string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider"); if ((!string.IsNullOrWhiteSpace(id) && (!string.IsNullOrWhiteSpace(displayName)) && (!id.EqualsIgnoreCase(displayName)) && (id.EqualsIgnoreCase(package.Name))) || (!string.IsNullOrWhiteSpace(providerNameFromPipeline) && Constants.ProviderName.EqualsIgnoreCase(providerNameFromPipeline)) || (!string.IsNullOrWhiteSpace(userSpecifiedProvider) && Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider))) { var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request); if (provider != null) { if (!_fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name); var p = _fastPackReftable[fastPackageReference]; var installing = provider.UninstallPackage(p, request); foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } return; } } } else { //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.Msi); return; } }
internal static bool UnInstallZipPackage(PackageSourceListRequest request, string fastPath) { string sourceLocation; string id; string displayName; string version; string path; var package = request.GetFastReferenceComplex(fastPath); if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out path)) { request.Error(ErrorCategory.InvalidOperation, "package", Constants.Messages.UnableToUninstallPackage); return false; } if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path)) { path.TryHardToDelete(); request.YieldFromSwidtag(package, path); return true; } else { request.Error(ErrorCategory.InvalidData, "folder {0} does not exist", path); return false; } }