views:

465

answers:

1

As part of a very simple cmd.exe install script, I need to run a program the next time the machine reboots. I don't want it to run after that (it's a one-shot configuration tool).

The program will actually be another cmd.exe script but any example should do since I can run cmd /c on the script itself.

What's the best way to go about doing this?

+1  A: 

You could use the SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce key

This VB script could help. Extract:

workfile      = ifile.ReadLine
strcomputer   = ucase(left(workfile,instr(workfile,",")-1))
Set oReg      = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strcomputer & "\root\default:StdRegProv")

if err.number <> 0 then
 ofile.WriteLine "[" & now() & "] " & strcomputer & " will NOT run once. Failed to set runonce install with error: " & Err.Number & "/" & left(Err.Description,17) 
else
 sKeyPathEnv      = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
 sValueName       = "Set_RunOnce"
 sKeyPath     = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
 sValueName       = "SystemRoot"

 oReg.GetExpandedStringValue HKLM, sKeyPath, sValueName, sSystemRoot
 oReg.SetStringValue HKLM, sKeyPathEnv, "Set_RunOnce", vRunOnce

 if Err.Number <> 0 then
  ofile.WriteLine "[" & now() & "] " & strcomputer & " will NOT run once. Failed to set runonce install with error: " & Err.Number & "/" & left(Err.Description,17) 
 else
  ofile.WriteLine "[" & now() & "] " & strcomputer & " will run once via runonce at next reboot. "
 end if
end if
VonC
Alternatively, you can use REG.EXE to accomplish the same thing. I usually prefer REG.EXE to incurring the extra mind-boggle of a scripting language.
Kim Gräsman
@Krim: true, the scripting language here only add log and error management. If you do not need that extra level, reg.exe is enough.
VonC
Winner by default, I guess :-) No, actually it's a good answer, and we *do* need error handling so I'll probably do it in VBScript. Thanks, VonC.
paxdiablo
@Pax: you're welcome. May be a bounty could have helped?
VonC
Note: RunOnce is only executed when a admin logs in...
Anders