批处理reg查询UninstallString

时间:2013-07-16 09:36:29

标签: windows batch-file for-loop registry findstr

我想搜索这些注册表项(可能带有“REG Query”)

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

和“Find”或“FindStr”特定变量并显示结果(echo?)

这个脚本有点像我想要实现的目标**:**

@ECHO OFF
:: Windows 7 only
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
VER | FINDSTR /R /E /C:" 6\.[01]\.[0-9\.]*\]" >NUL || GOTO Syntax

:: Check command line arguments
IF     "%~1"=="" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
ECHO "%~1" | FINDSTR /R /C:"[/?]" >NUL && GOTO Syntax

SETLOCAL ENABLEDELAYEDEXPANSION
SET Count=0
FOR /F "tokens=*" %%A IN ('REG Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "%~1" /D /S 2^>NUL ^| FINDSTR /R /B /C:"HKEY_"') DO (
    REG Query "%%~A" /F DisplayName /V /E | FINDSTR /R /I /C:" DisplayName .* .*%~1" >NUL 2>&1
    IF NOT ERRORLEVEL 1 (
        SET /A Count += 1
        FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F DisplayName    /V /E 2^>NUL ^| FIND /I " DisplayName "')     DO ECHO Program Name      = %%C
        FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F DisplayVersion /V /E 2^>NUL ^| FIND /I " DisplayVersion "')  DO ECHO Program Version   = %%C
        FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F InstallDate    /V /E 2^>NUL ^| FIND /I " InstallDate "')     DO (
            SET InstallDate=%%C
            ECHO Install Date      = !InstallDate:~0,4!-!InstallDate:~4,2!-!InstallDate:~6!
        )
        FOR /F "tokens=7 delims=\" %%B IN ("%%~A") DO ECHO Unique Identifier = %%B
        FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO ECHO Uninstall String  = %%C
        ECHO.
    )
)

WMIC.EXE Path Win32_Processor Get DataWidth 2>NUL | FIND "64" >NUL
IF ERRORLEVEL 1 (
    ECHO.
    ECHO %Count% programs found
) ELSE (
    SET Count32bit=0
    FOR /F "tokens=*" %%A IN ('REG Query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /F "%~1" /D /S 2^>NUL ^| FINDSTR /R /B /C:"HKEY_"') DO (
        REG Query "%%~A" /F DisplayName /V /E | FINDSTR /R /I /C:" DisplayName .* .*%~1" >NUL 2>&1
        IF NOT ERRORLEVEL 1 (
            SET /A Count32bit += 1
            FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F DisplayName    /V /E 2^>NUL ^| FIND /I " DisplayName "')     DO ECHO Program Name      = %%C
            FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F DisplayVersion /V /E 2^>NUL ^| FIND /I " DisplayVersion "')  DO ECHO Program Version   = %%C
            FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F InstallDate    /V /E 2^>NUL ^| FIND /I " InstallDate "')     DO (
                SET InstallDate=%%C
                ECHO Install Date      = !InstallDate:~0,4!-!InstallDate:~4,2!-!InstallDate:~6!
            )
            FOR /F "tokens=7 delims=\" %%B IN ("%%~A") DO ECHO Unique Identifier = %%B
            FOR /F "tokens=2*" %%B IN ('REG Query "%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO ECHO Uninstall String  = %%C
            ECHO.
        )
    )
    ECHO.
    ECHO     %Count% 64-bit programs and !Count32bit! 32-bit programs found
)

ENDLOCAL
GOTO:EOF


:Syntax
ECHO.
ECHO GetUninstall.bat,  Version 2.00 for Windows Vista and later
ECHO List or search uninstall command lines
ECHO.
ECHO Usage:    GETUNINSTALL.BAT  "filter"
ECHO.
ECHO Where:    "filter"    narrows down the search result to programs whose
ECHO                       uninstall data contains the string "filter"
ECHO.
ECHO Example:  GETUNINSTALL.BAT "Adobe Reader"
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

:: Set return code for Windows NT 4 or later
IF "%OS%"=="Windows_NT" COLOR 00

