Windows batch file REG ADD not working sometimes, but not all the time

48 Views Asked by At

I have a batch file that the user is running which writes a value to their registry file (HKCU). Sometimes the REG ADD works, sometimes it doesn't. For instance, the first time I trialled it on an end user, it didn't work (no errors though), I ran it again and it didn't work (still no errors), I ran it again and it worked. Weird...

These are the commands (obviously the variables have been defined earlier in the script):

SET "logFolder=%userprofile%\SomeCo\SomeCo - Documents\General\Email Signatures\04.InstalledLogs"
SET "outlookSigPath=%APPDATA%\Microsoft\Signatures"
SET "outlookRegProfile=HKCU\SOFTWARE\Microsoft\Office\16.0\Outlook\Profiles"
FOR /F "tokens=*" %%A IN ('whoami /upn') DO SET "userId=%%A"
SET "sigName=%userId%"
FOR /F "tokens=3" %%A in ('REG QUERY "HKCU\Software\Microsoft\Office\16.0\Outlook\Diagnostics\BootDiagnosticsData" /V "ProfileBeingOpened"  ^|findstr /ri "REG_SZ"') DO SET "OutProfName=%%A"

REG ADD "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002" /v "New Signature" /t REG_SZ /d "%sigName%" /f
REG ADD "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002" /v "Reply-Forward Signature" /t REG_SZ /d "%sigName%" /f

Any thoughts on why this might be happening, and how best to fix it so it writes the correct values the first run?

What I've tried: Rerunning the script helped, it eventually added the value to the registry. Running the same script on another computer didn't have any problems the first go.

I've added some logging on the script, e.g:

FOR /F "tokens=4" %%A in ('REG QUERY "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002" /v "New Signature"  ^|findstr /ri "REG_SZ"') DO SET "RegNewSignature=%%A"
ECHO New Signature name set in Registry to: %RegNewSignature% >>"%localLogFolder%\%userId%.log"

The first time it ran, it gave a blank value. Then I ran it again and it gave the correct values. I could maybe loop a REG QUERY and IF/ELSE statement like:

reg query mykey >nul
if %errorlevel% equ 0 (
  echo "mykey exists- do nothing"
) else (
  echo "mykey does not exist - add the key and give it a value"
  reg add mykey
  reg add mykey /v value ...
)

Help very much appreciated!

Edit: Original script:

@ECHO OFF
    REM To update, change path for "sigsFolder" in the variable definitions
    REM This script is run by the user to copy their pre-created htm Signature file from SharePoint, and then set that as their Signature in Outlook.

    REM Checks for log local log file path and creates a local log directory in C:\Temp folder  
IF exist C:\Temp\EmailSignatures ( ECHO  C:\Temp\ exists ) ELSE ( MKDIR C:\Temp\EmailSignatures )

    REM Sets the local log folder path
SET "localLogFolder=C:\Temp\EmailSignatures"

    REM Get the user principal name 
FOR /F "tokens=*" %%A IN ('whoami /upn') DO SET "userId=%%A"
ECHO Found user %userId% >"%localLogFolder%\%userId%.log"

    REM Variable Definitions
    REM Also defines folder and registry keys for Outlook signatures
    REM To update, change path for "sigsFolder" 
ECHO Setting Variables >>"%localLogFolder%\%userId%.log"

SET "sigsFolder=%userprofile%\SomeCo\SomeCo - Documents\General\Email Signatures\03.SigFiles"
ECHO sigsFolder variable set to: %lsigsFolder% >>"%localLogFolder%\%userId%.log"

SET "logFolder=%userprofile%\SomeCo\SomeCo - Documents\General\Email Signatures\04.InstalledLogs"
ECHO logFolder variable set to: %logFolder% >>"%localLogFolder%\%userId%.log"

SET "outlookSigPath=%APPDATA%\Microsoft\Signatures"
ECHO outlookSigPath variable set to: %outlookSigPath% >>"%localLogFolder%\%userId%.log"

SET "outlookRegProfile=HKCU\SOFTWARE\Microsoft\Office\16.0\Outlook\Profiles"
ECHO outlookRegProfile variable set to: %outlookRegProfile% >>"%localLogFolder%\%userId%.log"

SET "outlookRegMailSettings=HKCU\Software\Microsoft\Office\16.0\Common\MailSettings"
ECHO outlookRegMailSettings variable set to: %outlookRegMailSettings% >>"%localLogFolder%\%userId%.log"

SET "outlookRegFirstRunPath=HKCU\Software\Microsoft\Office\16.0\Outlook\Setup"
ECHO outlookRegFirstRunPath variable set to: %outlookRegFirstRunPath% >>"%localLogFolder%\%userId%.log"

SET "sigName=%userId%"
ECHO sigName variable set to: %sigName% >>"%localLogFolder%\%userId%.log"

    REM Copy the user's signature to their local Outlook signatures folder
ECHO Copying your signature file >>"%localLogFolder%\%userId%.log"
COPY /Y "%sigsFolder%\%userId%.htm" "%outlookSigPath%" >>"%localLogFolder%\%userId%.log"

    REM Terminate any instance of outlook.exe
ECHO Terminating Outlook >>"%localLogFolder%\%userId%.log"
TASKKILL /F /IM outlook.exe >>"%localLogFolder%\%userId%.log"

    REM Remove the "First-Run" value from the Registry if it exists
