注册查询脚本批处理脚本 [英] Reg Query script for batch script

查看:115
本文介绍了注册查询脚本批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着问这一次,但我认为有一些混乱,所以我要去尝试这一新的。这里是code:

 设置regVar = HKLM \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\卸载
集中排除=微软MDOP戴尔
设置计数= 0
FOR / F令牌= 2 *%%一个在('注册查询%regVar%/ S ^ |找到显示名称^ | FINDSTR / V排除%%')做(
    设置/计数+ = 1
    SETLOCAL EnableDelayedExpansion
    对于%%的n(!算!)做(
        ENDLOCAL
        集产品[%% N] = %% b
        回声%% n和%标签%%% b
        回声%%一
    )

这样做是经过所有的卸载注册表,然后将每个DisplayName项到一个数组所谓的产品[],然后打印当前数量的结果是我上环到屏幕上的菜单。那么,什么你能做的就是在号码类型,并且它的数组中返回显示名称:

因此​​,这因为它代表过滤掉一切微软MDOP和戴尔

我想要它做的也期待在关键为该显示名称,并返回称为UninstallString另一个关键并分配到数组uninstallprod []与同一关联的索引什么。我遇到的问题是,我不能使用的语句找出如何可以查询像您可以在SQL

 选择UninstallString从注册表,其中显示名称=程序名称

与节目名称作为字符串从显示名称返回您选择,或者有一个单一的查询,让我既过滤掉DisplayNames像你上面看到,也包括在同一语句中的UninstallString值。与尝试任何其他方式的问题是我无法过滤,没有它结束了与指数完全错误一举两得阵列(它会跳过的显示名称,但仍然分配uninstallstring因为它不是让我既过滤)

更新:

在这一点上我把它拉回来既显示名称和卸载的字符串,但它无法正常推进计数器。在它的当前状态,它分配2个变量,密钥的名称(%% a)至用于分配和比较关键是什么,以及键(%% C)的值,要么是程序的名称或卸载路径(字符串)在脚本后用于卸载。那么,最终情况是,我最终跳过的号码清单,因为柜台是越来越先进,即使有被打印到列表中没有显示名称。这里是code现在我有工作。

 关闭@echo
设置选项卡=
设置regVar = HKLM \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\卸载
集中排除=微软MDOP戴尔
集数= 1
FOR / F令牌= 1,2,*%%一个在('注册查询%regVar%/ S ^ | FINDSTR显示名称UninstallString^ | FINDSTR / V排除%%')做(
    SETLOCAL EnableDelayedExpansion
    对于%%的n(!算!)做(
        ENDLOCAL
        如果%%一个==显示名称集产品[%% N] = %% C和;回声%% N%标签%%%Ç
        如果%%一个==UninstallString如果定义的产品[%% N]设置卸载[%% N] == %% C和;设置/计数+ = 1
    )

只是作为一个例子,我有我的名单上崇高的文本2.0.1位和64位HP CIO组件安装程序。什么结束了发生的事情是,我得到一个列表如下:

  1。崇高文字2.0.1
3. 64位HP CIO组件安装程序

所以由于某些原因,计数器是越来越先进的过去2即使没有商品的[2]被定义。而在32位版本我有5项显示出来,其中一人是空白,打印这样的:

  1。的Adobe Flash Player的ActiveX 11
2.谷歌浏​​览器
3. TeamViewer 8
4。
4.谷歌更新助手。


解决方案

您就需要让这两个UninstallString和显示名称,通过最初的门,所以更改

FOR / F令牌= 2 *%%一个在('注册查询%regVar%/ S ^ |找到显示名称......

FOR / F令牌= 1,2,*%%一个在('注册查询%regVar%/ S ^ | FINDSTR显示名称UninstallString...

这将设置 %% A 显示名称 UninstallString %%ç来的数据值必需的。

,那么你必须要处理的顺序

DISPLAYNAME点¯x结果
DISPLAYNAMEÿ结果
卸载是

DISPLAYNAMEÿ结果
卸载ÿ结果
卸载ž

这实际上是不是特别辛苦。

您已经设置产品[N] - 你需要为每个做到这一点%% A == DISPLAYNAME。
重复卸载[N] - 为每一位做到这一点 %% A == UninstallString但只有当产品[n]被定义

 如果%%一个==UninstallString如果定义了产品[%% N]卸载设置[%% N] = %% C和;设置/计数+ = 1

取数递增的SETLOCAL出来之前,因为它需要做的事情只有当你有一个匹配的对产品和卸载的;您的初始值更改为1(除非你喜欢从零算起)

和祈祷,你没有得到一个序列

DISPLAYNAMEÿ结果
卸载ž

现在,如果你得到这个序列,只需找到一个方便的字符串还通过门让 - 样,或许HKEY_,如果 %% A 开始 HKEY_,那么集产品展示[%% N] =来清除任何DISPLAYNAME出现没有uninstallstring。

您可能甚至想改变周围一点点。

如果您设置


  • 对第一DISPLAYNAME产品

  • 对先卸载卸载字符串(即仅如果没有定义反安装[%% N]

  • 当HKEY_出现,如增加产品都[%% N]柜台
    卸载[%% n]的定义,否则集产品[%% n]和卸载[%% N]
    为[无]

然后卸载和显示名称的顺序是不相关的,但你需要在循环之后重复HKEY_试验取最后实例的照顾......


补遗解决讨论一些问题。

  @ECHO OFF
SETLOCAL
设置regVar = HKLM \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\卸载
集中排除= / C:微软/ C:MDOP/ C:戴尔
集数= 1
FOR / F令牌= 1,2,*%%一个在('注册查询%regVar%/ S ^ | FINDSTR显示名称HKEY_ UninstallString^ | FINDSTR / v%的除外%')做(
    SETLOCAL EnableDelayedExpansion
    对于%%的n(!算!)做(
        ENDLOCAL
        SET HKEY = Y
        IF%%一个==显示名称SETHKEY =&安培;集产品[%% N] = %%Ç
        IF%%一个==UninstallStringSETHKEY =&放大器;如果没有定义卸载[%% N]卸载设置[%% N] = %%Ç
        IF%%一个==QuietUninstallStringSETHKEY =&放大器;如果没有定义卸载[%% N]卸载设置[%% N] = %%Ç
    如果定义HKEY如果定义产品[%% N]如果定义卸载[%% N] SET /计数+ = 1&安培; SETHKEY =
    如果定义HKEY集的产品[%% N] =&放大器; SET卸载[%% N] =
    )
)::
::最新条目可能是不完整
::
如果没有定义的产品[%计%] SET卸载[%计%] =&放大器; SET / A count- = 1
如果没有定义卸载[%计%] SET产品[%计%] =&放大器; SET / A count- = 1发现ECHO%数量%的条目
SET PROD
SET unins

请注意,由于 HKEY 通过门现在是允许的,但它的测试疼痛行开始HKEY ,如果不是字符串显示名称之一,UninstallString或QuietUninstallString,那么它​​是presumed是HKEY。 如果定义适用于运行时presence /不存在一个变量,它允许容易的 - 呃,科夫 - 编程


这是一个阵列显示数据

  FOR / L %%我在(1,1,计数%%),DO(
CALL ECHO %%我。 %%产品[%% I] %%


附加数据20130419-1756Z

  @echo OFF
SETLOCAL
设置选项卡=
设置regVar = HKLM \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\卸载
集中排除=微软MDOP戴尔
FOR / Fdelims =%% i的('注册查询%regVar%/ S ^ |寻找HKEY_')DO(
 FOR %% S IN(DN我们曲PU DN UT)DO SET%% S =
 FOR / F令牌= 1,2,* delims =%% S IN('注册查询%%我/ S')DO(
  如果/我%%的==显示名称SET /一个DN + = 1&安培; SET DN = %%ü
  如果/我%%的==UninstallstringSET /美国+ = 1&安培; SET UT = %%ü
  如果/我%%的==quietUninstallstringSET /临朐+ = 1
  如果/我%%的==发行人设置PU ​​= %%ü
 )
 FOR %% S IN(DN我们曲)DO SET / A %% S + = 0
 CALL:报告
)GOTO:EOF:报告
SET / A土族= US +去
ECHO%DN%%的美国%%曲%%土族%%PU%%DN%%UT%
GOTO:EOF

此过程应显示有趣的细节。

报告列


  1. 的'数显示名已经找到

  2. 的'Uninstallstring的发现
  3. 号码
  4. 的'quiteuninstallstring的发现

  5. 共计previous的2列

  6. 出版商字符串 - 如果找到

  7. 显示名称 - 如果找到

  8. 卸载文本 - 如果找到

我建议你用这个数据做你的过滤和分配给您的阵列 - 内循环处理一个单一的卸载项。选择所需的部件并将它们添加或不给您的数组。

I tried asking this once, but I think there was some confusion so I'm going to try this fresh. Here is the code:

set regVar=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
set excluded=Microsoft MDOP Dell 
set count=0
for /f "tokens=2,*" %%a in ('Reg Query %regVar% /S^|find " DisplayName" ^| findstr /v "%excluded%"') do (
    set /a count+=1
    setlocal EnableDelayedExpansion
    for %%n in (!count!) do (
        endlocal
        set product[%%n]=%%b
        echo  %%n.%tab%%%b
        echo  %%a
    )   
)

What this does is go through all uninstall registries and then adds each DisplayName key into an array called product[] and then prints the result for the current number it's on i the loop to the screen for a menu. What you can then do is type in the number, and it returns the Display name in that array:

So this as it stands filters out everything microsoft, mdop, and dell

What I want it to do is ALSO look in that key for that DisplayName and return another key called UninstallString and assign that to the array uninstallprod[] with the same associated index. The issue I'm running into is that I cant figure out how to either query using a statement like you can in sql

Select UninstallString from REGISTRY where DisplayName="Program name"

with program name being the string returned from the DisplayName you selected, or to have a SINGLE query that will allow me to both filter out the DisplayNames like you see above and also include the UninstallString value in the same statement. The problem with trying it any other way is I can't filter and do both arrays without it ending up with indexes completely wrong (it'll skip a display name but still assign the uninstallstring because its not letting me filter both)

UPDATE:

At this point I have it pulling back both a DisplayName and Uninstall string, but it's not advancing the counter correctly. In it's current state, it assigns 2 variables, the name of the key (%%a) to use for assigning and comparing what the key is, and the value of the key (%%c) which is either the name of the program or the uninstall path (string) to use for uninstallation later in the script. So what ends up happening is that I end up with a list of skipped numbers because the counter is getting advanced even when there is no displayname being printed to the list. Here is the code I am now working with.

@echo off
set tab=    
set regVar=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
set excluded=Microsoft MDOP Dell 
set count=1
for /f "tokens=1,2,*" %%a in ('Reg Query %regVar% /S^|findstr "DisplayName UninstallString" ^| findstr /v "%excluded%"') do (
    setlocal EnableDelayedExpansion
    for %%n in (!count!) do (
        endlocal
        if "%%a"=="DisplayName" set product[%%n]=%%c&echo %%n.%tab%%%c
        if "%%a"=="UninstallString" if defined product[%%n] set uninstall[%%n]==%%c&set/a count+=1
    )
)

Just as an example, I have Sublime Text 2.0.1 and 64 bit HP CIO Components Installer on my list. What ends up happening is I get a list like this:

1.  Sublime Text 2.0.1
3.  64 bit HP CIO Components Installer

So for some reason the counter is getting advanced past 2 even though no product[2] is being defined. And in the 32 bit version I have 5 entries showing up, one of them is blank, which prints like this:

1.  Adobe Flash Player 11 ActiveX
2.  Google Chrome
3.  TeamViewer 8
4.  
4.  Google Update Helper.

解决方案

You'd need to let both "UninstallString" and "Displayname" through the initial gate, so change

for /f "tokens=2,*" %%a in ('Reg Query %regVar% /S^|find " DisplayName" "...

to

for /f "tokens=1,2,*" %%a in ('Reg Query %regVar% /S^|FINDSTR "Displayname UninstallString"...

Which will set %%a to Displayname or UninstallString and %%c to the data value required.

THEN you have to deal with the sequence

Displayname x
Displayname y
Uninstall y

or

Displayname y
Uninstall y
Uninstall z

Which actually isn't particularly hard.

You are already setting Product[n] - you need to do this for every %%a==Displayname. Repeat for Uninstall[n] - do this for every %%a==UninstallString BUT only if Product[n] is defined.

IF "%%a"=="UninstallString" if defined Product[%%n] set uninstall[%%n]=%%c&set/a count+=1

take the count-increment before the SETLOCAL out, as it needs to be done only if you have a matching pair of PRODUCT and UNINSTALL; change your initial value to 1 (unless you like counting from zero)

and pray that you don't get a sequence

Displayname y
Uninstall z

Now, if you DO get this sequence, simply find a convenient string to ALSO let through the gate - like, perhaps "HKEY_" and if %%a begins "HKEY_" then set "Product[%%n]=" to clear any Displayname appearing without an uninstallstring.

You may even want to change that around a little.

If you set

  • Product against the first Displayname
  • uninstall against the first uninstall string (ie. only if not defined unistall[%%n])
  • When a HKEY_ appears, increment the counter if both product[%%n] and uninstall[%%n] are defined, else set product[%%n] and uninstall[%%n] to [nothing]

then the sequence of uninstall and displayname is not relevant, although you'd need to repeat the HKEY_ test after the loop to take care of the very last instance...


Addendum for solving some problems discussed

@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" ^| findstr /v %excluded%') 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]="
    )   
)

::
:: Last entry may not be complete
::
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 prod
SET unins

Note that since HKEY is now allowed through the gate, but it's a pain to test line beginning HKEY, if it's NOT one of the strings "Displayname", "UninstallString" or "QuietUninstallString" then it's presumed to be HKEY. IF DEFINED works on the RUN-TIME presence/absence of a variable which permits easy - er, koff - programming.


Display data from an "array"

FOR /l %%i IN (1,1,%count%) DO (
CALL ECHO %%i. %%product[%%i]%%
)


Additional data 20130419-1756Z

@echo OFF
setlocal
set tab=    
set regVar=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
set excluded=Microsoft MDOP Dell 
FOR /f "delims=" %%i IN ('Reg Query %regVar% /S^|find "HKEY_"') DO (
 FOR %%s IN (dn us qu pu dn ut) DO SET "%%s="
 FOR /f "tokens=1,2,*delims= " %%s IN ('Reg Query "%%i" /S') DO (
  if /i "%%s"=="Displayname" SET /a dn+=1&SET dn=%%u
  if /i "%%s"=="Uninstallstring" SET /a us+=1&SET ut=%%u
  if /i "%%s"=="quietUninstallstring" SET /a qu+=1
  if /i "%%s"=="publisher" SET pu=%%u
 )
 FOR %%s IN (dn us qu) DO SET /a %%s+=0
 CALL :report
)

GOTO :eof

:report
SET /a tus=us+qu
ECHO %dn%   %us%   %qu%   %tus%   %pu% %dn% %ut%
GOTO :eof

This procedure should show interesting details.

The report columns are

  1. Number of 'displayname's found
  2. Number of 'Uninstallstring's found
  3. number of 'quiteuninstallstring's found
  4. total of previous 2 columns
  5. publisher string - if found
  6. displayname - if found
  7. uninstall text - if found

I'd suggest you use this data to do your filtering and assignment to your array - the inner loop is dealing with a single uninstall item. Select the required parts and add them or don't to your arrays.

这篇关于注册查询脚本批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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