【问题标题】:VBScript not working properly when scheduledVBScript 在计划时无法正常工作
【发布时间】:2018-12-05 12:35:39
【问题描述】:

我有一个 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。但在那之后,它会挂起什么的。

我怎样才能让它作为计划任务工作?

【问题讨论】:

    标签: vbscript windows-10 registry scheduled-tasks


    【解决方案1】:

    弄明白了,它实际上与代码无关。如果其他人遇到类似的事情,我会发布答案。事实证明,问题出在计划任务本身——我需要在“开始于”框中放置一个文件夹(在指定要计划的操作时)。我认为这是因为脚本一路输出到临时文件,所以它需要一个放置这些文件的地方。不管怎样,只要我填了那个空白,它就开始成功运行了。

    【讨论】:

    • 感谢分享答案。在这种情况下,我的另一个怀疑是“不正确的用户权限”。未来提示:您可以使用Process Monitor 轻松调试此类事情。它会帮助您立即发现这里的问题。
    【解决方案2】:

    尚未尝试您的代码,但您从脚本中发送了命令行命令。这对我来说似乎违反直觉。直接从脚本中进入注册表可能会更好。您可以在此处找到如何执行此操作的示例:https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/wmi-tasks--registry

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2014-02-15
      • 2013-12-11
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多