In the Task Scheduler I have a task using the SYSTEM account that runs a batch file. The user (non-administrator) has another batch file that runs the task. How can the user's batch set a variable that can be used by the task's batch?
1 Answers
Dynamically set a variable value in a batch script that'll be executed by Task Scheduler
Since you clarified and confirmed the following:
- You need the variable value in the batch script that the Task Scheduler executes as
SYSTEMto be set to as a value that matches the SID of the user that runs the login script- You can use local file path per machine for the script location the Task Scheduler will execute
- You will never have more than one user signing onto the same OS concurrently that'll execute this script
I've written this solution up as a separate answer with more detail of the approach suggested.
You will want to ensure the following:
- Confirm the correlated Scheduled Task name is setup on each machine that will execute the batch script locally
- Confirm that each correlated Scheduled Task on each machine that is executed with
schtasksall point to the same one standard batch script path and file name.
Batch [Login] Script Example
I reused the logic you already have setup for this, but I added a few extra variables and conditional IF logic that will make the local folder on local machine the batch script will reside if it does not already exist, and delete the batch file in that folder if it does already exist since it's created per run.
Essentially this will create a dynamic batch script each time it is executed (at login). The batch script it creates will contain the Reg Add logic and commands with the SID portion values being that of the user the login script ran for at their login. So when the Task Scheduler executes the script, this will ensure the script always has the needed value added per login/script execution.
@ECHO ON
SET LocalDir=C:\localfolder
SET TaskScript=TaskScript.cmd
IF NOT EXIST "%LocalDir%" MD "%LocalDir%"
IF EXIST "%LocalDir%\%TaskScript%" DEL /Q /F "%LocalDir%\%TaskScript%"
for /f "tokens=2" %%i in ('whoami /user /fo table /nh') do set usersid=%%i
ECHO reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%usersid%" /v "State" /t REG_DWORD /d 128 /f>>"%LocalDir%\%TaskScript%"
schtasks /run /tn "My Profile Changer"
Scheduled Task
You will only need to ensure that the task which you tell the schtasks to execute (i.e. "My Profile Changer") to point to the same full path as you specify in the login batch script for the SET LocalDir= and SET TaskScript= variable values.
So if the login script variables equal C:\localfolder\TaskScript.cmd then this is the same value you want the Program/Script field to point to. I would also suggest putting the SET LocalDir= variable value in the Start in (optional): field but you will NOT need to add any arguments so leave the Add Arguments (optional): field blank or empty with nothing in it (below screen shot).
Further Resources
- 42,624
