Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Batch Script


  • Please log in to reply

#1
Artellos

Artellos

    Tech Secretary

  • Global Moderator
  • 3,915 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!

  • Community Leader
  • 26,047 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,915 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!

  • Community Leader
  • 26,047 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,915 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,915 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,915 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,915 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