Solved

Registry script to check registry entries and add an entry in

Posted on 2014-04-08
9
649 Views
Last Modified: 2014-04-17
Hiya,

I am trying to find a bat script to do the following

1. The script searches through the registry for any values in the keys below which relate to Excel addins and if there are already values there, ignore them and then add 2 Excel Addins to the next available OPENX keys

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN1
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN2
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN3
HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options\OPEN4

I had some help from someone on here to use a script which kind of reversed this idea and checked through the OPEN keys to see if a value existed and if so deleted it which is below but didn't know if something similar could be used for this query

@echo off
setlocal enabledelayedexpansion
set RemoveList="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam" "D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"
for %%a in ("OPEN" "OPEN1" "OPEN2") do (
	set Data=
	for /f "tokens=2*" %%o in ('reg.exe query "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options" /v "%%~a" 2^>NUL') do set Data=%%~p
	for %%r in (%RemoveList%) do (
		if /i "!Data!"=="%%~r" (
			ECHO reg.exe delete "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options" /v "%%~a" /f
		)
	)
)

Open in new window

0
Comment
Question by:rhiancohen
  • 4
  • 3
  • 2
9 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Can you ellaborate on what would be the criteria to me "any values in the keys below which relate to Excel addins" ?

~bp
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Try the script below.
Set any number of variables "AddList[x]" to the Addins you want to add.
Set the variable LastIndex to the last index you want to scan.
Set the variable Quiet to 1 to suppress any output except error messages.
@echo off
setlocal enabledelayedexpansion
set AddinList[1]="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam"
set AddinList[2]="D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"
set Quiet=0
set LastIndex=10
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
for /f "tokens=1* delims==" %%a in ('set AddinList[') do (
	if %Quiet%==0 echo Processing '%%b'
	set Value=
	set Data=%%b
	set Data=!Data:"=\"!
	for /l %%i in (0, 1, %LastIndex%) do (
		if "!Value!"=="" (
			if %%i==0 (set TestValue=OPEN) else (set TestValue=OPEN%%i)
			if %Quiet%==0 echo Checking !TestValue! ...
			reg.exe query "%Key%" /v "!TestValue!" >NUL 2>&1
			if errorlevel 1 (
				if %Quiet%==0 echo ... free.
				set Value=!TestValue!
			) else (
				if %Quiet%==0 echo ... already used.
			)
		)
	)
	if "!Value!"=="" (
		echo Unable to find an empty index in the range of 0..%LastIndex%
		goto :eof
	) else (
		reg.exe add "%Key%" /v "!Value!" /t REG_SZ /d "!Data!" /f >NUL
		if errorlevel 1 (
			echo ERROR: Could not create value '!Value!' with data '!Data!' at '%Key%'
		) else (
			if %Quiet%==0 echo Value successfully added.
		)
	)
)

Open in new window

0
 
LVL 1

Author Comment

by:rhiancohen
Comment Utility
To elaborate, our team of developers log into a Terminal Server Farm and they may add for example. They probably won't all add the same addins

Analysis Toolpack
Euro Currency Tools
Solver Addin
Custom company addin 1
Custom company addin 2

We have a script on this particular Terminal Server farm  which looks for 2 Excel addins which are added from another Terminal Server Farm and removes them or Excel grumbles when you open it on this farm that the addins don't exist which they don't.

However when they log back into the other Terminal Server Farm where the addins exist, they need those 2 Excel Addins loaded back in automatically. However as they are roaming profiles, when they log into this server there will already be OPEN values for whatever addins follow their profile so we need to add the 2 Excel addins to the next available empty OPENx keys

Hope this makes sense
0
 
LVL 1

Author Comment

by:rhiancohen
Comment Utility
Thanks oBda. I'll take a look at the script and come back to you.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
I came at it slightly differently, give this a try too and see if it works and makes sense.

@echo off
setlocal EnableDelayedExpansion

REM Set to Y to enable some display of progress, N to disable output
set DEBUG=Y

REM Define base key, and new keys to add
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
set AddList="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam","D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"

REM Load existing keys from registry into local variables
call :LoadExisting

REM Add each new key desired (if not already present)
for %%A in (%AddList%) do call :AddNew %%A

REM Exit main script
exit /b


REM Subroutine to gather exiting OPEN keys from registry
:LoadExisting
  for /f "tokens=1,2*" %%A in ('reg query "%Key%" /v "open*" ^| findstr /i /r /c:"open.*"') do (
    if /i "%%A" EQU "OPEN" (
      set Open[0]=%%C
    ) else (
      set Index=%%A
      set Open[!Index:OPEN=!]=%%C
      if "%DEBUG%" EQU "Y" echo Existing: [Open[!Index:OPEN=!]=%%C]
    )
  )
  exit /b

