关于通过Intune批量卸载所有Python版本并仅安装3.13.1的问题咨询
关于通过Intune批量卸载所有Python版本并仅安装3.13.1的问题咨询
我目前的需求是:在客户环境的500多台设备上完成操作后,只保留最新的Python 3.13.1版本,彻底移除所有旧版本。
我写了一段PowerShell脚本(放在文末),用来完成两件核心操作:
- 移除所有已存在的Python版本(包含安装文件夹、注册表项等相关痕迹)
- 仅安装最新的Python 3.13.1版本
本地用管理员权限启动PowerShell运行脚本时,一切都正常:所有旧Python版本都被清理干净,最后只剩3.13.1版本。
但把这个脚本作为Win32应用,通过Microsoft Intune以系统权限执行时,卸载就不彻底了:比如Python Launcher被删掉了,但3.9、3.10这些主版本还留在设备上。Intune日志里没报任何错误,但执行python --version命令时,仍然显示旧版本。
我的疑问:
- 为什么本地卸载能正常完成,但在NT AUTHORITY\SYSTEM上下文下执行时,就没法彻底移除Python?
- 如何移除存储在HKCU注册表或者本地自定义文件夹(比如D:\Python)中的Python安装?会不会是Intune的系统账号没有权限访问用户注册表项(HKCU)或者某些非系统目录?
- 什么是最优策略,能确保通过Intune彻底移除所有Python版本(包括用户级别的个人安装),然后干净地安装Python 3.13.1?
我使用的PowerShell脚本:
Write-Host "Removing all Python versions and related traces..." # Remove Python via registry Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall, HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall | ForEach-Object { $DisplayName = $_.GetValue("DisplayName") if ($DisplayName -like "*Python*") { $UninstallString = $_.GetValue("UninstallString") if ($UninstallString) { Write-Host "Uninstalling: $DisplayName" Start-Process -FilePath "cmd.exe" -ArgumentList "/c $UninstallString" -Wait -NoNewWindow } } } # Remove residual Python folders $PythonPaths = @( "$env:ProgramFiles\Python*", "$env:ProgramFiles(x86)\Python*", "$env:LocalAppData\Programs\Python", "$env:AppData\Python", "$env:LocalAppData\Python" ) foreach ($Path in $PythonPaths) { if (Test-Path -Path $Path) { Write-Host "Removing directory: $Path" Remove-Item -Path $Path -Recurse -Force -ErrorAction SilentlyContinue } } Write-Host "Complete removal of Python executed." # Python installation $PythonInstallerUrl = "https://www.python.org/ftp/python/3.13.1/python-3.13.1.exe" $InstallerPath = "$env:TEMP\python_installer.exe" Write-Host "Downloading Python installer..." Invoke-WebRequest -Uri $PythonInstallerUrl -OutFile $InstallerPath Write-Host "Installing Python..." Start-Process -FilePath $InstallerPath -ArgumentList "/quiet InstallAllUsers=1 PrependPath=1" -Wait Write-Host "Removing the installer..." Remove-Item -Path $InstallerPath -Force Write-Host "Python successfully installed."
备注:内容来源于stack exchange,提问作者Kanuc




