Batch files running on startup - Not running synchronously.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Batch files running on startup - Not running synchronously.

#1 Post by Cancerous » 11 Oct 2012 17:09

I'm setting up an image for a totally automated install of Windows 7 - this includes setting the computer name (using wsname), and joining it to the domain (using netdom).

Overview:

Run sysprep
Change computer name
Reboot
Add to domain
Reboot


wsname and netdom both have reboot functions built in, however they seem to be creating the needed flag files before actually performing the function.

Here's what I have so far.

wsname.bat

Code: Select all

IF NOT EXIST c:\namechanged.flag (
c:\wsname.exe /N:$SERIALNUM /REBOOT
echo c:\namechanged.flag

joindomain.bat

Code: Select all

IF EXIST c:\namechanged.flag (
IF NOT EXIST c:\joineddomain.flag (
netdom join %computername% /Domain:Company /UserD:Admin PasswordD:password /REBoot
echo > joineddomain.flag)
)

delete.bat

Code: Select all

IF EXIST c:\joineddomain.flag (
net user Admin /del
del c:\joindomain.bat
del c:\wsname.bat
del c:\delete.bat
)


Also, the Admin account is deleted before joining the domain, so there's no option to log in.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch files running on startup - Not running synchronous

#2 Post by foxidrive » 11 Oct 2012 18:12

You are missing a period in two places, redirection character and a close parenthesis.

Cancerous wrote:
wsname.bat

IF NOT EXIST c:\namechanged.flag (
c:\wsname.exe /N:$SERIALNUM /REBOOT
echo.> c:\namechanged.flag
)


joindomain.bat
echo.> joineddomain.flag


Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Re: Batch files running on startup - Not running synchronous

#3 Post by Cancerous » 11 Oct 2012 18:23

Closed parenthesis was a typo, the script has it but I had to type them out due to them being on a VM and copy paste not working properly.

What's the purpose of a period in echo.>?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch files running on startup - Not running synchronous

#4 Post by foxidrive » 11 Oct 2012 18:28

Without it the command will generate an error.

Testing it in Win7 now it does create the file and the error message is in the file.
Same in an XP VM.

So that shouldn't affect your code except the first flag file wasn't being created.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch files running on startup - Not running synchronous

#5 Post by foxidrive » 11 Oct 2012 18:37

This is missing the c:\ path so is probably being created in a random place.

echo > joineddomain.flag)

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch files running on startup - Not running synchronous

#6 Post by Dos_Probie » 11 Oct 2012 20:15

You know if your wanting to do a automated install of win7 you could easily acomplish
most everything you want just by using the Autounattend.xml and also make your install
pretty much hands free as well. I will be glad to post or pm mine over to you if it would help.. 8)

Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Re: Batch files running on startup - Not running synchronous

#7 Post by Cancerous » 11 Oct 2012 22:22

Dos_Probie wrote:You know if your wanting to do a automated install of win7 you could easily acomplish
most everything you want just by using the Autounattend.xml and also make your install
pretty much hands free as well. I will be glad to post or pm mine over to you if it would help.. 8)


I'm using sysprep with unattend.xml. When I first started messing with it I was trying to do it all with sysprep and synchronous commands in the .xml, but it wasn't working properly so I resorted to startup scripts. I also need it to be able to rename the computer from the BIOS serialnumber, reboot and join the domain.
If you've got a working one it would be great to see, I've been bashing my head against this for months!


@People pointing out typo's, they are non-existent in the actual scripts.
I just ran it again, and it looks like it's creating c:\joineddomain.flag on the first reboot while changename.bat is still running, causing netdom to not run on the next reboot because the flag file exists. Any suggestions?

In bash I'd use the || operator to run the netdom script (|| means if the previous command failed, then run this), is there anything like that for batch?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch files running on startup - Not running synchronous

#8 Post by Squashman » 11 Oct 2012 22:41

You can use || and && in batch.

Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Re: Batch files running on startup - Not running synchronous

#9 Post by Cancerous » 11 Oct 2012 22:52

Squashman wrote:You can use || and && in batch.


Well then... That makes things easier...

Is this syntax correct?

Code: Select all

