您如何设置使用vbs在没有管理员权限的情况下登录时以当前用户身份运行的任务? [英] How do you set a task to run as the current user at logon using vbs without admin rights?

查看:106
本文介绍了您如何设置使用vbs在没有管理员权限的情况下登录时以当前用户身份运行的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在命令行中,我可以创建一个计划的任务,该任务在登录时运行,而无需管理员权限或用户输入密码来设置任务.但是,我必须使用xml文件来执行此操作.这是一个示例xml,其中"Domain \ User"部分必须在运行时替换为当前用户的域名和名称:

From the command line I can create a scheduled task that runs at logon without the need for admin privileges or the user entering a password to set the task. However I have to use an xml file to do this. here's an example xml, where the "Domain\User" part has to replaced at run time with the domain and name of the current user:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Author>Domain\User</Author>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <Enabled>true</Enabled>
      <UserId>Domain\User</UserId>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>Domain\User</UserId>
      <LogonType>InteractiveToken</LogonType>
    </Principal>
  </Principals>
  <Settings>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Windows\notepad.exe"</Command>
      <Arguments>/logon</Arguments>
    </Exec>
  </Actions>
</Task>

我可以使用xml使用以下命令创建任务:

I can use the xml to create a task using the command:

schtasks /create /TN "Test Logon Task" /XML task.xml

这工作正常,但是我试图在vbs中做同样的事情,而无需编写然后为用户加载xml文件.我一直在尝试适应来自MSDN的示例,但到目前为止,没有管理员权限或提示输入用户密码,我仍然无法添加任务.

This works fine but I'm trying to do the same thing in vbs without the need to write and then load an xml file for the user. I've been trying to adapt the example from MSDN but so far I've not been able to add the task without admin rights or prompting for the user's password.

有人知道该怎么做吗?

推荐答案

可能是Schtasks.exe-来自Microsoft文档

Probably Schtasks.exe - From Microsoft documentation

Permissions for schtasks

    You must have permission to run the command. Any user can schedule
    a task on the local computer, and they can view and change the tasks
    that they scheduled. Members of the Administrators group can schedule,
    view, and change all tasks on the local computer.

其中一个参数(/sc onlogon)允许定义将在登录时运行的任务.

And one of the parameters (/sc onlogon) allow to define a task that will run at logon.

似乎可能是答案,但我没有尝试过.

Seems possible to be the answer, BUT i've not tried.

编辑

由于schtasks不能解决问题,所以我从Microsoft那里获取了原始代码并适应了工作(希望对我有用)

Since schtasks proved not to be the answer, i've taken the original code from Microsoft and adapt to work (i hope, works for me)

'---------------------------------------------------------
' This sample schedules a task to start notepad.exe when a user logs on.
'---------------------------------------------------------

' A constant that specifies a logon trigger.
const TriggerTypeLogon = 9
' A constant that specifies an executable action.
const ActionTypeExecutable = 0   

const TASK_LOGON_INTERACTIVE_TOKEN = 3

'********************************************************
' Create the TaskService object.
Set service = CreateObject("Schedule.Service")
call service.Connect()

'********************************************************
' Get a folder to create a task definition in. 
Dim rootFolder
Set rootFolder = service.GetFolder("\")

' The taskDefinition variable is the TaskDefinition object.
Dim taskDefinition
' The flags parameter is 0 because it is not supported.
Set taskDefinition = service.NewTask(0) 

'********************************************************
' Define information about the task.

' Set the registration info for the task by 
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Task will execute Notepad when a " & _
    "specified user logs on."
regInfo.Author = "Author Name"

' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.StartWhenAvailable = True

'********************************************************
' Create a logon trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeLogon)

' Trigger variables that define when the trigger is active.
Dim startTime, endTime
startTime = "2013-10-22T10:49:02"
endTime = "2013-10-30T10:52:02"

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M"    ' Five minutes
trigger.Id = "LogonTriggerId"
trigger.UserId = "MyDomainOrComputer\testUser"   ' Must be a valid user account   

'***********************************************************
' Create the action for the task to execute.

' Add an action to the task. The action executes notepad.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExecutable )
Action.Path = "C:\Windows\System32\notepad.exe"

WScript.Echo "Task definition created. About to submit the task..."

'***********************************************************
' Deal with the password problems

taskDefinition.Principal.UserId = "MyDomainOrComputer\testUser"
taskDefinition.Principal.LogonType = TASK_LOGON_INTERACTIVE_TOKEN

'***********************************************************
' Register (create) the task.
const createOrUpdateTask = 6
call rootFolder.RegisterTaskDefinition( _
    "Test Logon Trigger", taskDefinition, createOrUpdateTask, _
    Empty , Empty, TASK_LOGON_INTERACTIVE_TOKEN)

WScript.Echo "Task submitted."

将RegisterTaskDefinition调用更改为不传递任务的凭据,并配置taskDefinition的del Principal对象属性

Changed the RegisterTaskDefinition call to not pass credentials for the task, and configured del Principal object properties of the taskDefinition

向非管理员用户注册的任务,无密码提示.

Task registered with non admin user, without password prompt.

这篇关于您如何设置使用vbs在没有管理员权限的情况下登录时以当前用户身份运行的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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