批处理文件:搜索注册表并设置多个变量



我正在尝试编写一个批处理脚本,该脚本将搜索注册表并将UninstallString的值添加到一个变量中
有几个注意事项:

  1. 不同工作站上的密钥可能不同(取决于使用的安装程序,同一软件版本存在多个修改后的MSI版本)
  2. 有安装了修补程序的主产品
  3. 必须先卸载修补程序,然后才能安装主产品

例如,Cisco Supervisor Desktop软件可能具有以下密钥:

修补程序3
KEY:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{981E3887-9D55-4B91-B643-7155AA98C906}
DisplayName值:Cisco Desktop Services 8.5(4)Maintenance Release 4 Engineering Special 6
UninstallString值:MsiExec.exe/X{981E3887-9D55-4B91-B643-7155AA98C906}

修补程序2
KEY:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}
DisplayName值:Cisco Desktop Services 8.5(4)Maintenance Release 4 Engineering Special 4
UninstallString值:MsiExec.exe/X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}

修补程序1:
KEY:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{CA941834-837E-44C2-BF83-E7E7558FDD61}
DisplayName值:Cisco Desktop Services 8.5(4)维护版本4
UninstallString值:MsiExec.exe/X{CA941834-837E-44C2-BF83-E7E7558FDD61}

主要产品:
KEY:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
DisplayName值:Cisco Supervisor Desktop
UninstallString值:MsiExec.exe/X{AB60EBDC-45A9-4764-96CB-EFCE4AD0C10B}

必须按此顺序卸载它们。当安装多个版本时,注册表中也可能存在其他密钥。

我认为可以使用DisplayName值Cisco Desktop Services为补丁程序创建搜索功能,因为它对所有补丁程序都很常见,然后单独搜索Cisco Supervisor Agent。

我目前有WMIC命令,但有时它们的运行速度可能非常慢,而如果我手动运行UninstallString值,它会很快完成。我也遇到过这样的例子,WMIC命令不会卸载产品,而UninstallString值会卸载。

根据@wOxxOm在评论中的建议(查看如何使用批处理文件和数组自动卸载显示名称中包含"VNC"的所有程序?),我已经获取了代码,对其进行了修改,并能够将DisplayNameUninstallString列表导出到文本文件中。我也能够正确地对DisplayName列表进行排序,但我无法获得相应的UninstallString以相同的顺序进行排序。例如,未排序的DisplayName列表列为2、3、1。我可以使用sort /r将其排序为3、2、1。但如果我用sort /rUninstallString列表进行排序,我会得到一个排序列表1、3、2。

app.txt    
2 Cisco Desktop Services 8.5(4) Maintenance Release 4 Engineering Special 4 
3 Cisco Desktop Services 8.5(4) Maintenance Release 4 Engineering Special 6 
1 Cisco Desktop Services 8.5(4) Maintenance Release 4 
app_sorted.txt
3 Cisco Desktop Services 8.5(4) Maintenance Release 4 Engineering Special 6 
2 Cisco Desktop Services 8.5(4) Maintenance Release 4 Engineering Special 4 
1 Cisco Desktop Services 8.5(4) Maintenance Release 4 
un.txt    
2 MsiExec.exe /X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}
3 MsiExec.exe /X{981E3887-9D55-4B91-B643-7155AA98C906}
1 MsiExec.exe /X{CA941834-837E-44C2-BF83-E7E7558FDD61}
un_sorted.txt    
1 MsiExec.exe /X{CA941834-837E-44C2-BF83-E7E7558FDD61}
3 MsiExec.exe /X{981E3887-9D55-4B91-B643-7155AA98C906}
2 MsiExec.exe /X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}
The un_sorted.txt needs to be:    
3 MsiExec.exe /X{981E3887-9D55-4B91-B643-7155AA98C906}
2 MsiExec.exe /X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}
1 MsiExec.exe /X{CA941834-837E-44C2-BF83-E7E7558FDD61}

每行开头的#是原始安装的顺序,因为它们必须以相反的顺序卸载。#仅供参考。

如果可能的话,我希望能够在没有txt文件的情况下做到这一点。

这是修改后的代码:

@echo off
setlocal enableDelayedExpansion
::Build array
for %%a in ("" "Wow6432Node") do (
    for /f "delims=" %%b in ('
        reg query HKLMSOFTWARE%%~aMicrosoftWindowsCurrentVersionUninstall ^
            /s /d /f "Cisco Desktop" ^| findstr "HKEY_ DisplayName" ^| sort /r
    ') do (
        set "line=%%b"
        if "!line:~0,4!"=="HKEY" (
            set "key=!line!"
        ) else (
            set Uninstall=
            rem Sort /r makes QuietUninstallString the last line
            for /f "tokens=2*" %%c in ('
                reg query "!key!" ^| find "UninstallString" ^| sort /r
            ') do if not "%%d"=="" set "Uninstall=%%d"
            if defined Uninstall (
                for /f "tokens=2*" %%c in ("!line!") do (
                set app=%%d
                echo !app! >> C:Testapp1.txt
                echo !app!,!Uninstall! >> C:Testun1.txt
                )
            )
        )
    )
)
sort /r < C:Testapp.txt > C:Testapp_sorted.txt

编辑:如果我对DisplayName查找行执行 ^| sort /r,我将获得我要查找的DisplayName结果,但它对列出的每个DisplayName重复相同的UninstallString

un.txt
3 Cisco Desktop Services 8.5(4) Maintenance Release 4 Engineering Special 6,MsiExec.exe /X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}    
2 Cisco Desktop Services 8.5(4) Maintenance Release 4 Engineering Special 4,MsiExec.exe /X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF}    
1 Cisco Desktop Services 8.5(4) Maintenance Release 4,MsiExec.exe /X{4FA5AFA8-FDEB-43C9-83B7-43092593ACDF} 

最新更新