# Batch reboot script with loop



## cwilliams255 (Sep 5, 2007)

Here is the problem, I wish to create a batch script to read in asset numbers (xp embedded machines) and reboot them one by one. I have a script to reboot them and I have a script that will do the looping bit but i dont know how to put them together. I would also like any errors encountered to be outputted to a file (if possible)

This code asks for asset number and then reboot via pssutdown tool


```
@ECHO OFF
CLS

SET /P input=Enter Asset number:

set str=%input%

set str=%str:~-4%

"psshutdown" -r -t 5 -m "This is a scheduled reboot" -u <username> -p <xx%str%xx> -f \\%input%
```
I have made a simple loop too that reads in each asset and at present instead of the above command just pings each asset in turn

Any help you could give would be much appreciated


----------



## goldhat (Aug 12, 2007)

one problem is your defining one variable to be two things

set str=%input%

set str=%str:~-4%

probably should not work

try this

set str=%input%

set str2=%str:~-4%


----------



## goldhat (Aug 12, 2007)

also post your loop script im expert at bat file i can probably put them together


----------



## cwilliams255 (Sep 5, 2007)

Erm my for loop said something like (im at home, batch file is at work)


```
FOR /F %input% IN (asset.txt) DO <command>
```
<command> is the reboot I wish to do

I really should read about defining variables and the sytax but im feeling a little lazy at the mo

Obviously the line asking for you to input the asset number is no longer needed.

I'd imagine I need something like

@ECHO OFF
CLS

set str=%input%

set str2=%str:~-4%

FOR /F %input% IN (asset.txt) DO "psshutdown" -r -t 5 -m "This is a scheduled reboot" -u <username> -p <xx%str%xx> -f \\%input%

I think the quotation marks and probably lots of other stuff is wrong


----------



## Squashman (Apr 4, 2003)

We had a very similar thread a couple years back. I believe it was StumpedTechy who was doing something very similar. I think we ended up doing this with a VBscript. I use to have it on my computer but deleted it because I never used it.

I assume the asset numbers are the computer names?


----------



## cwilliams255 (Sep 5, 2007)

Yes they are the names


----------



## ghostdog74 (Dec 7, 2005)

here's a reboot vbscript. change strComputer to suit your needs

```
strComputer = "mycomputername"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
    objOperatingSystem.Reboot()
Next
```


----------

