Link to home
Start Free TrialLog in
Avatar of taki1gostek
taki1gostekFlag for United States of America

asked on

Continue batch file after restart

Hi Experts!

I have a few unattended installs I am running in a batch script...   The script will be initially started using the local administrator account on non-domain PC's.  

The first thing I would like to do is to have the script create a local user and password (with administrator privileges/security group) and set windows to log-in automatically using those credentials.  

At this point, I would like the script to reboot the computer (using the shutdown -r command), and when the computer automatically logs-in (with the new username/password pair), the script should re-open, wait 15 seconds (ping -n 15 localhost >nul?) to let windows finish creating the new profile and continue on the next line of the batch file.  

I would also like the code that'll be needed in order to get rid of the automatic log-in (i.e. for Windows XP not to remember the password and require the user to log-in).  
Avatar of johnb6767
johnb6767
Flag of United States of America image

What I usually do, for something like this, is to use 2 batch files....

In the first one, you will need your user account creation command.....

net user John S3cr3tPa55w0rD /add
net localgroup Administrators John /add

Change the username and the top secret password accordingly....

Then have a copy command from your source, or a network location, to copy your next .bat file into the All Users Startup Group.

Course your shutdown.....

shutdown -f -r -t 05

In the next batch file......

ping -n 15 localhost >nul (or you can use the sleep.exe command from the resource kit....)

At the bottom of the batch file, add a delete command for the secondary script, to remove it from all users startup.....




You will need to enable the autologin in the first script.....

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /d DefaultUserName /t reg_sz /v John
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /d DefaultUserPassword /t reg_sz /v Password
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /d AutoAdminLogon /t reg_sz /v 0x1

ASKER CERTIFIED SOLUTION
Avatar of johnb6767
johnb6767
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of taki1gostek

ASKER

Thanks John!

Looks like a workable solution, but I was really hoping for some code, which will be 'smart enough' to start the same exact batch file and continue on the next line (after the shutdown -r command)...  Any other ideas?
Just so you have some background on what's being done...  I am running a number of unnattended installs one after the other... some of them will require a restart (and the unattended batch file will not be static - but dynamically created by a different batch script) -- so let's say the script is called go.bat and looks like this (not real, just stuff from memory)