问题:

它确实显示所有找到的卸载字符串,如“%1”所述,并与“Windows 7 PRO SP1”一起使用但不适用于“Windows XP Pro SP3”,我似乎无法在XP上使用它...

REM IF NOT "%OS%"=="Windows_NT" GOTO Syntax
REM VER | FINDSTR /R /E /C:" 6\.[01]\.[0-9\.]*\]" >NUL || GOTO Syntax

/ \当然不是那么容易; - p / \

2 个答案:

答案 0 :(得分:1)

这几乎与预期一致:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
REM SETLOCAL ENABLEDELAYEDEXPANSION
CLS
echo.Tim: uninstall (old/previous) Adobe ShockWave/Flash...

CALL "\\dc01.lksvdd.local\netlogon\Log_management.bat" >nul 2>&1

:: RUN_Once
IF NOT EXIST "%systemroot%\LOGS\LKSVDD\Tim\uninstall_(shockwave)flash_12+11.txt" (GOTO Uninstall_Adobe-Addons) ELSE GOTO:EOF
GOTO:EXIT


:Uninstall_Adobe-Addons
rem uninstall old adobe shockwave/flash installations; new adobe shockwave/flash setup pushed with GPO...

wmic product where "name like 'Adobe Flash Player 11%%'" call uninstall /nointeractive      > "%systemroot%\LOGS\LKSVDD\Tim\uninstall_(shockwave)flash_12+11.txt" 2>&1
wmic product where "name like 'Adobe Flash Player 10%%'" call uninstall /nointeractive          > "%systemroot%\LOGS\LKSVDD\Tim\uninstall_(shockwave)flash_12+11.txt" 2>&1
wmic product where "name like 'Adobe Shockwave Player 12%%'" call uninstall /nointeractive >> "%systemroot%\LOGS\LKSVDD\Tim\uninstall_(shockwave)flash_12+11.txt" 2>&1
wmic product where "name like 'Adobe Shockwave Player 11%%'" call uninstall /nointeractive     >> "%systemroot%\LOGS\LKSVDD\Tim\uninstall_(shockwave)flash_12+11.txt" 2>&1

rem Abort possible ShutDown..................
"%windir%\system32\shutdown.exe" /a >nul 2>&1


:EXIT
ECHO #runTimeStamp -4- "uninstall_adobe-addons.cmd" [%DATE% %TIME%] EOF >> "%systemroot%\LOGS\LKSVDD\Tim\uninstall_(shockwave)flash_12+11.txt" 2>&1

rem echo.
REM script date: 16-07-2013
rem echo.
rem PAUSE
ENDLOCAL
GOTO:EOF

“Adobe Flash Player”确实被卸载/删除,但“Adobe Flash Player”没有? 这是正确的程序描述,但由于某种原因它没有被识别,但不确定为什么呢?

答案 1 :(得分:0)

你为什么要对自己这样做?您在VBScript中的代码:

Set re = New RegExp
re.Pattern = "(\d{4})(\d{2})(\d{2})"

Set wmi = GetObject("winmgmts://./root/cimv2")
For Each p In wmi.ExecQuery("SELECT * FROM Win32_Product")
  WScript.Echo "Program Name      = " & p.Name & vbNewLine _
    & "Program Version   = " & p.Version & vbNewLine _
    & "Install Date      = " & re.Replace(p.InstallDate, "$1-$2-$3") & vbNewLine _
    & "Unique Identifier = " & p.IdentifyingNumber
Next

您在PowerShell中的代码:

gwmi Win32_Product | select Name, Version, InstallDate, IdentifyingNumber | % {
  "Program Name      = " + $_.Name
  "Program Version   = " + $_.Version
  "Install Date      = " + ($_.InstallDate -replace '(\d{4})(\d{2})(\d{2})',
                                                    '$1-$2-$3')
  "Unique Identifier = " + $_.IdentifyingNumber
}

提取卸载字符串需要更多的努力,但无论如何你还需要什么呢?如果要卸载程序:WMI类提供Uninstall方法。