2013-10-22 59 views
3

從命令行我可以創建一個計劃任務,在登錄時運行而不需要管理員權限或用戶輸入密碼來設置任務。不過,我必須使用一個XML文件來做到這一點。這裏是一個示例XML,這裏的「域\用戶」部分在運行時替換爲當前用戶的域和名稱:如何使用沒有管理員權限的vbs將任務設置爲當前用戶登錄時運行?

<?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使用命令創建任務:

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

這工作正常,但我想在vbs中做同樣的事情,而無需編寫,然後爲用戶加載一個XML文件。我一直在嘗試修改example from MSDN,但到目前爲止,我還沒有能夠在沒有管理員權限或提示輸入用戶密碼的情況下添加任務。

有誰知道如何做到這一點?

回答

3

大概SCHTASKS.EXE - 從微軟的文檔

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)允許定義將在登錄時運行任務之一。

似乎可能是答案,但我沒有嘗試過。

編輯

由於schtasks的證明不是答案,我從微軟所採取的原代碼和適應工作(我希望對我的作品)

'--------------------------------------------------------- 
' 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通話不傳遞任務憑證,並配置del Principal對象屬性的任務定義

任務註冊與非管理員用戶,沒有密碼提示。

+0

我試過了,它不起作用。你需要有用戶的密碼。到目前爲止,我唯一需要做的就是導入一個XML。 – ChrisD

+0

也許,你可以使用xml創建任務,使用Schedule.Service對象和RegisteredTask屬性([this](http://msdn.microsoft.com/en-us/library/windows/desktop/aa382079%28v= vs.85%29.aspx)和[this](http://msdn.microsoft.com/en-us/library/windows/desktop/aa446865%28v=vs.85%29.aspx)來訪問任務屬性,並使用GetSecurityDescriptor方法來獲取創建任務時需要的SetSecurityDescriptor方法所需的信息。對不起,沒有時間,祝您好運。 –

+0

請參閱更新後的答案。 –

相關問題