VBScript在计划时无法正常工作

VBScript在计划时无法正常工作,vbscript,windows-10,registry,scheduled-tasks,Vbscript,Windows 10,Registry,Scheduled Tasks,我使用VBScript将多个注册表项备份到单个文件中: Set objShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") arrRegPaths = Array( _ "HKEY_CURRENT_USER\RegKey1\", _ "HKEY_CURRENT_USER\RegKey2\", _ "HKEY_CURRENT_U

我使用VBScript将多个注册表项备份到单个文件中:

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

arrRegPaths = Array( _
      "HKEY_CURRENT_USER\RegKey1\", _
      "HKEY_CURRENT_USER\RegKey2\", _
      "HKEY_CURRENT_USER\RegKey3\" _
      )

Const intForReading = 1
Const intUnicode = -1

strFileName = objShell.ExpandEnvironmentStrings("%UserProfile%") & "\FolderName\FileName.reg"

Set objRegFile = objFSO.CreateTextFile(strFileName, True, True)
objRegFile.WriteLine "Windows Registry Editor Version 5.00"

For Each strRegPath In arrRegPaths
      strCommand = "cmd /c REG EXPORT " & strRegPath & " " & Replace(strRegPath, "\", "_") & ".reg"
      objShell.Run strCommand, 0, True
      If objFSO.FileExists(Replace(strRegPath, "\", "_") & ".reg") = True Then
            Set objInputFile = objFSO.OpenTextFile(Replace(strRegPath, "\", "_") & ".reg", intForReading, False, intUnicode)
            If Not objInputFile.AtEndOfStream Then
                  objInputFile.SkipLine
                  objRegFile.Write objInputFile.ReadAll
            End If
            objInputFile.Close
            Set objInputFile = Nothing
            objFSO.DeleteFile Replace(strRegPath, "\", "_") & ".reg", True
      End If
Next

objRegFile.Close
Set objRegFile = Nothing
我可以直接从资源管理器(Win10)运行它,也可以直接从CMD运行,或者使用WScript/CScript运行,而且运行起来非常完美。完成大约需要2秒钟,输出文件拥有它应该拥有的一切

但当我尝试将其作为计划任务运行时,它永远不会完成运行。我让它静置几分钟,然后手动杀死它。当我在这之后查看输出文件时,它只有第一行,即Windows注册表编辑器版本5.00。所以我知道脚本正在找到目标文件,并且它正在运行第一条WriteLine。但在那之后,它就挂起来了


如何使其作为计划任务工作?

尚未尝试您的代码,但您可以从脚本中发送命令行命令。对我来说,这似乎违反直觉。直接从脚本点击注册表可能会更好。您可以在这里找到如何做到这一点的示例:

解决了它,实际上它与代码没有什么关系。我会发布答案,以防其他人遇到类似的问题。原来问题出在计划的任务本身——我需要在“开始时间”框中放置一个文件夹(在指定要计划的操作时)。我假设这是因为脚本在过程中输出到临时文件,所以它需要一个放置这些文件的位置。不管怎样,一旦我填写了这个空白,它就开始成功运行了。

谢谢分享答案。在这种情况下,我的另一个怀疑是“不正确的用户权限”。未来的提示:您可以很容易地使用。这将有助于你马上发现这个问题。