Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Batch Script


  • Please log in to reply

#1
Artellos

Artellos

    Tech Secretary

  • Global Moderator
  • 3,888 posts
Hey Guys.

I'm trying to make a script that will make my life a lot easier.
At the moment its 2 seperate scripts as 1 part needs to be run before a reboot.
The other side of the script needs to be run after the reboot.

Simple question realy.
Is there a command that can be run in the win2000 command line that will allow me to execute a script, then reboot and -after- the reboot -continue- the script.

If it's not an integrated command, are there 3rd party programs that will allow me to do so?

Regards,
Olrik
  • 0

Advertisements


#2
dsenette

dsenette

    Je suis Napoléon!

  • Administrator
  • 26,019 posts
  • MVP
put the script that needs to run after the reboot in the startup folder.....then you run the pre-reboot script...reboot the pc and once it starts up it will run the other script
  • 0

#3
Artellos

Artellos

    Tech Secretary

  • Topic Starter
  • Global Moderator
  • 3,888 posts
Ah thanks dsenette!

I'll be using that idea and modifying it to something like this:
Start First script
Run first script
first script copies script 2 to startup
reboot
script 2 runs
script 2 deletes itself

Regards,
Artellos
  • 0

#4
dsenette

dsenette

    Je suis Napoléon!

  • Administrator
  • 26,019 posts
  • MVP
you might change that up to have script 2 initiate a 3rd script that removes script 2 from startup...i've not had much success with getting a running script to delete itself...however...if you've got a method of doing that....share! hehe
  • 0

#5
Artellos

Artellos

    Tech Secretary

  • Topic Starter
  • Global Moderator
  • 3,888 posts
you can just use the "del" command to delete a batch file.
As long as you keep it at the of the script it'll be fine.
Example:

This would work AND show the echo
echo this will be shown
del C:\test.bat

This would NOT work and not show the echo
del C:\test.bat
echo this won't be shown

Regards,
Olrik
  • 0

#6
aikeru

aikeru

    New Member

  • Member
  • Pip
  • 8 posts
I think a much cleaner approach would be to run the same 1st script with arguments like
/AFTERBOOT.

For instance at the beginning

IF /i {%1}=={/AFTERBOOT} GOTO PERFORM_AFTER_BOOT

...blahblah...

GOTO EOF

:PERFORM_AFTER_BOOT

...do after boot stuff...

:EOF

:)
  • 0

#7
Artellos

Artellos

    Tech Secretary

  • Topic Starter
  • Global Moderator
  • 3,888 posts
That's interresting.
Could you give me an example script?
Lets say, before the boot we need to change the user "Olrik" to remove his admin rights and add user rights.
net localgroup administrators /DELETE Olrik
net localgroup users /ADD Olrik
What should I have to use the reboot? :) (Remember this is an Win2000 machine)

And a simple echo like "The scipt worked" :)
I can't really get in my head how this would work.

Regards,
Olrik
  • 0

#8
aikeru

aikeru

    New Member

  • Member
  • Pip
  • 8 posts
Sure. I'm assuming your NET usage is correct.

@echo off
IF /i "%1"=="" GOTO FIRST_TIME
IF /i "%1"=="/REBOOT" GOTO SECOND_TIME

:FIRST_TIME

ECHO do stuff before reboot!
net localgroup administrators /DELETE Olrik
net localgroup users /ADD Olrik

ECHO Do what is needed to have this script kicked off after reboot
ECHO example:
REG ADD HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUNONCE /v MyScript /t REG_SZ /d "c:\myscript.bat /REBOOT"

SHUTDOWN /r


GOTO EOF

:SECOND_TIME

ECHO do other stuff after reboot!
ECHO Remove from registry
ECHO ie
ECHO y|REG DELETE HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUNONCE /v MyScript >NULL
PAUSE

GOTO EOF

:EOF


Some explanation:

IF /i ... -- Regardless of case
ECHO y| REG DELETE ... -- It prompts "are you sure?" so pipe "y" (yes) into it
SHUTDOWN /r -- This SHOULD work for 2000 but it may be SHUTDOWN -r, I don't have 2000 box to test on ATM. SHUTDOWN /? will tell you, of course. :)

As always be careful with registry for anyone else reading you can cause your computer to stop working if you modify the registry so be sure you know what you're doing.

Any more questions feel free to ask. :)

EDIT: Deleting the RunOnce key might not be necessary, always test a script before setting it loose in an infrastructure :)

Edited by aikeru, 24 April 2008 - 07:39 AM.

  • 0

#9
Artellos

Artellos

    Tech Secretary

  • Topic Starter
  • Global Moderator
  • 3,888 posts
Thanks a lot! :)
I'm pretty sure i'm going to experiment with this :)

The SHUTDOWN part we had already figured out and you where right saying it was /r (if you where currious)
We did have to paste the Shutdown.exe into the WINNT folder but that's because its not installed when you do a clean install.

As I'm not completely familliar with the Registry (yet :) ) would this be the exact registry command I'd need to type in or would I have to dig a bit myself first? (don't want to run it and found out I just did something silly :) )

Regards,
Olrik
  • 0

#10
aikeru

aikeru

    New Member

  • Member
  • Pip
  • 8 posts
I tested it to make sure it added/removed so... yes that would be the exact registry IF your script was "myscript.bat" and resided on the C:\.
  • 0

#11
Artellos

Artellos

    Tech Secretary

  • Topic Starter
  • Global Moderator
  • 3,888 posts
Awesome! :)
Thanks a lot.

Regards,
Olrik
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP