Solved List Job Scheduled to Run Under a particular User

June 11, 2015 at 10:57:12
Specs: Windows 7
i need to check in task schedular which all jobs are scheduled under a particular user . suppose i have jobs in task scheduler : A, B and C . A job when run, it runs under user "XYZ".
Job B and C they run under user "RQS".

is it possible to list out the jobs which are running under RQS using dos command or batch script ?

I need to disable those jobs running under RQS user .

Any help would be very appreciable. I am stuck in this task from last 2-3 week ..

Thanks !!!

See More: List Job Scheduled to Run Under a particular User

Report •

June 14, 2015 at 09:24:14
This should accomplish your task (assuming the processes run on the same computer as the script is executed):

@Echo off
setlocal enabledelayedexpansion
for /f %%A in ('tasklist /fi "USERNAME eq %COMPUTERNAME%\RQS" /fo CSV') do (
	for /f "tokens=1 delims=," %%B in ("%%A") do (
		set process=%%B
		set process=!process:"=!
		Echo !process! | find /i "." > nul
		if !errorlevel!==0 (
			Echo !process!
			REM Remove "::" below to make it terminate the processes.
			:: taskkill /f /im "!process!"
Echo Job complete.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ

Report •

June 14, 2015 at 09:55:43
✔ Best Answer
Well, if you're insistent on batch, you could use something like this. Parsing the output is left as an exercise for the reader, and false positives are possible.
schtasks /query /fo csv /v | findstr /i "RDS"

If you were inclined to use one of Windows' better scripting languages, you could talk directly to the Task Scheduler, and ask the tasks to be disabled. For instance, here's VBScript.
If Not HasConsoleOut Then _
  WScript.Quit CreateObject("WScript.Shell").Run( _
    "cmd /k cscript " & WScript.ScriptFullName, 1, True)

Set regex = New RegExp
regex.IgnoreCase = True : regex.Pattern = "\\RDS$"
Set sched = CreateObject("Schedule.Service")
EnumFolder sched.GetFolder("\")
WScript.Quit 0

Sub EnumFolder(oFolder)
  For Each task in oFolder.GetTasks(1)
    If (regex.Execute(task.Definition.Principal.UserId).Count) Then
      WScript.Echo task.Path
      task.Enabled = False
    End If
  Next 'task
  For Each folder in oFolder.GetFolders(1)
    EnumFolder folder
  Next 'folder
End Sub

Function HasConsoleOut
  On Error Resume Next
  WScript.StdOut.WriteLine ""
  HasConsoleOut = Err.Number = 0
End Function

How To Ask Questions The Smart Way

message edited by Razor2.3

Report •
Related Solutions

Ask Question