从cmd.exe脚本,如何安排任务在下次启动时运行(并且永远不会再次运行)?

时间:2009-08-26 07:59:43

标签: windows scripting cmd scheduled-tasks

作为一个非常简单的cmd.exe安装脚本的一部分,我需要在下次机器重新启动时运行程序。我不希望它在那之后运行(它是一次性配置工具)。

该程序实际上是另一个cmd.exe脚本,但任何示例都应该这样做,因为我可以在脚本本身上运行cmd / c。

这样做最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以使用SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce密钥

VB script可能会有所帮助。提取物:

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