none
List scheduled tasks on remote servers (vbscript)

    Question

  • How can I list all scheduled tasks on a remote computer with details including the user account the job runs as?
    I have tried the sample code at http://gallery.technet.microsoft.com/ScriptCenter/en-us/36acfeaf-5229-47fb-b50e-a9c0b4384d8b but that only works on my local machine.
    If I change strComputer = "." to strComputer = "Server1" no results are returned.

    I have about 250 servers to check and I don;t have Powershell installed on any of them, so as an alternative, that's unfortunately a non starter.

    Thanks in advance,
    Iain
    Wednesday, November 18, 2009 10:41 AM

Answers

  • Have you run the command on the server as it states :
    "Enumerates all the scheduled tasks on a computer. Note: WMI can only enumerate scheduled tasks created using the Win32_ScheduledJob class or the At.exe utility. It cannot enumerate tasks created using the Task Scheduler"

    So you muight find the scheduled tasks were created using schedular. I use the "schtasks" cmd locally on all machines and out put it to a file and pull that off the server. Not ideal I know.
    • Marked as answer by IainM1 Friday, November 20, 2009 8:15 AM
    Wednesday, November 18, 2009 10:57 AM

All replies

  • Have you run the command on the server as it states :
    "Enumerates all the scheduled tasks on a computer. Note: WMI can only enumerate scheduled tasks created using the Win32_ScheduledJob class or the At.exe utility. It cannot enumerate tasks created using the Task Scheduler"

    So you muight find the scheduled tasks were created using schedular. I use the "schtasks" cmd locally on all machines and out put it to a file and pull that off the server. Not ideal I know.
    • Marked as answer by IainM1 Friday, November 20, 2009 8:15 AM
    Wednesday, November 18, 2009 10:57 AM
  • Thanks a lot for replying. That could be the answer. I'll probably go with the schtasks command.
    Friday, November 20, 2009 8:16 AM
  • Have you found a solution?  I'm being asked to do the same.
    Monday, May 09, 2011 4:33 PM
  • Hi,

    This thread is already marked as answered. Use the schtasks command and redirect its output to a file.

    Bill

    Monday, May 09, 2011 4:43 PM
  • Well, I was looking for a "real" answer.  doing the schtasks command is easy but running it against a response file and applying the results to an output file is what I was asking.

    I've been working on this most of the day and I'm almost there; I'll post the answer when I'm finished.

    Monday, May 09, 2011 6:31 PM
  • Hi,

    I don't know what "running it against a response file" means. Schtasks uses command-line parameters to specify the computer against which you want to enumerate scheduled tasks. For example:

    schtasks /query /s computername

    As far as it being a "real" answer, well, sometimes you need to use what works in order to get the job done.

    HTH,

    Bill

    Monday, May 09, 2011 6:58 PM
  • Yea, I hear ya, udnerstand, and agree.  I was thinking there's an easier way.  For example.  I grabbed a vbscript off MS web site which allows for me to ping all servers.  The script looks at a "response file" (maybe not the best words to use) which has all servers I'd like for it to ping.  The vbscript then creates a csv file so I can view the results.

    Is there an option to use in place of computername so I can grab all results for all servers?  Like /s cn=,ou=,dc=  (schtasks /query /s cn=corp,ou=acme,dc=com) this way I would be able to collect on all servers.

    What I use now for singel member server information:  schtasks.exe /query /s servername /v /fo csv /nh >> \\my-local-pc\d$\vbscripts\sched_tasks.csv

    Here's a vbscript (don't laugh) I'm trying to use but it just fails and probably because it's not actually executing the schtasks.exe command on the servers I'd like to collect from.  As you'll see, it looks just like the ping command on MS web site.

    'This script will use a response file to to collect detailed information on scheduled tasks.
    'The response file must have each server listed on its own line with a hard return after the name.
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("WScript.Shell")

    tempobj="sched_server_list.txt"

    Set objTextFile = objFSO.OpenTextFile("2_fnma_server_list.txt", ForReading)

    'The next 4 lines creates the output for each schedule task on each server
    logfile="details_sched_tasks.csv"
    Set ofile=objFSO.CreateTextFile(logfile,True)
    strText = objTextFile.ReadAll
    objTextFile.Close

    wscript.echo "collecting scheduled tasks on member servers; please be patient"&_
    vbcrlf&"finish, depending on the number of hosts to check.  You "_
    &"will be "&vbcrlf&"notified upon the completion of this script."
    ofile.WriteLine ","&"script -- Date: " & Now() & vbCrLf

    arrComputers = Split(strText, vbCrLF)
    for each item in arrcomputers
    objShell.Run "cmd /c schtasks /query /v " & item & " >sched_server_list.txt", 0, True
    Set tempfile = objFSO.OpenTextFile(tempobj,ForReading)
    Do Until tempfile.AtEndOfStream
    temp=tempfile.readall
    '  striploc = InStr(temp,"[")
    '        If striploc=0 Then
    '            strip=""
    '        Else
    '            strip=Mid(temp,striploc,14)
    '            strip=Replace(strip,"[","")
    '            strip=Replace(strip,"]","")
    '            strip=Replace(strip,"w"," ")
    '            strip=Replace(strip," ","")
    '        End If     
       
    '        If InStr(temp, "Reply from") Then
    '             ofile.writeline item & ","&strip&","&"Online."
    '        ElseIf InStr(temp, "Request timed out.") Then
    '             ofile.writeline item &","&strip&","&"No response (Offline)."
    '        ELSEIf InStr(temp, "try again") Then
    '             ofile.writeline item & ","&strip&","&"Unknown host (no DNS entry)."
            
    'End If
    Loop
    Next
    tempfile.close
    objfso.deletefile(tempobj)
    ofile.writeline
    ofile.writeline ","&"Ping batch complete "&now()

     


    Wednesday, May 11, 2011 6:12 PM
  • Okay, I've figured this out.

    Detailed Views of Scheduled Tasks

    -  Create a file named server_list.txt and supply all the names of the servers you wish to run the command against.
    Log onto a server in the domain you wish to run the command against.
    Place the file into a directory like d:\temp or even just the root of d:\
    Open a command prompt and go to the same directory you have your list of servers.
    Copy/paste the below command into the command window and hit enter.
            for /f %x in (server_list.txt) do (schtasks /query /s %x /v /fo csv /nh >> d:\sched_tasks.csv)
     
    Now I need to figure out how I can change an account on all 2,200 servers.
    • Proposed as answer by Jerzdevs Thursday, May 12, 2011 5:23 PM
    Thursday, May 12, 2011 5:23 PM
  • This last post from Jerzdevs just saved me hours of work

    Thanks to Jerzdevs

    Wednesday, May 09, 2012 9:17 PM
  • And old post, but good info: Exactly as Jerzdevs said, do the 'for' loop with the schtasks command.
    Some tips:

    1) You don't have to "log onto any server," IF you already have the necessary admin privs - i.e., you can run this fine from a workstation (depending on o/s, etc. - runs fine from my Windows 7 Pro 64-bit workstation)

    2) You don't have to go to any directory where you have your list of servers, IF you always open the "input file" (not 'response file,' fyi :) with "full path;" i.e., always keep a "c:\scripts" folder for scripts & input files, place server_list.txt file & "tl.bat" file in "c:\scripts" folder:

    for /f %%x in (c:\scripts\server_list.txt) do (schtasks /query /s %%x /v /fo csv /nh >> c:\scripts\sched_tasks.csv)
        and then, from wherever you happen to be, just execute c:\scripts\tl

    3) Your example will NOT work in a script, because you have to "double" the "%%" inside a script - that's just always been the case. Interactively, you can use one "%," but, in a batch script, you have to double those, in order to make it work.

    4) To make it MUCH sweeter, you could feed a "dsquery" command to your "for loop," that way, you get a dynamic listing of your servers, and you don't have to use a static "servers.txt" file. Alternatively, you could feed the output of dsquey to a file:
     like this: dsquery computer "OU=Servers,DC=Your-Domain-Name,DC=com" > c:\scripts\server_raw.txt

    So your final script [MIGHT] look sort of like my script below (CHANGE YOUR-DOMAIN etc. to match YOUR info!)
    NOTE: I just tested this (actually TWO scripts - c:\scripts\tl.bat and c:\scripts\stripit.bat) and it worked like a charm!

    REM - Save this script as c:\scripts\tl.bat (it contains the 2nd script c:\scripts\stripit.bat near the bottom)
    REM - c:\scripts\tl.bat - List all schedule tasks on all remote servers

    if exist c:\scripts\server_raw.txt del c:\scripts\server_raw.txt
    if exist c:\scripts\server_list.txt del c:\scripts\server_list.txt

    dsquery computer ou="COMPUTERS - Servers",dc=YOUR-DOMAIN,dc=COM | dsget computer -samid -c > c:\scripts\server_raw.txt

    REM - [stripit.bat] strips the "$" from the end of the server name in "server_raw.txt" and creates CLEAN "server_list.txt"

    for /f %%a in (c:\scripts\server_raw.txt) do call c:\scripts\stripit.bat %%a

    for /f %%a in (c:\scripts\server_list.txt) do schtasks /query /s %%a /v /fo csv >> c:\scripts\sched_tasks.csv

    goto fin

    REM - You can use below lines to make a stripit.bat file (else turn below into subroutine)
    REM - Take out the FIRST "REM" in each line below, and save it as c:\scripts\stripit.bat (first 2 lines skip the "dsget" and "samid" lines)

    REM REM c:\scripts\stripit.bat
    REM if %1%==dsget goto skipit
    REM if %1%==samid goto skipit
    REM set sname=%1
    REM set sname=%sname:~0,-1%
    REM echo %sname% >> c:\scripts\server_list.txt
    REM :skipit

    :fin

    5) To your other issue: To change an account on "x number of servers," use "psservice" and/or "psexec" (part of the free "pstools" suite).
    I have used those with great success to do that exact task: to change service account name, password, etc. on remote machines.

    NOTE ALSO: My example is NOT "elegant" - there probably are much more elegant ways to do this.


    tnjman

    • Proposed as answer by titanyoda Saturday, June 08, 2013 10:40 PM
    Friday, June 07, 2013 6:08 PM
  • very helpful thanks
    Sunday, June 09, 2013 9:54 AM