0

We want to make a change to hosts file of our mobile PC we are adding to our sites. However the client isn't so well versed with computers, that's why I wanted to create a batch file to do the update for them.

The idea is to -Make a copy of the original with the date it was copied. -Update the hosts file.

Due to dealing with win 10 and 7 we found out sometimes we cant straight update the hosts file without copying it first and then override the original. Therefore the procedure I thought would work is this.

  1. Copy of the original with the date it was copied.
  2. Copy the hosts to desktop.
  3. Update the hosts file on desktop.
  4. override the original hosts file.
  5. message if success or failed.

You can see the program doesn't look so difficult, but I encountered an error due to privileges error. So due to our clients having not enough knowledge, I wanted to try and elevate it though my batch file.

I'v tied searching here and found a post "How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?". But unfortunately my VBS and batch knowledge isn't that high and I hoped someone might help me decipher it.

The code I'v created:

::the copy to desktop command                                                                         xcopy /s C:\Windows\System32\drivers\etc\hosts %userprofile%\desktop

::the update of hosts file                                                                            
echo www.qpv-view.info xxx.xxx.xxx.xxx >hosts

::the date for the copied hosts file                                                               
@echo off                                                                                              
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a                       
set YYYY=%dt:~0,4%                                                                                     
set MM=%dt:~4,2%                                                                                       
set DD=%dt:~6,2%                                                                                       set stamp=Oldhosts_%YYYY%%MM%%DD%                                                                     
copy /y C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers\etc\%stamp%                                          

::The override of the updated file                                                                    
move /s/Y %userprofile%\desktop\hosts C:\Windows\System32\drivers\etc

end

When I launch, the copy works fine. However the update part requires elevated privileges. However if I start the script with elevated privileges the copy doesn't occur.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Nik
  • 11
  • 2
  • https://winsourcecode.blogspot.com/2019/12/rem-three-files-followrem.html –  Feb 13 '20 at 01:05
  • 1
    Read [ask] and [mcve]. `end` is not a command or a label. `::` are an abuse of label feature that will cause you problems eventually. Use `rem` instead. If you expect your script to run correctly, it should be run elevated. – jwdonahue Feb 13 '20 at 01:11
  • When/where is the `stamp` variable set? – jwdonahue Feb 13 '20 at 01:14
  • 2
    This is a classic [XY problem](https://en.wikipedia.org/wiki/XY_problem). You're asking for help you don't actually need, because your code us broken in places you don't seem to understand. – jwdonahue Feb 13 '20 at 01:17
  • I would suggest that you do not try to elevate from within the script. In order to run the script, the end user should right click on it and choose 'Run as administrator', especially as `HOSTS` is located within a protected system directory structure. You can then just check that the end user has the required privileges with a line at the beginning of your script: `@"%__AppDir__%net.exe" Session>NUL 2>&1||(Echo Exiting ... please right click this script and run as administrator&Pause>NUL&Exit /B 1)` – Compo Feb 13 '20 at 02:09
  • jwdonahue, noted thanks for the heads up. And about the stamp part I just forgot to copy it to the post... set stamp=Oldhosts_%YYYY%%MM%%DD% is the line. – Nik Feb 13 '20 at 02:25

2 Answers2

0

Because stamp is not defined, this line will copy the hosts file over itself:

copy /y C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers\etc\%stamp%

Because it is equivalent to:

copy /y C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers\etc\
jwdonahue
  • 6,199
  • 2
  • 21
  • 43
0

It seems that you cannot copy to %userprofile%\desktop while using elevated privileges!

So I just copied the file to drivers folder and all ran fine.

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set stamp=Oldhosts_%YYYY%%MM%%DD%

copy /y C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers\etc\%stamp%
copy /y C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers

echo www.qpv-view.info 172.16.10.60 >C:\Windows\System32\drivers\hosts

move /y C:\Windows\System32\drivers\hosts C:\Windows\System32\drivers\etc

pause
Compo
  • 36,585
  • 5
  • 27
  • 39
Nik
  • 11
  • 2
  • Might I suggest that you replace lines `2` through `6` with just one line: `@For /F "Tokens=1-3Delims=/ " %%G In (""%__AppDir__%Robocopy.exe" \: . /NJH /L|"%__AppDir__%find.exe" " 123""')Do @Set "stamp=Oldhosts_%%G%%H%%I"`. It should run a little quicker as well as reduce your code by four lines. – Compo Feb 13 '20 at 09:34