[英]Batch file: search registry and set multiple variables
我正在尝试编写一个批处理脚本,该脚本将搜索注册表并将键中 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 桌面服务 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 值,它会很快完成。 我也遇到过当 UninstallString 值卸载时 WMIC 命令不会卸载产品的情况。
如果可能的话,任何帮助将不胜感激。
@wOxxOm
(这是一个很长的评论而不是答案。)谢谢您的建议。 不幸的是,我以前从未如此深入地进行批处理编码。 但是我已经使用了您的代码,对其进行了修改,并且能够将DisplayName
和UninstallString
列表导出到文本文件中。 我还能够正确地对DisplayName
列表进行排序,但是我无法让相应的UninstallString
以相同的顺序进行排序。 例如,未排序的DisplayName
列表被列为 2, 3, 1。我可以使用sort /r
将它排序为 3, 2, 1。 但是,如果我使用sort /r
对UninstallString
列表进行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}
每行开头的 # 是原始安装的顺序,因为它们必须以相反的顺序卸载。 # 号供参考。
如果可能的话,我希望能够在没有 txt 文件的情况下做到这一点。
谢谢
这是修改后的代码:
@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
编辑:如果我这样做^| sort /r
^| sort /r
到DisplayName
查找行,我将获得我正在查找的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.