• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Task Scheduler


Dwarf

The Contemplator, (1963-2013)
Moderator
Doncaster, UK

Posts
638
Task Scheduler
This Tutorial will show you how to create a Scheduled Logon Task.
Published by Dwarf
#1
Category:
ByLine
How to create a Logon task using Task Scheduler
Synopsis
This Tutorial will show you how to create a Scheduled Logon Task.
information   Information
This Tutorial will show you how to create a Scheduled Logon Task.


Note   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.

Capture.png

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   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.


Capture1.PNG Capture2.PNG

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.

Capture3.PNG Capture4.PNG

Note   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.

Capture5.PNG Capture6.PNG

Note   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   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.

Capture7.PNG

We need to change the information under When running this task, use the following user account:, so click on Change User or Group...

Capture8.PNG

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.)

Capture9.PNG

Click OK.

Capture10.PNG

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.

Capture11.PNG

Click OK.

Capture12.PNG

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.

Capture13.PNG

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   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.
 
Last edited by a moderator:

My Computer

System One

  • OS
    Windows 8.1 Pro RTM x64
    Computer type
    PC/Desktop
    System Manufacturer/Model Number
    Dwarf Dwf/11/2012 r09/2013
    CPU
    Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.2GHz)
    Motherboard
    ASRock Z77 Extreme4-M
    Memory
    4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
    Graphics Card(s)
    MSI GeForce GTX770 Gaming OC 2GB
    Sound Card
    Realtek High Definition on board solution (ALC 898)
    Monitor(s) Displays
    ViewSonic VA1912w Widescreen
    Screen Resolution
    1440x900
    Hard Drives
    OCZ Agility 3 120GB SATA III x2 (RAID 0)
    Samsung HD501LJ 500GB SATA II x2
    Hitachi HDS721010CLA332 1TB SATA II
    Iomega 1.5TB Ext USB 2.0
    WD 2.0TB Ext USB 3.0
    PSU
    XFX Pro Series 850W Semi-Modular
    Case
    Gigabyte IF233
    Cooling
    1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
    Keyboard
    Microsoft Comfort Curve Keyboard 3000 (USB)
    Mouse
    Microsoft Comfort Mouse 3000 for Business (USB)
    Internet Speed
    NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/sec (O2)
    Browser
    IE 11
    Antivirus
    Avast! 8.0.1497 MalwareBytes 1.75.0.1300
    Other Info
    Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
    Lexmark S305 Printer/Scanner/Copier (USB)
    WEI Score: 8.1/8.1/8.5/8.5/8.25
    Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)

JaidynM

BSOD Kernel Dump Analyst
VIP Member
Power User
Gold Member
#2
Great tutorial, Guy! I'll be tipping the scales. :)
 

My Computer

System One

  • OS
    Windows 7 Ultimate x64/Windows 8 Consumer Preview x64/Ubuntu 11.04
    System Manufacturer/Model Number
    Custom Build
    CPU
    Intel Pentium Dual Core E6700 3.2GHz
    Motherboard
    ASUS P5G41T-M LX
    Memory
    Strontium 8192MB DDR3 1333Mhz
    Graphics Card(s)
    msi GeForce N560GTX-M2D1GD5 1GB
    Sound Card
    Realtek HD Audio
    Monitor(s) Displays
    CHIMEI CMV 221D 22"
    Screen Resolution
    1680x1050
    Hard Drives
    Seagate 3.5'' 2TB HDD/WD Elements External 1TB HDD
    PSU
    SHAW Viper-1500w Gaming PSU
    Case
    Black eMaxx ATX Mini Tower Case
    Keyboard
    Microsoft Wireless Keyboard 1000
    Mouse
    Microsoft Wireless Optical Mouse 2000
    Internet Speed
    100GB @ 4.76Mbps
Posts
4
#3
Thanks, Dwarf!!!! You tried to help me, but I;m too much of a doofus to follow you after this point:

"Section TWO

Instead of choosing Display a message for the Action:, select Start a program instead, as shown below."

Your sample program was sumpn about "logon....." What is that, and how would I go about selecting my target task from a long list of items that are unfamiliar and udecipherable to me? I want to schedule a Norton scan in the wee hours, but I dont see that option listed in my elementary English. And I fear getting into the register like poison!
 

My Computer

System One

  • OS
    windows 8.1
    Computer type
    Laptop
    System Manufacturer/Model Number
    hp envy
    Browser
    IE, FIREFOX
    Antivirus
    norton