Windows 中的计划任务需要用户登录这一事实的解决方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6939548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A workaround for the fact that a scheduled task in Windows requires a user to be logged in
提问by AlexC
I am running a small executable created by a third party that needs to run at regular intervals on a Windows 2008 server. This executable effectively ETLs information from one system to another and needs to run every hour or so around the clock. As part of its processing the executable launches a small Windows Forms type UI.
我正在运行一个由第三方创建的小型可执行文件,它需要在 Windows 2008 服务器上定期运行。这个可执行文件有效地将信息从一个系统传输到另一个系统,并且需要全天候运行大约每小时一次。作为其处理的一部分,可执行文件会启动一个小的 Windows 窗体类型的 UI。
I have set up a scheduled task to call the file and this works ONLY if the user under which the task is configured to run is logged onto the machine (either locally or via Remote Desktop). If I set the task to run as another user, or set the task to run when the user is not logged, on the scheduled task executes and errors. I have tried running as different users including Administrator user and System user. Is there any possible workarounds (without changing the third party code which I have no access to) which would allow this code to be run without a specific user logged in.
我已经设置了一个计划任务来调用该文件,并且仅当配置为运行该任务的用户登录到计算机(本地或通过远程桌面)时,这才有效。如果我将任务设置为以其他用户身份运行,或者将任务设置为在用户未登录时运行,则计划任务会执行并出错。我尝试以不同的用户身份运行,包括管理员用户和系统用户。是否有任何可能的解决方法(不更改我无法访问的第三方代码),这将允许在没有特定用户登录的情况下运行此代码。
采纳答案by David Heffernan
The GUI app needs a desktop and you only get one of those for a logged in user.
GUI 应用程序需要一个桌面,而登录用户只能获得其中一个。
回答by Breeze
This article shows how to create a task that does not require any login: https://www.scriptjunkie.us/2013/01/running-code-from-a-non-elevated-account-at-any-time/
本文展示了如何创建一个不需要任何登录的任务:https: //www.scriptjunkie.us/2013/01/running-code-from-a-non-elevated-account-at-any-time/
The described procedure is as follows:
描述的过程如下:
First, create a scheduled task to run your command with default options as the current user (this will by default create a scheduled task that only runs when you are logged in):
schtasks /create /tn mytask /SC HOURLY /TR "calc"
Then export the task as XML:
schtasks /query /XML /tn mytask > temp.xml
and delete the task:
schtasks /delete /tn mytask /f
Then open the xml file, and replace the line
<LogonType>InteractiveToken</LogonType>
with<LogonType>S4U</LogonType>
This can be done with the following commands assuming powershell is on the system:
powershell -Command "Get-Content '.\temp.xml' | foreach {$_ -replace 'InteractiveToken', 'S4U' }" > new.xml move /y new.xml temp.xml
Now recreate the task from the modified XML file:
schtasks /create /xml temp.xml /tn mytasks
and remove your temp file:
del /f /q temp.xml
首先,创建一个计划任务以作为当前用户使用默认选项运行您的命令(这将默认创建一个仅在您登录时运行的计划任务):
schtasks /create /tn mytask /SC HOURLY /TR "calc"
然后将任务导出为 XML:
schtasks /query /XML /tn mytask > temp.xml
并删除任务:
schtasks /delete /tn mytask /f
然后打开XML文件,并替换行
<LogonType>InteractiveToken</LogonType>
用<LogonType>S4U</LogonType>
假设系统上有 powershell,可以使用以下命令完成此操作:
powershell -Command "Get-Content '.\temp.xml' | foreach {$_ -replace 'InteractiveToken', 'S4U' }" > new.xml move /y new.xml temp.xml
现在从修改后的 XML 文件重新创建任务:
schtasks /create /xml temp.xml /tn mytasks
并删除您的临时文件:
del /f /q temp.xml
回答by MansoorAliA
I might be late in replying, but can't we use at command, without /interactive...
我可能回复晚了,但是我们不能在没有 /interactive 的情况下使用 at 命令...
https://support.microsoft.com/en-us/kb/313565
https://support.microsoft.com/en-us/kb/313565
As per microsoft: /interactive: Use this parameter to allow the task to interact with the desktop of the user who is logged on at the time the task runs.
根据 microsoft: /interactive: 使用此参数允许任务与在任务运行时登录的用户的桌面进行交互。
回答by SystemAdmin
There is a simple solution. Change group to local group "users" and you will not be prompted for a password. (Scheduled Task - General - Security Options - Change User or Group).
有一个简单的解决方案。将组更改为本地组“用户”,不会提示您输入密码。(计划任务 - 常规 - 安全选项 - 更改用户或组)。
回答by Marcus Rogers
I think I have found a solution for this situation. You need to have two user accounts on the server (User1 and User2). RMD into the server under User1. Within this RMD, create your scheduled task, and set it to run under User2 account. Then, from within this RMD, you need to RMD into the server itself using User2 credentials (kind of like Inception's dream within a dream). It's important not to minimize this new RMD window; you can make it small, but it must be open. You are then free to close the original RMD session and the task will run under the User2 account, because User2 has an open desktop from your 2nd RMD session.
我想我已经找到了解决这种情况的方法。您需要在服务器上有两个用户帐户(User1 和 User2)。RMD 进入服务器 User1 下。在此 RMD 中,创建您的计划任务,并将其设置为在 User2 帐户下运行。然后,从这个 RMD 中,您需要使用 User2 凭据将 RMD 进入服务器本身(有点像 Inception 的梦中之梦)。重要的是不要最小化这个新的 RMD 窗口;你可以把它变小,但它必须是开放的。然后您可以自由关闭原始 RMD 会话,任务将在 User2 帐户下运行,因为 User2 有一个来自您的第二个 RMD 会话的打开桌面。
Protip - don't unpin the RMD window handles at the top of the RMD window - it can be a pain to close the correct RMD then. If you do, you'll need to use the Start > Log Out option all the way out of your RMDs.
提示 - 不要在 RMD 窗口顶部取消固定 RMD 窗口手柄 - 然后关闭正确的 RMD 可能会很痛苦。如果这样做,您将需要从 RMD 中一直使用“开始”>“注销”选项。
回答by AlexC
It would seem that from the research I have done (and David Heffernan's answer), without affecting the source code, this is not possible.
从我所做的研究(以及 David Heffernan 的回答)来看,在不影响源代码的情况下,这是不可能的。
There are some useful thoughts on How can I run a Windows GUI application on as a service?which relate to this but none give a viable workaround to this problem.
有一些关于如何将 Windows GUI 应用程序作为服务运行的有用想法?这与此有关,但没有一个提供可行的解决方法来解决此问题。
回答by Martin Becker
This looks to be an old thread, but I recently ran in to this in my organization due to UAC requirements they were not using. I am still testing this, but I believe you can still enable interactive mode on a scheduled task by using the /Change
command on the task and adding the /IT
flag to make it interactive. Referenced here: https://docs.microsoft.com/en-us/windows/desktop/taskschd/schtasks
这看起来是一个旧线程,但由于他们没有使用 UAC 要求,我最近在我的组织中遇到了这个问题。我仍在对此进行测试,但我相信您仍然可以通过在任务上使用/Change
命令并添加/IT
标志以使其具有交互性,从而在计划任务上启用交互模式。此处引用:https: //docs.microsoft.com/en-us/windows/desktop/taskschd/schtasks
schtasks /Change /tn "Task A" /IT /RP "password of user if used"
My initial tests show this to be working, however I cannot noticeably see a difference to the task in task scheduler when I do this. So, I am not sure of how to verify if it is set to do this.
我的初始测试表明这是有效的,但是当我这样做时,我看不到任务调度程序中的任务有什么不同。所以,我不确定如何验证它是否设置为这样做。