Information
This Tutorial will show you how to create a Scheduled Logon Task.
Note
You should, ideally, perform this Tutorial from within an account with administrative privileges.
Section ONE
Open Task Scheduler.
Click Create Task...
This will open the following dialogue panel, with the General tab selected. Give the task a suitable Name and Description. Also, change the other settings (if different) to match those shown below.
Click the Triggers tab and then click New...
Enter the settings as shown below. If you wish, you can set a start date (as shown below), but if you don't want (or need) to, just uncheck the Activate: option. Click OK.
Note
You can set a start date and/or end date for the task if you so choose. Just check the appropriate option(s) shown below (Activate: for the start date and/or Expires: for the end date). I suggest that you set the time(s) to 00:00:00 (midnight), and that you check the option(s) Synchronize across time zones.
Click the Actions tab and then click New...
Enter the settings as shown below (note that you can change the message to whatever you want it to display) and click OK.
Note
This only provides for a simple text message to be displayed for each user, with each user having the same message. If that is all you require, skip over the next section, but for more flexibility, see below.
Section TWO
Instead of choosing Display a message for the Action:, select Start a program instead, as shown below.
Note
The following blocks of code are what actually carry out the action when the task is triggered. In all cases, replace the data in BLUE with the actual UserName(s) and the data in <GREEN> with the routine to be executed. For the third block of code, you can choose to use the same routine or a different routine. With the last block of code, a different routine should be used for each user.
With this script, the routine is executed only if the current user IS the user referenced to by MyStr.
Code:
Dim StrUser, MyStr
Set wshNetwork = CreateObject("WScript.Network")
Set wshShell = CreateObject("WScript.Shell")
StrUser = wshNetwork.Username
MyStr = "[COLOR=#0000ff]account[/COLOR]"
If StrComp(StrUser, MyStr, 1) = 0 Then
wshShell.Run ("[COLOR=#008000]<Routine, including path>[/COLOR]")
End If
In this script, the routine is executed if the current user IS NOT the user referenced to by MyStr.
Code:
Dim StrUser, MyStr
Set wshNetwork = CreateObject("WScript.Network")
Set wshShell = CreateObject("WScript.Shell")
StrUser = wshNetwork.Username
MyStr = "[COLOR=#0000ff]account[/COLOR]"
If StrComp(StrUser, MyStr, 1) <> 0 Then
wshShell.Run ("[COLOR=#008000]<Routine, including path>[/COLOR]")
End If
With this script, a routine is executed if the current user IS one of the users referenced to by MyStr1 or MyStr2. Note that the routine can be different for each user, and that you may need to adapt the code to fit in with the accounts on your system. For this example, account1 will execute Routine1, account2 will execute Routine2, and account3 will not execute either routine.
Code:
Dim StrUser, MyStr1, MyStr2, MyStr3
Set wshNetwork = CreateObject("WScript.Network")
Set wshShell = CreateObject("WScript.Shell")
StrUser = wshNetwork.Username
MyStr1 = "[COLOR=#0000ff]account1[/COLOR]"
MyStr2 = "[COLOR=#0000ff]account2[/COLOR]"
MyStr3 = "[COLOR=#0000ff]account3[/COLOR]"
If StrComp(StrUser, MyStr1, 1) = 0 Then
wshShell.Run ("[COLOR=#008000]<Routine1, including path>[/COLOR]")
ElseIf StrComp(StrUser, MyStr2, 1) = 0 Then
wshShell.Run ("[COLOR=#008000]<Routine2, including path>[/COLOR]")
End If
With this script, Routine1 is executed if the current user IS the user referenced to by MyStr. All other users execute Routine2.
Code:
Dim StrUser, MyStr
Set wshNetwork = CreateObject("WScript.Network")
Set wshShell = CreateObject("WScript.Shell")
StrUser = wshNetwork.Username
MyStr = "[COLOR=#0000ff]account[/COLOR]"
If StrComp(StrUser, MyStr, 1) = 0 Then
wshShell.Run ("[COLOR=#008000]<Routine1, including path>[/COLOR]")
Else
wshShell.Run ("[COLOR=#008000]<Routine2, including path>[/COLOR]")
End If
Note
If you want to display a simple message, or messages, simply replace wshShell.Run ("<Routine, including path>") with MsgBox "<Message>", vbSystemModal (change <Message> to whatever you want to display).
All of the above blocks of code are vbscript files, and should be created using Notepad, saving with a .vbs extension. For the purposes of this Tutorial, the file is saved in the root of the C:\ drive as logonscript.vbs, although you can change the name and location if you wish. Just make sure that the name and location are updated above to reflect any changes.
Since we don't need to change any further settings, click on OK to finish creating the task and close the Task Scheduler.
Section THREE
Triggering a task at logon Logon tasks can be set for any user or a specific user or user group. If the user whose logon has triggered the task is not the user in whose security context the task is running, the task will be noninteractive - in other words, essentially invisible. (The user can note the presence of the task - and terminate it - by running Windows Task Manager, going to the Processes tab, clicking Show Processes From All Users, and answering the UAC prompt.)
To overcome this limitation, follow the steps below.
Open Task Scheduler, click Task Scheduler Library to bring up the list of tasks, highlight the one you've just created and click Properties to bring up the following dialogue.
We need to change the information under When running this task, use the following user account:, so click on Change User or Group...
Click Object Types... and change the settings so that only Groups is selected, as shown below. (Note that this is optional, it only narrows down the list you have to look through in the next step. If you wish to omit this step, click on Advanced instead and move on to the next step.)
Click OK.
Click Advanced... and then click Find Now. This will bring up a list of items that match the search criteria. Scroll through and highlight Users as shown below.
Click OK.
Click OK. If the next dialogue comes up as shown below, you have been successful. If not, review the above steps to ensure that you haven't missed anything.
Click OK to save the task, and close the Task Scheduler. Regardless of the user logging on, this task will execute and s/he will see the results of it on their screen in their account.
Warning
You need to ensure that all users can freely execute the vbscript files created in the implementation of this Tutorial. Failure to do so will result in the code not being able to be executed in one or more accounts. No error message will be given, and, in tasks where a sequential set of actions is to be performed, only those actions preceding will be executed.
Attachments
Last edited by a moderator: