how to create a scheduled task that runs when any user logins to the system

15,839

Solution 1

After struggling my head for so many days, I finally found the answer for running the program as admin

I wrote the following batch file to run one of my system program in admin mode without UAC Popup( it auto Enters the admin password )

I wrote a batch file run.bat with following content => it then executes a vb script which waits for 5 second and keys in the password.

================run.bat Start========================

set USER_NAME="administrator"
set PASSWORD="test"
set PROGRAM_NAME="C:\\ClassConnect\\class_student.bat"
set "cm=cscript /B /nologo runas4.vbs %PASSWORD%"
%cm%
runas /profile /env /user:%USER_NAME% "%PROGRAM_NAME%"

================run.bat End========================

================runas4.vbs Start========================

Set objArgs = Wscript.Arguments
password=objArgs(0)
set WshShell = WScript.CreateObject("Wscript.Shell")
WScript.Sleep 5000
bWindowFound = WshShell.AppActivate("ClassConnect_Teacher")
WScript.Sleep 500
WshShell.SendKeys password
WshShell.SendKeys "{ENTER}"
set WshShell = nothing

================runas4.vbs End========================

The above script waits for 5 second and then enters the password for runas command thus I am able to run the script in admin mode.

If you are not sure about your access rights, download the isadmin.exe from internet.

if you do not have admin access on the system , activate the default disabled Administrator account. You can activate the account by using

net user administrator /active:yes

For resetting the default administrator password use: net user administrator *

Solution 2

I would recommend you to put your class_server.cmd file in the alluser start-up folder: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

Or call your .cmd file via shortcut and runas in the start-up folder to solve the UAC problem. Follow this documentation: http://www.howtogeek.com/124087/how-to-create-a-shortcut-that-lets-a-standard-user-run-an-application-as-administrator/

Share:
15,839
Rajesh Agrawal
Author by

Rajesh Agrawal

We believe in making use of the software the way it was meant to be. To reduce/ease human effort and that’s what we have been doing since the start of TechnoWings in 2006.

Updated on June 05, 2022

Comments

  • Rajesh Agrawal
    Rajesh Agrawal almost 2 years

    I want to launch an exe file of my product (C:\ClassConnect\class_server.cmd) on user login. I tried 2 solutions ( but nothing seems to work) Solution 1 : ( Added Startup Shortcut )

    It asks the user for UAC dialog, which obviously my users will not accept as its a spy app. Solution 2 : ( Added batch to windows scheduler so that it runs for any user)

    It runs fine with the administrator account but fails for other users. Moreover I am not able to view scheduled tasks on other users

    Please help. ( I want the batch to run on startup for all users on my machine)

  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    but in that case it asks for UAC dialog and moreover I want to run the batch as an administrator
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    Visited your reference webpage I tried creating a windows task -> it works fine with local administrator account. It does not launch the batch on other accounts.
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    Thanks for the solution ! The app that I am trying to run is a spy app, When I create a shortcut to run as administrator -> Everytime I restart my machine it asks for password which a person sitting on the computer will not enter. Kindly provide a solution wherein we don't need to enter password multiple times ( first time is OKAY)
  • Yanick Schraner
    Yanick Schraner over 9 years
    well I tried the solution and I just have to enter the PW once. Maybe there is something wrong with your runas command. Could you post it?
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    C:\Windows\System32\runas.exe /user:TWINGS-COMP7\technowings-pc /savecred "C:\Program Files (x86)\ClassConnect\class_student.bat"
  • Yanick Schraner
    Yanick Schraner over 9 years
    Is there a software installed which resets your HDD after a reboot? Something like HDGuard?
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    No ... I tried it on 5 machines and not working any of them .. Machines are Windows 8,Windows 7 64 bit and 32 bit
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    See when I run the [runas /savecred /user:TWINGS-COMP7\technowings-pc BATCH_FILE_PATH] on standard user account -> It asks for the password only for the first time but in admin account it keeps asking password after restart of machine
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    I tried the following script for run as admin [runas /profile /env /user:%user_name1% "%~1"
  • Rajesh Agrawal
    Rajesh Agrawal over 9 years
    @yanick can you review?