简体   繁体   English

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

[英]Batch file: search registry and set multiple variables

I'm attempting to write a batch script that will search the registry and add the value of the UninstallString within a key into a variable.我正在尝试编写一个批处理脚本,该脚本将搜索注册表并将键中 UninstallString 的值添加到变量中。
There are a few caveats:有几个注意事项:
1. The keys may be different on different workstations (depends on installer used, multiple modified MSI versions exist for samesoftware version) 1. 不同工作站的密钥可能不同(取决于使用的安装程序,同一软件版本存在多个修改的MSI版本)
2. There is the main product with patches installed 2.有安装补丁的主产品
3. The patches must be uninstalled first before the main product 3. 主产品必须先卸载补丁

For example, the Cisco Supervisor Desktop software may have the following keys:例如,Cisco Supervisor Desktop 软件可能具有以下键:

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

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

Patch 1:补丁 1:
KEY: HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall{CA941834-837E-44C2-BF83-E7E7558FDD61} KEY: HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall{CA941834-837E-44C2-BF83-E7E7558FDD61}
DisplayName value: Cisco Desktop Services 8.5(4) Maintenance Release 4 DisplayName 值: Cisco 桌面服务 8.5(4) 维护版本 4
UninstallString value: MsiExec.exe /X{CA941834-837E-44C2-BF83-E7E7558FDD61} UninstallString 值: MsiExec.exe /X{CA941834-837E-44C2-BF83-E7E7558FDD61}

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

They must be uninstalled in that order.必须按该顺序卸载它们。 There is also the potential for other keys to be in the registry when multiple versions are installed.安装多个版本时,注册表中也有可能存在其他键。

I would think a search function could be created for the patches using DisplayName value Cisco Desktop Services since it's common for all the patches then a separate search for the Cisco Supervisor Agent.我认为可以使用 DisplayName 值 Cisco Desktop Services 为补丁创建搜索功能,因为所有补丁都是通用的,然后单独搜索 Cisco Supervisor Agent。

I currently have WMIC commands but sometimes they can be extremely slow to run while if I manually run the UninstallString value it completes quickly.我目前有 WMIC 命令,但有时它们运行起来可能会非常慢,而如果我手动运行 UninstallString 值,它会很快完成。 I have also had instances where the WMIC command will not uninstall the product when the UninstallString value will.我也遇到过当 UninstallString 值卸载时 WMIC 命令不会卸载产品的情况。

Any help would be appreciated if this is possible.如果可能的话,任何帮助将不胜感激。

@wOxxOm @wOxxOm
(This is a long comment not an answer.) Thank you for your suggestion. (这是一个很长的评论而不是答案。)谢谢您的建议。 Unfortunately I have never been this deep into batch coding before.不幸的是,我以前从未如此深入地进行批处理编码。 But I have taken your code, modified it, and was able to export the DisplayName and UninstallString lists into text files.但是我已经使用了您的代码,对其进行了修改,并且能够将DisplayNameUninstallString列表导出到文本文件中。 I have also been able to sort the DisplayName list correctly but I have not been able to get the corresponding UninstallString to sort in the same order.我还能够正确地对DisplayName列表进行排序,但是我无法让相应的UninstallString以相同的顺序进行排序。 For example, the unsorted DisplayName list is listed as 2, 3, 1. I can get it sorted as 3, 2, 1 using sort /r .例如,未排序的DisplayName列表被列为 2, 3, 1。我可以使用sort /r将它排序为 3, 2, 1。 But if I sort the UninstallString list with sort /r I get a sorted list of 1, 3, 2.但是,如果我使用sort /rUninstallString列表进行sort /r则会得到一个 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}

The #'s at the beginning of each line is the order of original installation as they MUST be uninstalled in reverse order.每行开头的 # 是原始安装的顺序,因为它们必须以相反的顺序卸载。 The #'s are for reference. # 号供参考。

I would like to be able to do this without the txt files if possible.如果可能的话,我希望能够在没有 txt 文件的情况下做到这一点。

Thanks谢谢

Here is the modified code:这是修改后的代码:

@echo off
setlocal enableDelayedExpansion
::Build array
for %%a in ("" "\Wow6432Node") do (
    for /f "delims=" %%b in ('
        reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
            /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:\Test\app1.txt
                echo !app!,!Uninstall! >> C:\Test\un1.txt
                )
            )
        )
    )
)
sort /r < C:\Test\app.txt > C:\Test\app_sorted.txt

EDIT: If I do ^| sort /r编辑:如果我这样做^| sort /r ^| sort /r to the DisplayName find line, I will get the DisplayName results I am looking for but it repeats the same UninstallString for each DisplayName listed: ^| sort /rDisplayName查找行,我将获得我正在查找的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} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM