script to modify add reg key

Microsoft Windows xp professional w/serv...
July 1, 2010 at 00:28:39
Specs: Windows XP
Hi, I am a desktop support person, would greatly appreciate if someone point me to right direction to create a batch file or vb script to add a new trusted location in registry:
For multiple users I need to find how many current locations exist and add a new one.
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security\Trusted Locations\Location0.......Location5
etc.
If user has only 5 then add \Location6 (with subkeys, of course).
Thank you for your time.
Hetura

See More: script to modify add reg key

Report •

#1
July 1, 2010 at 15:37:13
Are you running Windows Server 2003 or 2008? If so you can just add the registry mods to the GPO.

http://technet.microsoft.com/en-us/...


Report •

#2
July 1, 2010 at 16:41:30
Thanks ace_omega,
Not sure if I understood. Do you mean, add registry mods in GPO and push to users via SCCM.
Script to be run and add on User's computer with XP SP2/3.

Report •

#3
July 1, 2010 at 16:54:35
See if you have the REG command available to you. A batch or script could
be written around REG QUERY followed by REG ADD.

REG QUERY:

19:48:51>reg query /?

Console Registry Tool for Windows - version 3.0
Copyright (C) Microsoft Corp. 1981-2001.  All rights reserved


REG QUERY KeyName [/v ValueName | /ve] [/s]

  KeyName    [\Machine\]FullKey
    Machine - Name of remote machine,  omitting defaults to the current machine
         Only HKLM and HKU are available on remote machines
    FullKey  - in the form of ROOTKEY\SubKey name
         ROOTKEY  [ HKLM | HKCU | HKCR | HKU | HKCC ]
         SubKey  - The full name of a registry key under the selected ROOTKEY
  /v  query for a specific registry key
         ValueName  - The name, under the selected Key, to query
         if omitted, all values under the Key are queried
  /ve query for the default value or empty value name <no name>
  /s  queries all subkeys and values

Examples:

  REG QUERY HKLM\Software\Microsoft\ResKit /v Version
    Displays the value of the registry value Version

  REG QUERY HKLM\Software\Microsoft\ResKit\Nt\Setup /s
    Displays all subkeys and values under the registry key Setup

REG ADD:

19:49:11>reg add /?

Console Registry Tool for Windows - version 3.0
Copyright (C) Microsoft Corp. 1981-2001.  All rights reserved


REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [/d Data] [/f]

  KeyName  [\\Machine\]FullKey
           Machine  Name of remote machine - omitting defaults to the current
                    machine Only HKLM and HKU are available on remote machines
           FullKey  ROOTKEY\SubKey
           ROOTKEY  [ HKLM | HKCU | HKCR | HKU | HKCC ]
           SubKey   The full name of a registry key under the selected ROOTKEY

  /v       The value name, under the selected Key, to add

  /ve      adds an empty value name <no name> for the key

  /t       RegKey data types
           [ REG_SZ    | REG_MULTI_SZ  | REG_DWORD_BIG_ENDIAN    |
             REG_DWORD | REG_BINARY    | REG_DWORD_LITTLE_ENDIAN |
             REG_NONE  | REG_EXPAND_SZ ]
           If omitted, REG_SZ is assumed

  /s       Specify one charactor that you use as the separator in your data
           string for REG_MULTI_SZ. If omitted, use "\0" as the separator

  /d       The data to assign to the registry ValueName being added

  /f       Force overwriting the existing registry entry without prompt

Examples:

  REG ADD \\ABC\HKLM\Software\MyCo
    Adds a key HKLM\Software\MyCo on remote machine ABC

  REG ADD HKLM\Software\MyCo /v Data /t REG_BINARY /d fe340ead
    Adds a value (name: Data, type: REG_BINARY, data: fe340ead)

  REG ADD HKLM\Software\MyCo /v MRU /t REG_MULTI_SZ /d fax\0mail
    Adds a value (name: MRU, type: REG_MUTLI_SZ, data: fax\0mail\0\0)

  REG ADD HKLM\Software\MyCo /v Path /t REG_EXPAND_SZ /d %%systemroot%%
    Adds a value (name: Path, type: REG_EXPAND_SZ, data: %systemroot%)
    Notice:  Use the double percentage ( %% ) inside the expand string

Insomniac at large


Report •

Related Solutions

#4
July 1, 2010 at 19:40:46
Thanks Orangeboy,
I did explore, REG Query/Add option, But I could not understand how I could pass the last reg key name, so a new one can be added. E.g
Reg Query HKCU\software\Myco1
Reg Query HKCU\software\Myco2
Reg Query HKCU\software\Myco3
So, if last key was Myco3 then add a new one
Reg ADD HKCU\software\Myco4
Thank you for your time.
Regards

Report •

#5
July 1, 2010 at 22:36:16
Here ya go:

::21829.bat
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET my_key=HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security\Trusted Locations

FOR /F "delims=" %%a IN ('REG QUERY "%my_key%" /s^|FIND /I "%my_key%"') DO (
    SET last_key=%%a
)

SET /A last_char=4
:Find_Number
SET last_num=!last_key:~-%last_char%!
ECHO !last_num!|FINDSTR /B /R "[0-9]" >NUL
IF !ERRORLEVEL! GTR 0 (
    SET /A last_char-=1
    IF !last_char! EQU 0 (
        SET /A last_num=0
        GOTO Only_Key
    )
    GOTO Find_Number
)
:Only_Key

SET /A next_num=%last_num%+1
SET next_key=!last_key:~0,-%last_char%!%next_num%
REM REG ADD %next_key%
REM REG ADD %next_key% /v "Value Name" /t REG_SV /d "New Value"
EXIT /B

The first FOR /F loops through all the keys that look like my_key,
and sets the variable named last_key to it's value. If another key
is found, last_key is replaced with that key's name. Repeats until
it finds no more.

The next part grabs the last for characters of the last key's name,
then tests it using FINDSTR's regex capabilities to see if the first
character is a number. If it's not, the number of characters checked
is shrunk by 1, and tested again. This is repeated until a number is
found, or the number of characters to check becomes zero. If the
number of characters to check is zero, the last number will be implied
as "zero".

Now that we have a known numbered key, some math is performed
to increment that number by one, which will become a part of the
next key's name. The next key's name is derived from the last key's
name, but shortened at the end by the number of characters that were
numbers, then concatenated with the number that just had math
performed on it.

At this point, the "%next_key% is ready to be added into the registry
with whatever values you need to add. Simple, huh?

Insomniac at large


Report •

Ask Question