REM Subroutine to check if a key exists, and add it in not to the next available OPEN* value
:AddNew [key-to-add]
  set Found=N
  for /f "tokens=1* delims==" %%A in ('set Open[') do (
    if %1 EQU %%B (
      set Found=Y
      if "%DEBUG%" EQU "Y" echo Skipped: [%%B]
    )
  )
  if "!Found!" EQU "N" (
    set Added=N
    for /l %%I in (0, 1, 99) do (
      if "!Added!" EQU "N" (
        if not defined Open[%%I] (
          set Added=Y
          set Value=OPEN
          if %%I GTR 0 set Value=!Value!%%I
          set Data=%1
          set Data=!Data:"=\"!
          reg.exe add "%Key%" /v !Value! /t REG_SZ /d "!Data!" /f >NUL
          if "%DEBUG%" EQU "Y" echo Added: [!Value!=%1]
        )
      )
    )
  )
  exit /b

Open in new window

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
A few small fixes / enhancements to the above...

@echo off
setlocal EnableDelayedExpansion

REM Set to Y to enable some display of progress, N to disable output
set DEBUG=Y

REM Define base key, and new keys to add
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
set AddList="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam","D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"

REM Load existing keys from registry into local variables
call :LoadExisting

REM Add each new key desired (if not already present)
for %%A in (%AddList%) do call :AddNew %%A

REM Exit main script
exit /b


REM Subroutine to gather exiting OPEN keys from registry
:LoadExisting
  for /f "tokens=1,2*" %%A in ('reg query "%Key%" /v "open*" ^| findstr /i /r /c:"open.*"') do (
    if /i "%%A" EQU "OPEN" (
      set Open[0]=%%C
      if "%DEBUG%" EQU "Y" echo Existing: [Open[0]=%%C]
    ) else (
      set Index=%%A
      set Open[!Index:OPEN=!]=%%C
      if "%DEBUG%" EQU "Y" echo Existing: [Open[!Index:OPEN=!]=%%C]
    )
  )
  exit /b

REM Subroutine to check if a key exists, and add it in not to the next available OPEN* value
:AddNew [key-to-add]
  set Found=N
  for /f "tokens=1* delims==" %%A in ('set Open[ 2^>NUL') do (
    if %1 EQU %%B (
      set Found=Y
      if "%DEBUG%" EQU "Y" echo Skipped: [%%B]
    )
  )
  if "!Found!" EQU "N" (
    set Added=N
    for /l %%I in (0, 1, 99) do (
      if "!Added!" EQU "N" (
        if not defined Open[%%I] (
          set Added=Y
          set Value=OPEN
          if %%I GTR 0 set Value=!Value!%%I
          set Data=%1
          set Data=!Data:"=\"!
          reg.exe add "%Key%" /v !Value! /t REG_SZ /d "!Data!" /f >NUL
          set OPEN[%%I]=!Data!
          if "%DEBUG%" EQU "Y" echo Added: [!Value!=%1]
        )
      )
    )
  )
  exit /b

Open in new window

~bp
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Updated version, now ignores if the Addin already exists:
@echo off
setlocal enabledelayedexpansion
set AddinList[1]="D:\Program Files (x86)\ibm\cognos\tm1\bin\tm1p.xlam"
set AddinList[2]="D:\Program Files (x86)\ibm\cognos\tm1\bin\ManCalcv.xlam"
set Key=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options
set Quiet=0
set LastIndex=10
for /f "tokens=1* delims==" %%a in ('set AddinList[') do (
	if %Quiet%==0 echo Processing '%%b'
	set Value=
	set FoundAt=
	set Data=%%b
	set CompareData=!Data:"=!
	for /l %%i in (0, 1, %LastIndex%) do (
		if %%i==0 (set TestValue=OPEN) else (set TestValue=OPEN%%i)
		reg.exe query "%Key%" /v "!TestValue!" >NUL 2>&1
		if errorlevel 1 (
			if "!Value!"=="" (
				if %Quiet%==0 echo !TestValue! ... free.
				set Value=!TestValue!
			)
		) else (
			if %Quiet%==0 echo !TestValue! ... already used.
			for /f "tokens=2*" %%o in ('reg.exe query "%Key%" /v "!TestValue!"') do set CompareDataExisting=%%p
			set CompareDataExisting=!CompareDataExisting:"=!
			if /i "!CompareData!"=="!CompareDataExisting!" set FoundAt=!TestValue!
		)
	)
	if "!Value!"=="" (
		echo ERROR: Unable to find an empty index in the range of 0..%LastIndex%
		goto :eof
	)
	if "!FoundAt!"=="" (
		reg.exe add "%Key%" /v "!Value!" /t REG_SZ /d "!Data:"=\"!" /f >NUL
		if errorlevel 1 (
			echo ERROR: Could not create value '!Value!' with data '!Data!' at '%Key%'
		) else (
			if %Quiet%==0 echo Value successfully added.
		)
	) else (
		if %Quiet%==0 echo Found data in '!FoundAt!', nothing added.
	)
)

Open in new window


BillPrew,
using a list format like AddList won't work for every case. The data can contain additional arguments like /R "C:\Program Files\Microsoft Office\Office14\Library\Analysis\ANALYS32.XLL" which can't be correctly parsed in a list.
In a case like this, your AddNew function will fail, too.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Okay, let me adjust for that...

~bp
0
 
LVL 1

Author Comment

by:rhiancohen
Comment Utility
Hiya oDba and Bill,

Thanks very much to you both for your assistance.

I've just tested oDBa's script at the moment and this seems to work great thanks very much.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now