ECHO Deleting Outlook First Run Keys >>"%localLogFolder%\%userId%.log"
REG DELETE "%outlookRegFirstRunPath%" /v "First-Run" /f  >>"%localLogFolder%\%userId%.log"

    REM Find Outlook signature Registry path
FOR /F "tokens=3" %%A in ('REG QUERY "HKCU\Software\Microsoft\Office\16.0\Outlook\Diagnostics\BootDiagnosticsData" /V "ProfileBeingOpened"  ^|findstr /ri "REG_SZ"') DO SET "OutProfName=%%A"
ECHO Found Outlook Profile: %OutProfName% >>"%localLogFolder%\%userId%.log"

    REM Set the Outlook New and Reply signatures in the Registry - Outlook Profile path
ECHO Setting your Outlook signature in the Windows Registry >>"%localLogFolder%\%userId%.log"
REG ADD "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002"  /v "New Signature" /t REG_SZ /d "%sigName%" /f >>"%localLogFolder%\%userId%.log"
REG ADD "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002"  /v "Reply-Forward Signature" /t REG_SZ /d "%sigName%" /f >>"%localLogFolder%\%userId%.log"
FOR /F "tokens=4" %%A in ('REG QUERY "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002"  /v "New Signature"  ^|findstr /ri "REG_SZ"') DO SET "RegNewSignature=%%A"
ECHO New Signature name set in Registry to: %RegNewSignature% >>"%localLogFolder%\%userId%.log"
FOR /F "tokens=4" %%A in ('REG QUERY "%outlookRegProfile%\%OutProfName%\9375CFF0413111d3B88A00104B2A6676\00000002"  /v "Reply-Forward Signature"  ^|findstr /ri "REG_SZ"') DO SET "RegReplySignature=%%A"
ECHO Reply and Forward Signature name set in Registry to: %RegReplySignature% >>"%localLogFolder%\%userId%.log"

    REM Set the signature names in the Registry - Outlook Mail Settings
    REM REG ADD "%outlookRegMailSettings%" /v "NewSignature" /t REG_SZ /d "%sigName%" /f
    REM REG ADD "%outlookRegMailSettings%" /v "ReplySignature" /t REG_SZ /d "%sigName%" /f

    REM Disable Outlook Roaming signatures to enable Outlook to use signatures on this device
ECHO Disabling Roaming Signatures >>"%localLogFolder%\%userId%.log"
REG ADD "HKCU\SOFTWARE\Microsoft\Office\16.0\Outlook\Setup" /v "DisableRoamingSignaturesTemporaryToggle" /t REG_DWORD /d 00000001 /f >>"%localLogFolder%\%userId%.log"
FOR /F "tokens=3" %%A in ('REG QUERY "HKCU\SOFTWARE\Microsoft\Office\16.0\Outlook\Setup"  /v "DisableRoamingSignaturesTemporaryToggle"  ^|findstr /ri "REG_DWORD"') DO SET "RegDisableRoamingSig=%%A"
ECHO Outlook Roaming Signatures Toggle set in Registry to: %RegDisableRoamingSig% >>"%localLogFolder%\%userId%.log"

    REM Log to user logfile
REM IF %errorlevel% NEQ 0 (ECHO "ERROR - Finding Outlook Profile path" >>%logFolder%\%userId%.txt
REM ) ELSE
REM (ECHO "SUCCESS - Outlook Profile Path is %OutProfName%" >>%logFolder%\%userId%.txt)

ECHO Outlook Signature script completed >>"%localLogFolder%\%userId%.log"

    REM Copy log file from local computer to Installed Logs folder
COPY "%localLogFolder%\%userId%.log" "%logFolder%\" /Y

When I ran it for the user the first time the log file looked like this:

Setting Variables
sigsFolder variable set to: C:\Users\User\SomeCo\SomeCo - Documents\SomeCo\Admin\Email Signatures\UserSigs 

logFolder variable set to: C:\Users\User\SomeCo\SomeCo - Documents\SomeCo\Admin\EmailSignatures\InstalledLogs 

outlookSigPath variable set to:
C:\Users\User\AppData\Roaming\Microsoft\Signatures 

outlookRegProfile variable set to:
HKCU\SOFTWARE\Microsoft\Office\16.0\Outlook\Profiles 

outlookRegMailSettings variable set to:
HKCU\Software\Microsoft\Office\16.0\Common\MailSettings 

outlookRegFirstRunPath variable set to:
HKCU\Software\Microsoft\Office\16.0\Outlook\Setup  

sigName variable set to: [email protected]  

Copying your signature file 
        1 file(s) copied. 
Terminating Outlook
SUCCESS: The process "OUTLOOK.EXE" with PID 9136 has been terminated. 
Deleting Outlook First Run Keys
The operation completed successfully.

Found Outlook Profile: Outlook
Setting your Outlook signature in the Windows Registry  
The operation completed successfully.

The operation completed successfully.

New Signature name set in Registry to:   
Reply and Forward Signature name set in Registry to:   
Disabling Roaming Signatures
The operation completed successfully.

Outlook Roaming Signatures Toggle set in Registry to: 0x1  Outlook
Signature script completed

The script seemed to complete, but the settings Registry values for New Signature and Reply and Forward Signature are both blank in the output. When I ran it a second and third time though, the log file showed the correct data in the log fil for these values.

0

There are 0 best solutions below