NSIS with admin access fails to create reg key via batch script - batch-file

I have a NSIS script with RequestExecutionLevel admin set and within this I invoke a .bat script which adds a reg key.
When the batch-file is executed through command-prompt the reg key gets added. But when running the installer, it executes the .bat file but fails to add the reg key.
nsExec::ExecToStack '"$pluginsdir${SETUP_PATH}\UpdateNtpConfiguration.bat" $Ip1'
UpdateNtpConfiguration.bat content
set adds=%1
REM Get the list of ntp servers showing up in System Date & Time->Internet Time dropdown
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1
REM Add address at the end
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "%adds%" /f
goto:eof

Just like #Anders said, there are built in functions for that.
https://nsis.sourceforge.io/Docs/Chapter4.html#registry
If an an error happens with this command, it will be a lot easier to debug as it is built-in.
If you're not doing anything else in the batch file, it is better to find out if NSIS has a command already integrated with it....
EDIT:
It also might be that the admin installer is run the bat file without admin privs.

After some debugging found out that NSIS was writing the registry values in 32 bit reg space(HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft). I had to append /reg:64 to make it add to the 64 bit reg space.
Also the reason I had the bat script was to have some logic to look for duplicates before adding the key.

Related

Reg Query in script not working when executed remotely with psexec