1 @ECHO OFF
2 TITLE Installing Selected Applications
3 ECHO Please wait...
4 START /WAIT msiexec /i c:\installs\adobe.msc
5 START /WAIT msiexec /i c:\installs\someprogram.msc
6 REM SCRIPT TO MAKE SURE THAT THE SCRIPT WILL CONTINUE ON LINE 8 AFTER REBOOT
7 shutdown -r
8 ECHO Continuing Installation
9 start /wait c:\installs\program.exe /quiet /norestart
10 start /wait msiexec /i c:\installs\testing.msc
11 REM SCRIPT TO MAKE SURE THAT THE SCRIPT WILL CONTINUE ON LINE 13 AFTER REBOOT
12 shutdown -r
13 ECHO Continuing Installation
14 start /wait c:\installs\lastprogram.exe /q
In terms of continuing the batch file where it left off I suppose what you need to do is have something like this in it (having alreadyy added the go.bat to startup group or HKLM Run key and set the autologon...

@echo off
title xxxxx
set /p nextpart=<c:\nextpart.txt
if not "%nextpart%"=="" goto nexpart
START /wait ...
START /wait ....
echo part2 > c:\nextpart.txt
shutdown -r
:part2

etc.

i.e. create a text file "c:\nextpart.txt" which stores the next line label to start at in go.bat.  Read the text file into variable %nextpart% and use that to goto the label...

I like the menu routine in the other Q btw ... bookmarked!

Steve
Sorry some of that didn't copy/paste too well....

@echo off
title xxxxx
set /p nextpart=<c:\nextpart.txt
if not "%nextpart%"=="" goto %nextpart%
START /wait ...
START /wait ....
echo part2 > c:\nextpart.txt
shutdown -r
:part2
Steve,

Being that my first batch creates the "go.bat" file dynamically from a group of different batch files, how should i modify the batch files that require a restart for your script to do its job properly?  Wouldn't i have to have different variables for different programs?, Let's say the script looks like this:

set /p nextpart=<c:\nextpart.txt
if not "%nextpart%"=="" goto %nextpart%
msiexec /i \\server\share\office.msi
echo part2 > c:\nextpart.txt
shutdown -r
set /p nextpart=<c:\nextpart.txt
if not "%nextpart%"==" goto %nextpart%
msiexec /i \\server\share\someprogram.msi
echo part2 > c:\nextpart.txt

Don't my variables need to be different for every program?  I guess I need the script to "dynamically" generate maybe an incremental number in the text file, so that it knows that it should start on the next line as opposed to taking it from the start?  

I'm sure I'm not making myself clear... sorry - but hope you can figure it out?  lol  You've done some work on the other script question I was working on, so you probably get the full picture...  
not aplication on the pc right now...  will think about it.   I was imagining that the label numbers and any restarts they need etc. would go in the 1.bat 2.bat etc. before combining....

Actually what we need is the go.bat to be made from

rem start bits about going to next part...
if exist go1.bat call go1.bat
:line2
if exist go2.bat call go2.bat
etc.

then if go1 needs a reboot it does a shutdown command and echo line2>nextpart.txt and the script continues at line2 above.  It doesn't matter I the next script to run is no 2,3,4 or whatever because if 2 or 3 is deselected then go2 won't exist and it won't run.. will just fall through to 4 say.

Sory if bit meey"sy, sending from mobile... will look back later
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why not just push the restart until the end of all the apps....

I usually NEVER do immediate reboots when prompted, and not once experienced a problem. Mainly because I dont try and use the apps until the reboot etc.....

Most packages can be run to supress the reboot.....

msiexec /i adobe.msi /norestart
I ran into major issues when suppressing restarts in between installation of Autodesk applications... I'm not just installing them, I'm activating them, starting them, then exiting, then rebooting, just to be safe, and to simulate what was done by hand...  Idea is to really & fully automate the process.
I had to split points equally between dragon & john... thank you very much for your help guys!
I will post my complete working script as soon as I get a chance tonight!  Thanks again!
John, what's the purpose behind msiexec /i?  I mean, I can run the same ***.msi with the correct switches without first starting windows installer...  Just a random question I haven't looked at.
Can give you more control if you need the other swithces... or you could potentially do

start /wait "" yourmsi.msi

but the msiexec way is better IMO.

Steve
Just a preference really..... /i to install, /x to uninstall etc....

Plus once a package is registered, you can use GUID strings to uninstall etc....

msiexec /x{blahdf-dfdfdfd-df-dfdf-d-}

Look forward to the script......
I'm still sort of stuck here... trying to figure this out...   What do you think about maybe approaching this a litle differently?  I have a concept but no clue how to program it...  

Here's what I'm thinking - please let me know if you think i'm onto something... and of course, correct... I will start a new question if you think this is possible!  

:next
start /wait office.msi /silent
type goto next >> go.bat <---- how can I add "goto next followed by enter" to the first line of the go.bat batch file?  And now do something like this:
for /f "tokens=* delims= " %%a in (go.bat) do (
echo %%a | findstr ":next" >> go.bat <--------- This is the line I would need help with... basically, can the findstr look for ":next" and remove the first instance of it that it finds in go.bat?  The first goto next line will tell the go.bat file to jump to the next instance of :next  
shutdown -r
:next
REM LAST PROGRAM INSTALL
start /wait adobe.msi /silent
for /f "tokens=* delims= " %%a in (go.bat) do (
echo %%a | findstr ":next" >> go.bat (again search for the first instance of :next and remove the whole line)
shutdown -r
:next
start /wait vnc.exe /silent
for /f "tokens=* delims= " %%a in (go.bat) do (
echo %%a | findstr ":next" >> go.bat (again search for the first instance of :next and remove the whole line)
shutdown -r
:next
REM END OF FILE
PAUSE
Actually the very first line should already say Goto Next
(so we don't have to worry about adding that, as in my comments above)

i.e.

GOTO NEXT
:NEXT
code begins...
OK - I started a new question:

https://www.experts-exchange.com/questions/24251809/Batch-Script-find-first-instance-of-string-delete-that-line.html

Please see if you can take a stab at it -- Thanks again!