活动目录健康检查:PowerShell脚本

如果不及时处理,Java运行时环境可能会在企业中像野火一样蔓延。当我运行报告以了解我的环境中Java的情况时,我发现有62个不同版本的Java!是时候重新掌控局面并构建一个用于删除旧版本Java的PowerShell脚本了。

在本教程中,你将学习如何创建一个用于在PowerShell中删除旧Java的Java删除工具。

让我们开始吧!

创建脚本

首先,打开你最喜欢的代码编辑器,比如Visual Studio Code,并将文件保存为Remove-Java.ps1。这个PowerShell脚本将包含你清理旧版本Java所需的一切。

定义注册表路径

因为Java实例可以隐藏在注册表中的两个地方,所以首先定义每个父级注册表键。下面的代码片段提供了32位和64位注册表路径。

$RegUninstallPaths = @(
   'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
    'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)

定义要保留的版本

因为你可能想要保留一些旧版本的Java,所以创建一个包含你想要保留的每个版本的数组。

你可以在控制面板的“程序和功能”中找到要使用的确切文本。

$VersionsToKeep = @('Java 8 Update 261')

查找并停止所有运行中的进程

接下来,找到所有正在运行的Java进程。下面的代码片段使用WMI枚举所有运行中的进程,这些进程都是在路径匹配“Program Files\Java“的文件中运行的。

由于Java可以嵌入到Internet Explorer中且无法删除,因此也请继续停止所有Internet Explorer进程。

Get-CimInstance -ClassName 'Win32_Process' | Where-Object {$_.ExecutablePath -like '*Program Files\Java*'} | 
    Select-Object @{n='Name';e={$_.Name.Split('.')[0]}} | Stop-Process -Force
 
Get-process -Name *iexplore* | Stop-Process -Force -ErrorAction SilentlyContinue

搜索注册表中的Java并强制卸载

接下来,构建一个过滤器,你将用它来枚举上面定义的注册表键下的所有值。此过滤器查找所有的Java实例,并排除你想保留的所有版本。

$UninstallSearchFilter = {($_.GetValue('DisplayName') -like '*Java*') -and (($_.GetValue('Publisher') -eq 'Oracle Corporation')) -and ($VersionsToKeep -notcontains $_.GetValue('DisplayName'))}

现在搜索注册表以找到所有的Java实例,如果找到,则运行msiexec.exe /X来移除每个实例。

# 卸载不需要的Java版本并清理程序文件

foreach ($Path in $RegUninstallPaths) {
    if (Test-Path $Path) {
        Get-ChildItem $Path | Where-Object $UninstallSearchFilter | 
       foreach { 
        Start-Process 'C:\Windows\System32\msiexec.exe' "/X$($_.PSChildName) /qn" -Wait
    
        }
    }
}

移除任何旧的Java残留

清理Java卸载程序本身无法清除的任何残留部分。

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
$ClassesRootPath = "HKCR:\Installer\Products"
Get-ChildItem $ClassesRootPath | 
    Where-Object { ($_.GetValue('ProductName') -like '*Java*')} | Foreach {
    Remove-Item $_.PsPath -Force -Recurse
}

$JavaSoftPath = 'HKLM:\SOFTWARE\JavaSoft'
if (Test-Path $JavaSoftPath) {
    Remove-Item $JavaSoftPath -Force -Recurse
}

总结

就是这样了!在想要清理Java的每台计算机上运行此PowerShell脚本,使该过程尽可能无缝。

随时在下方一次性下载整个脚本。


$RegUninstallPaths = @(
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall,
HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
)
$VersionsToKeep = @(Java 8 Update 261)
Get-CimInstance ClassName Win32_Process | Where-Object {$_.ExecutablePath -like *Program Files\Java*} |
Select-Object @{n=Name;e={$_.Name.Split(.)[0]}} | Stop-Process Force
Get-process Name *iexplore* | Stop-Process Force ErrorAction SilentlyContinue
$UninstallSearchFilter = {($_.GetValue(DisplayName) -like *Java*) -and (($_.GetValue(Publisher) -eq Oracle Corporation)) -and ($VersionsToKeep -notcontains $_.GetValue(DisplayName))}
# Uninstall unwanted Java versions and clean up program files
foreach ($Path in $RegUninstallPaths) {
if (Test-Path $Path) {
Get-ChildItem $Path | Where-Object $UninstallSearchFilter |
foreach {
Start-Process C:\Windows\System32\msiexec.exe /X$($_.PSChildName) /qn Wait
}
}
}
New-PSDrive Name HKCR PSProvider Registry Root HKEY_CLASSES_ROOT | Out-Null
$ClassesRootPath = HKCR:\Installer\Products
Get-ChildItem $ClassesRootPath |
Where-Object { ($_.GetValue(ProductName) -like *Java*)} | Foreach {
Remove-Item $_.PsPath Force Recurse
}
$JavaSoftPath = HKLM:\SOFTWARE\JavaSoft
if (Test-Path $JavaSoftPath) {
Remove-Item $JavaSoftPath Force Recurse
}
view raw

Remove-Java.ps1

hosted with ❤ by GitHub

感谢Sam Turner帮助我修复了这段代码中的一些错误。

Source:
https://adamtheautomator.com/removing-old-java/