powershell - 如何使用 PowerShell 包管理删除 Python 安装?

标签 powershell chocolatey

我想编写一个脚本来清理机器上所有现有的 Python 版本,以及一个单独的脚本来在标准化位置重新安装 2.7 和 3.5 版本。我目前正在尝试使用 PowerShell 5.1.14393.187 中的包管理 cmdlet 来执行此操作。

对于清理脚本,我从 PowerShell 的包命令开始:

Get-Package "*python*" | Uninstall-Package

当从管理控制台运行时,它似乎工作得很好,但在进一步调查中留下了一些剩余的包......

PS C:\WINDOWS\system32> Get-Package "*python*"

Name                           Version          Source                           ProviderName
----                           -------          ------                           ------------
Python 3.5.1 (64-bit)          3.5.1150.0                                        Programs
Python 3.5.1 pip Bootstrap ... 3.5.1150.0                                        msi
Python 3.5.1 Tcl/Tk Support... 3.5.1150.0                                        msi
Python 2.7.11                  2.7.11150                                         msi
Python 3.5.2 pip Bootstrap ... 3.5.2150.0                                        msi
Python 3.5.2 (32-bit)          3.5.2150.0                                        Programs

为什么这些包在 Uninstall-Package 之后仍然存在?有没有最佳实践方法来做到这一点?是否有最佳实践方法来编写 Python 的重新安装脚本,以免再次发生这种情况?

更新

通过使用控制面板 GUI 先修复然后卸载 Python 3 安装,我已经成功清理了其中的大部分内容。我很惊讶没有 Repair-Package 命令与 Get-Package 一起使用。

修复 Python 3 的其他部分后,有一个名为“Python Launcher”的 MSI 包,Get-Package 报告了 Python 2.7 MSI 包,但 GUI 中没有任何内容。此时,“Python 启动器”上的 Uninstall-Package 成功,并出现需要重启的警告。 msi:Python 2.7.11/2.7.11150 就没有这样的运气。


附加信息:

我认为 Chocolatey v0.10.1 可能是造成目前情况的原因。至少有些机器可能已经使用公共(public)存储库中的 chocolaty 安装了 python。在上面的同一台机器上,我也试过这个:

PS C:\WINDOWS\system32> choco uninstall python
Chocolatey v0.10.1
Uninstalling the following packages:
python
python is not installed. Cannot uninstall a non-existent package.

Chocolatey uninstalled 0/1 packages. 1 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Failures
 - python - python is not installed. Cannot uninstall a non-existent package.

Did you know the proceeds of Pro (and some proceeds from other
 licensed editions) go into bettering the community infrastructure?
 Your support ensures an active community, it makes you look smarter,
 plus it nets you some awesome features!
 https://chocolatey.org/compare
PS C:\WINDOWS\system32> choco uninstall python3
Chocolatey v0.10.1
Uninstalling the following packages:
python3

python3 v3.5.1
 Skipping auto uninstaller - No registry snapshot.
 python3 has been successfully uninstalled.

Chocolatey uninstalled 1/1 packages. 0 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

PS C:\WINDOWS\system32> choco uninstall python2
Chocolatey v0.10.1
Uninstalling the following packages:
python2

python2 v2.7.11
 Running auto uninstaller...
 Skipping auto uninstaller - The application appears to have been uninstalled already by other means.
 python2 has been successfully uninstalled.

Chocolatey uninstalled 1/1 packages. 0 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

PS C:\WINDOWS\system32> get-package "*python*"

Name                           Version          Source                           ProviderName
----                           -------          ------                           ------------
Python 3.5.1 (64-bit)          3.5.1150.0                                        Programs
Python 3.5.1 pip Bootstrap ... 3.5.1150.0                                        msi
Python 3.5.1 Tcl/Tk Support... 3.5.1150.0                                        msi
Python 2.7.11                  2.7.11150                                         msi
Python 3.5.2 pip Bootstrap ... 3.5.2150.0                                        msi

最佳答案

为了回答这个问题,我将强调几点供您考虑。

  1. 在宣布正式支持之前,请勿使用 Chocolatey 的非官方 PowerShell PackageManagement 提供程序。这是一个不受支持的预览版,存在错误和安全漏洞(它也不是由 Chocolatey 团队创建的)。而是使用 choco.exe 或其他官方提供程序。
  2. AutoUninstaller 是官方 Chocolatey 客户端中的资源,可以从不包含卸载脚本的软件包中删除 native 安装的软件。请务必注意,您还需要从那些官方客户端进行安装。更多信息请访问 https://chocolatey.org/docs/commands-uninstall

为什么这些包在 uninstall-package 后仍然存在?

这实际上取决于您使用什么来安装软件包以及 Chocolatey 是否能够为 auto uninstaller 捕获快照.

许多软件包不需要卸载脚本。有些人这样做。当它们是 MSI 并且在 Chocolatey 之外升级时(就像 Chrome 自动执行的那样),您要么需要 Package Synchronizer或用于卸载软件的卸载脚本。

是否有执行此操作的最佳实践方法?

如果这是供组织使用,并且您对损坏的容忍度较低,我们建议您构建自己的内部包。然后您就可以完全控制流程并拥有可重复、可靠的流程。这就是目前使用 Chocolatey 的数百家组织如何增强其安装过程的方式。他们通常已经在一些内部文件共享上安装了软件安装程序,并围绕它们构建软件包,以利用更好的自动化流程(相对于他们可能一直在使用的旧批处理文件,或者更糟的是,从中手动安装)。

如果您对为什么要自己构建感到好奇,请参阅 https://chocolatey.org/docs/community-packages-disclaimer (它试图强调公共(public)存储库的问题,并且它受分发权的约束,而内部存储库不受此约束)。

是否有最佳实践方法来编写 python 的重新安装脚本,以免再次发生这种情况?

将 Puppet、Chef、Ansible 或 DSC 等配置管理工具与 Chocolatey 提供程序结合使用。 https://chocolatey.org/docs/features-infrastructure-automation

这就是您在所有机器上创建自动化并利用包管理的方式。

关于powershell - 如何使用 PowerShell 包管理删除 Python 安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39710224/

相关文章:

Powershell - 在 json 字符串中添加变量

windows - Chocolatey的安装路径在哪里?

chocolatey - 如何将参数传递给 Chocolatey 包中的安装程序?

powershell - 在 Windows 上通过 Ansible 安装 Chocolatey 时出错

c# - 通过 PowerShell 将多个查询参数传递给 WebService

rest - Powershell使用NTLM调用WebRequest进行发布

visual-studio - 删除 Visual Studio 终端中的 "Visual Studio 2022 Developer PowerShell" header

package - 我可以为不是安装程序的 EXE 创建 Chocolatey 包吗?

chocolatey - Chocolatey 如何知道本地安装了哪些软件包?

powershell - 如何使用 Powershell(不在 MS Active Directory 中)查询 LDAP 组的成员