使用用户帐户的任务调度程序和 vbs [英] Task scheduler and vbs using a user account

查看:57
本文介绍了使用用户帐户的任务调度程序和 vbs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安排一个 vbs 脚本以常规用户权限运行.以用户身份登录时脚本运行良好,但是当我尝试从任务计划程序运行脚本时,无论用户是否登录都运行",它卡在以下行:

I am trying to schedule a vbs script to run with regular user rights. The script runs fine when logged in as the user, but when I try to run the script from the task scheduler as "Run whether user is logged on or not", it gets stuck on the following line:

设置 IE = CreateObject("InternetExplorer.Application")

Set IE = CreateObject("InternetExplorer.Application")

我尝试在选中和取消选中以最高权限运行"的情况下运行它.我正在从任务调度程序运行程序:

I have tried running it with "Run with highest privileges" checked and unchecked. I am running the program from task scheduler as:

program/script: "c:\windows\system32\cscript.exe"
arguments: "test.vbs"
start in: c:\

完整代码如下:

Set fso = WScript.CreateObject("Scripting.Filesystemobject")
set tfo = fso.createTextFile("c:\123.txt")
tfo.writeline("1")
Set IE = CreateObject("InternetExplorer.Application")
tfo.writeline("2")
tfo.close

当运行为仅在用户登录时运行"时的输出:

output when ran as "Run only when user is logged on":

1
2

当运行为无论用户是否登录都运行"时的输出:

output when ran as "Run whether user is logged on or not":

1

此外,使用管理员帐户时,该任务将正确运行为无论用户是否登录都运行",但我无法使用管理员帐户作为解决方案.

additionally, the task will run correctly as "Run whether user is logged on or not" when using an admin account, but I cannot use an admin account as a solution.

推荐答案

您需要授予用户作为批处理作业登录"的权限.这可以通过 GUI 完成:

You need to grant the user the "Log on as a batch job" privilege. This can be done either via GUI:

  • 启动 gpedit.msc
  • 导航到计算机配置 → Windows 设置 → 安全设置 → 本地策略 → 用户权限分配
  • 双击作为批处理作业登录"权限
  • 添加用户帐户
  • 点击确定"并关闭gpedit.msc

或在命令行上:

ntrights +r SeBatchLogonRight -u domain\username

ntrights.exeWindows Server 2003 资源工具包工具,但也适用于 Windows 7.您不必安装整个软件包.相反,您可以使用例如7-zip 打开/解压 中的 rktools.msirktools.exe.

ntrights.exe is part of the Windows Server 2003 Resource Kit Tools, but works on Windows 7 too. You don't have to install the whole package. Instead you can use e.g. 7-zip to open/unpack the rktools.msi inside the rktools.exe.

既然您已经这样做了,问题可能是脚本无法生成 GUI 应用程序,因为当用户未登录时您没有交互式桌面.尝试在脚本中添加一些调试代码:

Since you already did that, the issue is probably that the script can't spawn a GUI application, because you don't have an interactive desktop when the user isn't logged on. Try adding some debugging code to your script:

...
On Error Resume Next
Set IE = CreateObject("InternetExplorer.Application")
If Err Then tfo.writeline Err.Number & vbTab & Err.Description
On Error Goto 0
...

此代码片段的测试运行给了我一个权限被拒绝"错误.显然受限用户无法在计划任务中创建 IE 实例.

A test-run of this code snippet gave me a "permission denied" error. Apparently limited users cannot create an IE instance in a scheduled task.

也就是说,您想通过 Internet Explorer 对象实现什么目标?使用 XMLHttpRequest 可能是处理后台任务的更好方法.

That said, what are you trying to achieve with the Internet Explorer object? Using an XMLHttpRequest might be a better approach for background tasks.

这篇关于使用用户帐户的任务调度程序和 vbs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