@echo off
IF NOT EXIST c:\changedname.flag c:\wsname.exe /N:$SERIALNUM /REBOOT; echo.> c:\changedname.flag

|| IF NOT EXIST c:\joineddomain.flag netdom join %computername% /Domain:Domain /UserD:Admin /PasswordD:Pass /REBOOT; echo.> c:\joineddomain.flag

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch files running on startup - Not running synchronous

#10 Post by foxidrive » 12 Oct 2012 01:43

Cancerous wrote:@People pointing out typo's, they are non-existent in the actual scripts.


It's just as likely that the actual scripts have typos in them.

Your logic seems sound, assuming all three batch files are running in the startup group.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch files running on startup - Not running synchronous

#11 Post by foxidrive » 12 Oct 2012 01:47

Cancerous wrote:joindomain.bat

Code: Select all

IF EXIST c:\namechanged.flag (
IF NOT EXIST c:\joineddomain.flag (
netdom join %computername% /Domain:Company /UserD:Admin PasswordD:password /REBoot
echo > joineddomain.flag)
)

delete.bat

Code: Select all

IF EXIST c:\joineddomain.flag (
net user Admin /del
del c:\joindomain.bat
del c:\wsname.bat
del c:\delete.bat
)


Also, the Admin account is deleted before joining the domain, so there's no option to log in.


It looks like the admin account is deleted AFTER joining the domain, if netdom is doing that task.

Can an admin account be deleted without any prompting??

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch files running on startup - Not running synchronous

#12 Post by Dos_Probie » 12 Oct 2012 04:02

I dont mess with sysprep install, I slipstream with RT7 and use oobe and setupcomplete via runonce, with sfx apps and then add my unattend.xml to root of usb , walk away and when I come back win7 is setup 100% , all apps preinstalled ie: Office Pro, Nero, AcrobatX Pro , AVG etc and setup , pinned to taskbar, updates, drivers all done etc. etc. 8)

Since your using sysprep check this out (top link) it may help you.. http://bit.ly/QoJbZu

Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Re: Batch files running on startup - Not running synchronous

#13 Post by Cancerous » 14 Oct 2012 16:28

foxidrive wrote:
Cancerous wrote:joindomain.bat

Code: Select all

IF EXIST c:\namechanged.flag (
IF NOT EXIST c:\joineddomain.flag (
netdom join %computername% /Domain:Company /UserD:Admin PasswordD:password /REBoot
echo > joineddomain.flag)
)

delete.bat

Code: Select all

IF EXIST c:\joineddomain.flag (
net user Admin /del
del c:\joindomain.bat
del c:\wsname.bat
del c:\delete.bat
)


Also, the Admin account is deleted before joining the domain, so there's no option to log in.


It looks like the admin account is deleted AFTER joining the domain, if netdom is doing that task.

Can an admin account be deleted without any prompting??


It "looks" that way, but it doesn't perform that way, which is the issue because when the Admin account is deleted, there's no accounts left for the other scripts to run in.
And yes, it gets deleted without prompt, all group policy scripts are elevated I believe.

Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Re: Batch files running on startup - Not running synchronous

#14 Post by Cancerous » 14 Oct 2012 16:42

Dos_Probie wrote:I dont mess with sysprep install, I slipstream with RT7 and use oobe and setupcomplete via runonce, with sfx apps and then add my unattend.xml to root of usb , walk away and when I come back win7 is setup 100% , all apps preinstalled ie: Office Pro, Nero, AcrobatX Pro , AVG etc and setup , pinned to taskbar, updates, drivers all done etc. etc. 8)

Since your using sysprep check this out (top link) it may help you.. http://bit.ly/QoJbZu


Not much use for me. Our infrastructure is Unix based so we don't use AD, and I've already got all the LDAP stuff set up. Also don't see any of it dealing with actually adding it to the domain, they just check if the account exists.

Cancerous
Posts: 9
Joined: 11 Oct 2012 16:55

Re: Batch files running on startup - Not running synchronous

#15 Post by Cancerous » 15 Oct 2012 20:56

Soooo I figured out what is most likely the issue.

Netdom won't run from the .bat file, but will run if I type it directly in to the shell. Any ideas as to why this happens?

Post Reply