如何自动卸载所有包含"VNC"的程序.在其显示名称中使用批处理文件? [英] How can I automatically uninstall all programs containing "VNC" in their displayname using a batch file?

查看:132
本文介绍了如何自动卸载所有包含"VNC"的程序.在其显示名称中使用批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个批处理脚本,该脚本将查询注册表以获取卸载软件包.目标是创建一个批处理脚本,该脚本将卸载所有VNC程序(UltraVNC,RealVNC和TightVNC).这必须与Windows XP和Windows 7兼容.

I am attempting to create a batch script that will query the registry for uninstall packages. The goal is to create a batch script that will uninstall all VNC programs (UltraVNC, RealVNC, and TightVNC). This must be compatible with both Windows XP and Windows 7.

我从此StackOverflow解决方案的脚本开始批处理脚本的Reg查询脚本.我正在尝试更改代码,以允许它在DisplayName中搜索"VNC",在相应的UninstallPath中的{}之间删除文本,然后运行msiexec.exe /qn /x {package-identifier}静默卸载程序.

I started with the script from this StackOverflow solution Reg Query script for batch script . I am trying to alter the code to allow it to search for "VNC" in the DisplayName, strip the text between the {} in its corresponding UninstallPath, and then run msiexec.exe /qn /x {package-identifier} to silently uninstall the program.

运行以下代码时,它将检索几个在DisplayName中不包含"VNC"的程序.问题似乎出在以下代码行中:

When running the following code, it retrieves several programs that do not contain "VNC" in their DisplayName. The issue appears to lie in the following line of code:

if not "x!str1:VNC=!"=="x!str1!" (

此行代码的来源:我尝试了以下发现的替代代码,但它也列出了太多软件包,并且没有正确选择名称中只有"VNC"的软件包.

I have tried the following code found at as an alternative, but it also listed way too many packages and did not properly pick only packages with "VNC" in their names.

set str1 = !product[%counter%]!
echo str1 > temp.dat
findstr /c:"VNC" "temp.dat" >nul 2>&1
if %errorlevel% == 0 (

注意:许多echo命令仅用于测试目的.

Note: Many of the echo commands are there simply for testing purposes.

 @echo off
    setlocal
    set regVar=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
    set excluded=/c:" Microsoft" /c:"MDOP" /c:"Dell" 
    set count=1
    for /f "tokens=1,2,*" %%a in ('Reg Query %regVar% /S ^| findstr "DisplayName HKEY_ UninstallString"') do (
        setlocal EnableDelayedExpansion
        for %%n in (!count!) do (
            ENDLOCAL
            SET HKEY=Y
            IF "%%a"=="DisplayName" SET "HKEY="&set product[%%n]=%%c
            IF "%%a"=="UninstallString" SET "HKEY="&IF NOT DEFINED uninstall[%%n] set uninstall[%%n]=%%c
            IF "%%a"=="QuietUninstallString" SET "HKEY="&IF NOT DEFINED uninstall[%%n] set uninstall[%%n]=%%c
        IF DEFINED hkey IF DEFINED product[%%n] IF defined uninstall[%%n] SET /a count+=1&SET "hkey="
        IF DEFINED hkey set "product[%%n]="&SET "uninstall[%%n]="
        )
    )

    IF NOT DEFINED product[%count%] SET "uninstall[%count%]="&SET /a count-=1
    IF NOT DEFINED uninstall[%count%] SET "product[%count%]="&SET /a count-=1
    ECHO %count% entries found
    set counter=%count%
    pause
    @setlocal enableextensions enabledelayedexpansion
    :while
    if %counter% GTR 0 (
        set str1 = !product[%counter%]!
        echo str1
        if not "x!str1:VNC=!"=="x!str1!" (
            echo !product[%counter%]!
            echo !uninstall[%counter%]!
            set UninstallString=!uninstall[%counter%]!
            echo !UninstallString!
            set RunUninstall = ""
            for /f "tokens=2 delims={}" %%A in ("!uninstall[%counter%]!") do echo %%A REM set RunUninstall="msiExec.exe /qn /x{%%A}" (commented for testing purposes)
            call !RunUninstall!
            set /a counter-=1
            GOTO While
            ) Else (
            set /a counter-=1
            GOTO While
            )
        )

输出摘要:

Intel(R) Network Connections 15.7.176.0
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86418051F0}
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86418051F0}
26A24AE4-039D-4CA4-87B4-2F86418051F0 REM set RunUninstall="msiExec.exe /qn /x{26
A24AE4-039D-4CA4-87B4-2F86418051F0}"
ECHO is off.
Microsoft .NET Framework 4.5.2
MsiExec.exe /X{26784146-6E05-3FF9-9335-786C7C0FB5BE}
MsiExec.exe /X{26784146-6E05-3FF9-9335-786C7C0FB5BE}
26784146-6E05-3FF9-9335-786C7C0FB5BE REM set RunUninstall="msiExec.exe /qn /x{26
784146-6E05-3FF9-9335-786C7C0FB5BE}"
ECHO is off.
Lenovo ThinkVantage Toolbox
MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
23170F69-40C1-2702-0920-000001000000 REM set RunUninstall="msiExec.exe /qn /x{23
170F69-40C1-2702-0920-000001000000}"
ECHO is off.
OSFMount v1.5
MsiExec.exe /I{1B8ABA62-74F0-47ED-B18C-A43128E591B8}
MsiExec.exe /I{1B8ABA62-74F0-47ED-B18C-A43128E591B8}
1B8ABA62-74F0-47ED-B18C-A43128E591B8 REM set RunUninstall="msiExec.exe /qn /x{1B
8ABA62-74F0-47ED-B18C-A43128E591B8}"