I have recently gotten a script together for querying the reg for a temp folder path and cleaning the folder and it works great(Thanks Compo) but when used remotely with psexec it says, "The system was unable to find the specified registry key or value." Before I use PSEXEC to execute the batch I first have it copied to the C:\ and I use this to do that.
set /p cpu=
robocopy "\\nmcfs01\software\scripts\Jacob's Awesome Outlook Scripts" \\%cpu%\c$\Jacob'sTemp
That part goes well then I execute it with psexec with
psexec \\%cpu% -u administrator -i -d "C:\Jacob'sTemp\compo.bat"
That connects but when it executes the batch file it does not find the reg key. HOWEVER, if I go to the C:\Jacob'sTemp and double click on the compo.bat file it works fine and deletes out the files. Also, if I just use PSEXEC to execute it again outside of this script just separately after it is copied over it still does not work. Here is the script I am using for cleaning it up.
#Echo Off
SetLocal EnableExtensions
(Set OV=14.0)
Choice /C YN /M "Delete Outlook Temp Files?"
If ErrorLevel 2 Exit/B
Set "BK=HKCU\SOFTWARE\Microsoft\Office\"
Set "EK=\Outlook\Security"
Set "VN=OutlookSecureTempFolder"
For /F "Tokens=2*" %%I In ('Reg Query "%BK%%OV%%EK%" /V %VN%') Do Set "VD=%%J"
PushD "%VD%" && (RD/S/Q "%VD%" 2>Nul) && PopD
pause
REM The below commands will empty Jacob'sTemp:
If Exist "%SystemDrive%\Jacob'sTemp" (PushD "%SystemDrive%\Jacob'sTemp" && (
(RD/S/Q "%SystemDrive%\Jacob'sTemp" 2>Nul) && PopD
pause
REM The below commands without the first two characters will remove Jacob'sTemp
If Exist "%SystemDrive%\Jacob'sTemp" (RD/S/Q "%SystemDrive%\Jacob'sTemp"
Pause
Echo(------------------------------------------------------------------------------
Echo( Complete! Goodbye!
Echo(------------------------------------------------------------------------------
Timeout 5 >Nul
It would be a huge help to have this work as intended where we can just type the computer name in and done but I do not know why it does not work when executed remotely and it is copied to the computer. Any help is much appreciated!
HKCU, the target of your reg query, is a per user registry hive. psexec's remote service runs in SYSTEM account and when it issues reg query that wont be directed to the remote machine's currently logged-in user's HKCU. It would be directed to the SYSTEM account's HKCU which maps under HKEY_USERS\S-1-5-18\Software.... Hence the error "The system was unable to find the specified registry key or value."

Clear the PendingFileRenameOperations registry value

One of my troubleshooting steps is to clear the PendingFileRenameOperations registry value to avoid rebooting a server.
What I would like to do is clear this through a batch file, I don't want to delete it, just clear it.
It's the following registry value
HKEY_LOCAL_MACHINE/SOFTWARE/Session Manager/PendingFileRenameOperations
The command
%SystemRoot%\System32\reg.exe add "HKLM\System\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /t REG_MULTI_SZ /d "" /f
replaces current value of multi-string value PendingFileRenameOperations with an empty string.
The path you wrote in the question does not exist on my Windows 7 x64 machine.
For details on command reg open a command prompt window and run there first reg /? and second reg add /?
But why clearing this registry value used to delete or replace (usually update) files after reboot before Windows loads drivers and starts processes and applications should avoid rebooting a Windows server is beyond my understanding.
Some software just checks for the key existence and doesn't care if its blank,So For deleting Key,Use My Way :
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f

Finding a program's installation path using a batch file

I've been working very hard on a now, very very long script. What I need to do is find the installation directory for a given piece of software. I was using
for /f "tokens=2*" %%a in ('REG Query "HKEY_CLASSES_ROOT\TypeLib\{1779292D-1DAD-47E3-9404-8B32455264C2}\8.1e\HELPDIR" /ve 2^>nul') do set "ExeLoc=%%~b"
The problem with this is that it only works on the latest version of the software. Is there a way I can check the Programs and Features for an application, then grab the directory and set it to "ExeLoc"? And is there a way to do this without being too specific? For example, let's say "Program" is the application name, but in Programs and Features it may show up as "Program" or "Program Client" or "Program Version X.Y" or "Program Client Version X.Y". Would there be a way to search for "Starts with Program" then grab the path from there?
Let me know if you need further information from me.
Oh, and this needs to be within a batch file, without installing any additional API or other scripts, because it needs to be able to run on any machine on the fly, and we're not allowed to install additional software on machines.
Here is the help of the command Where
Try this batch that can accept wildcards with the command Where.exe as string input :
#echo off
Mode con cols=90 lines=15
set Location=C:\Program files
Set FileName=Program*
Set Tmp=Tmp.txt
Set SearchResult=SearchResult.txt
echo( & cls
echo( & echo Please Wait for moment .... Searching for "%FileName%" on "%Location%"
where /r "%Location%" "%FileName%" > %Tmp%
Cmd /U /C Type %Tmp% > %SearchResult%
Del %Tmp%
Start %SearchResult%

Batch script not able to access remote drive when scheduled

I have a simple batch script that copies files locally from a Sharepoint drive that's mapped on the local machine. This runs just fine when being manually run, but when run using task scheduler, it seems as though the script cannot access the drive at all. Even a simple DIR print out fails. It's also worth noting that the user set to run the script in scheduler has full rights to everything involved and is the same account being used to run it manually.
A couple of the settings for the scheduled task:
set to run whether user is logged in or not
running with highest privileges
Any idea how I could get this working in scheduler?
Edit:
Screen shots of the scripts and the task run history
http://imgur.com/a/wCsET
EDIT 2
callCopyFiles.bat
del "C:\apache\htdocs\IESite\Reports\*.*?"
FOR /f "delims=" %%L in (directorylist.txt) do (
echo %%L >> log.txt
CALL copyfiles.bat "%%L"
)
copyfiles.bat
FOR /F "delims=" %%I IN ('DIR %1 /s /b /O:-D') DO (
COPY "%%I" "C:\apache\htdocs\IESite\Reports"
goto :end
)
:ender
My suspicion is that this line in callCopyFiles.bat:
CALL copyfiles.bat "%%L"
is failing because the current directory is different when your batch file is running as a scheduled task.
Try changing this line to list the full path...something like:
CALL C:\YourFolderHere\copyfiles.bat "%%L"
EDIT
Based on the comments, I think you likely need to map the Z: drive within the copyfiles.bat file.
EDIT 2
Don't specify the credentials when mapping the drive.
Just add NET USE Z: \\YourServer\ShareName to the copyfiles.bat file.

Batch - Loop through folders and run command

I am trying to remove Dropbox from around 500+ Windows 7 computers using a batch script. Dropbox is installed on a per-user basis under "%APPDATA%\Dropbox".
To uninstall silently you have to run "%APPDATA%\Dropbox\bin\DropboxUninstaller.exe /S". I have tested this command and it works fine, but the problem is it only uninstalls it from the user directory you run it from. I have tried taking the DropboxUninstaller.exe file and running it from the root of C:, but it fails.
I will be pushing this script out via SCCM, so it will run under the SYSTEM account. I need to find a way to loop between all of the user direcories, find which profiles have the Dropbox\bin\DropboxUninstaller.exe path/file and run it in that location.
I have tested logging in as another user (local admin) and running the Dropbox uninstaller from another users directory, and it uninstalls fine for the user whos directory I run it in, so I know this will work.
If anyone could help me out with the correct command, that would be great! I have googled around for an answer, but it doesn't seem to be out there.
Thanks in advance,
Search user profiles, and for each if unistaller exists, execute it (asumming an "standard" installation, maybe this needs to be adjusted)
#echo off
setlocal enableextensions
for /F "tokens=2,*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfileImagePath /s ^| find "REG_EXPAND_SZ" ^| findstr /v /i "\\windows\\ \\system32\\"') do (
call :doUninstall "%%b"
)
endlocal
exit /b
:doUninstall
set "_uninstaller=%~1\AppData\Roaming\Dropbox\bin\DropboxUninstaller.exe"
if not exist "%_uninstaller%" goto :EOF
start "" /wait "%_uninstaller%" /S
goto :EOF
for /r c:\windows %A in (notepad.exe) do "%A" %windir%\win.ini
Remember %%A in a batch and %A at command prompt.

Resources