更新: 我修改了wOxxOm提供的代码,以尝试使TightVNC成为异常并安静地将其卸载.由于TightVNC的注册表中没有QuietUninstallString,因此我尝试通过使用msiexec.exe /qn /x选项来完成此操作.但是,它始终抛出以下错误:

UPDATE: I modified the code provided by wOxxOm in an attempt to make an exception for TightVNC and uninstall it quietly. Since there is not a QuietUninstallString in the registry for TightVNC, I am attempting to accomplish this by utilizing the msiexec.exe /qn /x options. However it keeps throwing the following error:

else was unexpected at this time.

下面列出了我对wOxxOm代码的修改后的版本:

My modified version of wOxxOm's code is listed below:

@echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
    for /f "delims=" %%b in ('
        reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
            /s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
    ') 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 product=%%d
                    if "x!product:TightVNC=!"=="x!product!" (
                        for /f "tokens=2 delims={}" %%A in ("!Uninstall!") do (
                        echo Found !product!
                        Running msiExec.exe /qn /x{%%A} REM (COMMENTED FOR TESTING)
                        REM CALL msiExec.exe /qn /x{%%A}
                        )
                    ) else (
                    echo Found %%d
                    echo Running !Uninstall! (COMMENTED FOR TESTING)
                    rem call !Uninstall!
                    echo.
                    )
                )
           )
        )
    )

推荐答案

  1. 请勿在set str1=!product[%counter%]!
  2. 中的=周围使用空格
  3. 代替if %counter% GTR 0 (无效,并使循环变为无限)
    循环后使用if %counter%==0 goto done:done标签
  4. 带有引号的您无需在比较中添加x.
  5. 不要忘记64位Windows,也要检查Wow6432Node树(请参见替代代码)
  1. Don't use spaces around = in set str1=!product[%counter%]!
  2. Instead of if %counter% GTR 0 ( which doesn't work and makes the loop infinite)
    use if %counter%==0 goto done and a :done label after the loop
  3. With quotes you don't need to add x to the comparison.
  4. Don't forget about 64-bit Windows and check Wow6432Node tree too (see the alternative code)


:while
if %counter%==0 goto done
set str1=!product[%counter%]!
if not "!str1:VNC=!"=="!str1!" (
    ..............................
    rem call !RunUninstall!
)
set /a counter-=1
GOTO While

:done


或者,通过仅在DisplayName中仅列出具有VNC的产品,然后获取UninstallString:


Alternatively you can make the batch file much faster (the "arrays" are slow in case of many strings) by listing only the products with VNC in DisplayName first and then fetch the UninstallString:

@echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
    for /f "delims=" %%b in ('
        reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
            /s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
    ') 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 echo Found %%d
                echo Running !Uninstall! (COMMENTED FOR TESTING)
                rem call !Uninstall!
                echo.
            )
        )
    )
)
pause

这篇关于如何自动卸载所有包含"VNC"的程序.在其显示名称中使用批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